C/C++知识点之C-Travel along the Line ZOJ - 4006题解
小标 2018-08-27 来源 : 阅读 943 评论 0

摘要:本文主要向大家介绍了C/C++知识点之C-Travel along the Line ZOJ - 4006题解,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之C-Travel along the Line ZOJ - 4006题解,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

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

题解: 
我们枚举向左移动的次数,那么很容易可以得到向右和保持不动的此处 
然后根据公式 
(nl)(n−lr)(14)l+r(12)s=(nl)(n−lr)(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();
    }
}    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程