C - Travel along the Line ZOJ - 4006

本文介绍了一道名为C-TravelalongtheLine的编程题目(ZOJ-4006),该题涉及概率计算。文章详细解释了如何通过枚举向左移动的次数来计算达到特定位置的概率,并提供了具体的数学公式和C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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

题解:
我们枚举向左移动的次数,那么很容易可以得到向右和保持不动的此处
然后根据公式

(nl)(nlr)(14)l+r(12)s=(nl)(nlr)(12)2(l+r)+s(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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值