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;
}