B - Travel along the Line ZOJ - 4006 组合数学

Travel along the Line


Time Limit: 1 Second      Memory Limit: 65536 KB


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

题意:一个人开始时在0原点,每秒有1/4的概率向左和向右走,1/2的概率留在原地。问在确切的n秒后在m处的概率为多大(对1e9+7取模)

 

做法:考虑停留在位置 m 的概率,只有(n-m)/2秒的机会可停留(两次)或向左(一左一右)走,n-m为奇数时一定会停留一次,若此时停留了i秒,则向左走了 ((n-m)/2-i) 步,则向右走了 m+(向左走) 步,枚举 i ,计算答案即可(大概思路)


代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
const int maxn=100005;
ll n,m,fac[maxn],inv_fac[maxn];
ll quick(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b/=2;
    }
    return ans;
}
ll C(ll n,ll m){return fac[n]*inv_fac[m]%mod*inv_fac[n-m]%mod;}
ll inv(ll x){return quick(x,mod-2);}
void init(){
    inv_fac[0]=fac[0]=1,fac[1]=inv_fac[1]=1;
    for(int i=2;i<maxn;i++)
        fac[i]=fac[i-1]*i%mod;
    for(int i=2;i<maxn;i++)
        inv_fac[i]=quick(fac[i],mod-2);
}
int main(){
    
    int t;
    cin>>t;
    while(t--){
        scanf("%lld%lld",&n,&m);
        m=abs(m);
        if(m>n){
            printf("0\n");
            continue;
        }
        ll ans=0,res=n-m,l,r;
        if(res%2){
            for(int i=1;i<=res;i+=2){
                l=(res-i)/2,r=l+m;
                ans=(ans+C(n,i)*C(n-i,l)%mod*inv(quick(2,i)*quick(4,l+r)%mod)%mod)%mod;
            }
        }
        else {
            for(int i=0;i<=res;i+=2){
                l=(res-i)/2,r=l+m;
                ans=(ans+C(n,i)*C(n-i,l)%mod*inv(quick(2,i)*quick(4,l+r)%mod)%mod)%mod;
            }
        }
        printf("%lld\n",ans);
    }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值