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
Author: YE, Zicheng
Source: ZOJ Monthly, March 2018
题意:初始在0点,走n步走到m点,向左或向右走的概率为1/4,停留的概率为1/2,走到m点的概率为P/Q,输出P*inv(Q)%1e9+7
题解:起初傻傻的认为直接走到m点不就好了嘛?大胆交题,WA到怀疑人生,冷静下来想了一下是不是还可能考虑在哪步停留,哪步左走,哪步右走,但是这组合数学加概率还真不知道咋写,当时就没做出来,现在重新回顾这道题发现并不是特别困难。
首先可直接m步走到m点,然后考虑在最终结果还停留在这个点的情况下进行左右移动和停留,当n-m为奇数的时候,因为当移动到m点时左移一定等于右移,所以停留一定为奇数,n-m为偶数时,停留为偶数,便可枚举停留的步数i,利用组合数学,先从n步中挑出i步停留,然后再从n-i步中选出left步进行左移,那剩下的即为右移的,再乘上概率的逆元即可。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
#define gcd(a,b) __gcd(a,b)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll inv2(ll b){return b==1?1:(mod-mod/b)*inv2(mod%b)%mod;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
ll fac[1000005];
void init()
{
fac[0]=fac[1]=1;
for (int i=2;i<=1000000;i++) fac[i]=fac[i-1]*i%mod;
}
ll C(ll n,ll m)
{
return fac[n]*inv2(fac[m])%mod*inv1(fac[n-m])%mod;
}
int main ()
{
init();
int t;
t=read();
while(t--)
{
ll n,m;
n=read();
m=read();
m=m>=0?m:-m;
ll temp=n-m;
ll left,right;
ll sum=0;
if(temp%2)
{
for(ll i=1;i<=temp;i+=2)
{
left=(temp-i)/2;
right=left+m;
sum=(sum+C(n,i)*C(n-i,left)%mod*inv1(qpow(4,left+right)*qpow(2,i)%mod))%mod;
}
}
else
{
for(ll i=0;i<=temp;i+=2)
{
left=(temp-i)/2;
right=left+m;
sum=(sum+C(n,i)*C(n-i,left)%mod*inv1(qpow(4,left+right)*qpow(2,i)%mod))%mod;
}
}
printf("%lld\n",sum%mod);
}
return 0;
}