C - Travel along the Line ZOJ - 4006

2023-11-02 11:58
文章标签 travel line zoj along 4006

本文主要是介绍C - Travel along the Line ZOJ - 4006,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目

C - Travel along the Line ZOJ - 4006 
BaoBao is traveling along a line with infinite length.At the beginning of his trip, he is standing at position 0. At the beginning of each second, if he is standing at position , with  probability he will move to position , with  probability he will move to position , and with probability he will stay at position . Positions can be positive, 0, or negative.DreamGrid, BaoBao's best friend, is waiting for him at position . BaoBao would like to meet DreamGrid at position  after exactly  seconds. Please help BaoBao calculate the probability he can get to position  after exactly  seconds.It's easy to show that the answer can be represented as , where  and  are coprime integers, and  is not divisible by . Please print the value of  modulo , where  is the multiplicative inverse of  modulo .Input
There are multiple test cases. The first line of the input contains an integer (about 10), indicating the number of test cases. For each test case:The first and only line contains two integers  and  (). Their meanings are described above.Output
For each test case output one integer, indicating the answer.Sample Input
3
2 -2
0 0
0 1
Sample Output
562500004
1
0

题解:
我们枚举向左移动的次数,那么很容易可以得到向右和保持不动的此处
然后根据公式

(nl)(nlr)(14)l+r(12)s=(nl)(nlr)(12)2(l+r)+s ( n l ) ( n − l r ) ( 1 4 ) l + r ( 1 2 ) s = ( n l ) ( n − l r ) ( 1 2 ) 2 ( l + r ) + s

    #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <queue>
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)#define mset(var,val) memset(var,val,sizeof(var))#define test(a) cout<<a<<endl
#define test2(a,b) cout<<a<<" "<<b<<endl#define test3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
const int N= 2e5;
const int mod =1e9+7;
using namespace std;
typedef long long ll;
ll a[N+10];
ll b[N+10];
ll fac[N+10];
ll inv(ll a){if(a==1)return 1;return inv(mod%a)*(mod-mod/a)%mod;
}
ll C(ll n,ll m){ll ans = fac[n]*(inv(1ll*fac[m]*fac[n-m]%mod));return ans % mod ;
}
void init(){fac[0]=1;b[0]=1;for(int i =1;i<=N;i++){fac[i]=(fac[i-1]*i)%mod;b[i]=(b[i-1]*2ll)%mod;}
}void work(){int n,y;scdd(n,y);long long ans=0;for(int i=0;i<=n;i++){int l = i;int r = y+i;int s = n-l-r;if(r<0||s<0||r>n||s>n)continue;ll son = C(n,l)*C(n-l,r)%mod;ll mon = inv(b[2*(l+r)+s]);ans =( ans + (1ll*son*mon))%mod;}printf("%lld\n",ans);
}
int main(){#ifdef localfreopen("in.txt","r",stdin);#endifint t;init();scd(t);while(t--){work();}
}

这篇关于C - Travel along the Line ZOJ - 4006的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/330428

相关文章

数论ZOJ 2562

题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最大的一个。 分析:反素数定义:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数。 性质一:一个反素数的质因子必然是从2开始连续的质数。 性质二:p=2^t1*3^t2*5^t3*7

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

zoj 4624

题目分析:有两排灯,每排n个,每个灯亮的概率为p,每个灯之间互不影响,亮了的灯不再灭,问两排中,每排有大于等于m个灯亮的概率。 设dp[ i ][ j ]为第一排亮了i个灯,第二排亮了j个灯,距离目标状态的期望天数。显然 i >= m ,j >= m时 , dp[ i ][ j ] = 0 。 状态转移 : 第一排亮了a个灯,a 在[ 0 , n - i] 之间,第二排亮了b个灯 , b 在

zoj 3228 ac自动机

给出一个字符串和若干个单词,问这些单词在字符串里面出现了多少次。单词前面为0表示这个单词可重叠出现,1为不可重叠出现。 Sample Input ab 2 0 ab 1 ab abababac 2 0 aba 1 aba abcdefghijklmnopqrstuvwxyz 3 0 abc 1 def 1 jmn Sample Output Case 1 1 1 Case 2

ZOJ Monthly, August 2014小记

最近太忙太忙,只能抽时间写几道简单题。不过我倒是明白要想水平提高不看题解是最好的了。 A  我只能死找规律了,无法证明 int a[50002][2] ;vector< vector<int> > gmax , gmin ;int main(){int n , i , j , k , cmax , cmin ;while(cin>>n){/* g

QT Travel

Code Resource: https://github.com/MoreYoungGavin/QT_Travel.git What is QT? QT is a cross-platform application development framework for desktop,embedded and mobile. What need install QT before? Yo

ZOJ 3324 Machine(线段树区间合并)

这道题网上很多代码是错误的,由于后台数据水,他们可以AC。 比如这组数据 10 3 p 0 9 r 0 5 r 6 9 输出应该是 0 1 1 所以有的人直接记录该区间是否被覆盖过的方法是错误的 正确方法应该是记录这段区间的最小高度(就是最接近初始位置的高度),和最小高度对应的最长左区间和右区间 开一个sum记录这段区间最小高度的块数,min_v 记录该区间最小高度 cover

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 24 in XML document from

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 24 in XML document from class path resource [bean1.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineN

【ZOJ】3362 Beer Problem 最小费用流

传送门:【ZOJ】3362 Beer Problem 题目分析:这道题本来应该很快就AC的,但是!因为我以前犯的一个致命错误导致我这题一天了到现在才调出来!唉。。失策。。貌似给的模板也有这个错误。。。马上就去改。。但是这个错误竟然还能过掉那么多的题。。害我还要一题一题的改回去。。 本题就是赤裸裸的最小费用流。 新建汇点t(源点即1),将所有的n-1个城市和汇点建边,容量为无穷大,

【ZOJ】2332 Gems 最大流——判断满流

传送门:【ZOJ】2332 Gems 题目分析:首先我们设立源点s,汇点t,s向所有宝石建边,容量为题目中给出的,然后所有可行的转换,向两个宝石之间建无向边,容量为INF,接下来所有的宝石向自己相应的类型建边,容量INF,所有的宝石向自己相应的颜色建边,容量INF。最后,所有的类型以及颜色向汇点建边,容量为题目中给出的。最后跑一遍最大流,如果满流,说明所有的宝石都成功的限制条件下分给了男主