设为首页 加入收藏

TOP

C-Travel along the Line ZOJ - 4006题解
2018-04-24 06:06:25 】 浏览:321
Tags:C-Travel along the Line ZOJ 4006 题解

题目

C - 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 pisible 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

题解:

我们枚举向左移动的次数,那么很容易可以得到向右和保持不动的此处

然后根据公式

(nl)(n?lr)(14)l+r(12)s=(nl)(n?lr)(12)2(l+r)+s
    #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <queue>
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)

#define mset(var,val) memset(var,val,sizeof(var))

#define test(a) cout<<a<<endl
#define test2(a,b) cout<<a<<" "<<b<<endl

#define test3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
const int N= 2e5;
const int mod =1e9+7;
using namespace std;
typedef long long ll;
ll a[N+10];
ll b[N+10];
ll fac[N+10];
ll inv(ll a){
    if(a==1)return 1;
    return inv(mod%a)*(mod-mod/a)%mod;
}
ll C(ll n,ll m){
        ll ans = fac[n]*(inv(1ll*fac[m]*fac[n-m]%mod));
        return ans % mod ;
}
void init(){
        fac[0]=1;
        b[0]=1;
        for(int i =1;i<=N;i++){
                fac[i]=(fac[i-1]*i)%mod;
                b[i]=(b[i-1]*2ll)%mod;
        }
}

void work(){
        int n,y;
        scdd(n,y);
        long long ans=0;
        for(int i=0;i<=n;i++){
                int l = i;
                int r = y+i;
                int s = n-l-r;
                if(r<0||s<0||r>n||s>n)continue;
                ll son = C(n,l)*C(n-l,r)%mod;
                ll mon = inv(b[2*(l+r)+s]);
                ans =( ans + (1ll*son*mon))%mod;
        }
        printf("%lld\n",ans);
}
int main(){
    #ifdef local
        freopen("in.txt","r",stdin);
    #endif
    int t;
    init();
    scd(t);
    while(t--){
        work();
    }
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言怎么求得一个32位数的原码、.. 下一篇C语言进阶学习之struct与union分析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目