当前位置: 代码迷 >> 综合 >> C - Travel along the Line ZOJ - 4006
  详细解决方案

C - Travel along the Line ZOJ - 4006

热度:37   发布时间:2024-01-09 12:15:33.0

题目

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)(n?lr)(14)l+r(12)s=(nl)(n?lr)(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();}
}
  相关解决方案