ZCMU — 2157

2157: K.ly's travel plan

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 11   Solved: 3
[ Submit][ Status][ Web Board]

Description

The summer vacation is coming soon, ly began to prepare her vacation travel plan, she bought a ticket for a very amazing train, assuming that the route of this train is regarded as an X axis, ly will get on the train at the 0 position. Then began her journey. This magical train has a magical travel rule. When it is at coordinate X, it has a 1/4 probability of moving to (X+1) and a 1/4 probability of moving to (X-1) every day. There is a 1/2 probability of staying in place for one more day.
As a good friend of ly, wjw of course hopes to hang out with ly, but wjw needs to go home first, so wjw will wait for ly at the coordinate point M. Ly now wants to know how likely she is When I reach point M on the Nth day, I see wjw.
Obviously, the answer can be expressed as A/B, (A, B are relatively prime), and Q is not divisible by 1000000007. Please output the result of A(B^-1) mod 1000000007, (B^-1) is the reciprocal modulo of B multiplication 1000000007

Input

Multiple sets of data, the first row contains a positive integer T (T<=10),
and each row has two positive integers N, M (0<=n, |M|<=100000), as the title suggests

Output

Output results for each group of N and M

Sample Input

2
2 -2
0 0

Sample Output

562500004
1

【analysis】

If you want to take i steps from 0 to m, then you have to return to im steps and stay in place at the end n+m-2*i steps

So the answer is c[n][i]*c[ni][im]*A*B*C

A, B, and C respectively represent the probability of taking i step, im step, n+m-2*i step C[n][i] = n!/i!/(ni)!

Then enumerate all the possibilities of i, and then remember the inverse element... nothing else

【Code】

#include<algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include  <stdio.h>
#include   <math.h>
#include   <time.h>
#include   <vector>
#include   <bitset>
#include    <queue>
#include      <set>
#include      <map>
using namespace std;
 
typedef long long LL;
const int Mod=1e9+7,N=200005;
 
int Pow(int a,int b)
{
    int c=1;
    while(b)
    {
        if(b&1)
            c=c*(LL)a%Mod;
        a=a*(LL)a%Mod;b>>=1;
    }
    return c;
}
 
int fac[N];
 
 
int C(int n,int m)
{
    if(m<0||m>n)
        return 0;
    return fac[n]*(LL)Pow(fac[m],Mod-2)%Mod*Pow(fac[n-m],Mod-2)%Mod;
}
 
void solve()
{
    int t,p;scanf("%d%d",&t,&p);
    if(p<0)
        p=-p;
    cout<<C(2*t,t-p)*(LL)Pow(Mod+1>>1,2*t)%Mod<<endl;
}
 
int main()
{
// #ifndef ONLINE_JUDGE
    //freopen("5.in","r",stdin);
    //freopen("5.out","w",stdout);
// #endif
    fac[0]=1;
    for(int i=1;i<N;i++)
        fac[i]=fac[i-1]*(LL)i%Mod;
    int T;cin>>T;
    while(T--)
        solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/jnxxhzz/article/details/80691698