B - 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
可以把它当做都往右走,再枚举站在原地不动的个数,再乘上概率的逆元就好了

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll jc[100005];
void init()
{
    jc[0]=1;
    ll ret=1;
    for(ll i=1;i<=100000;i++)
    {
        ret=(ret*i)%mod;
        jc[i]=ret;
    }
}
ll qpow(ll a,ll b)
{
    ll ret=a;
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*ret)%mod;
        ret=(ret*ret)%mod;
        b>>=1;
    }
    return ans;
}
ll c(ll a,ll b)
{
    return jc[a]*qpow(jc[b]*jc[a-b]%mod,mod-2)%mod;
}
int main()
{
    int t;
    scanf("%d",&t);
    init();
    while(t--)
    {
        ll n,m;
        scanf("%lld%lld",&n,&m);
        if(m<0)
            m=-m;
        ll ans=0;
        int maxn=n-m;
        if(maxn%2)
        {
            ll l,r;
            for(ll i=1;i<=maxn;i+=2)
            {
                l=maxn-i>>1;
                r=l+m;
                ans=(ans+c(n,i)*c(n-i,l)%mod*qpow(qpow(4,l+r)*qpow(2,i)%mod,mod-2)%mod)%mod;
            }
        }
        else
        {
            ll l,r;
            for(ll i=0;i<=maxn;i+=2)
            {
                l=maxn-i>>1;
                r=l+m;
                ans=(ans+c(n,i)*c(n-i,l)%mod*qpow(qpow(4,l+r)*qpow(2,i)%mod,mod-2)%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、付费专栏及课程。

余额充值