ICPC2017网络赛(西安)B coin (概率计算)

Bob has a not even coin, every time he tosses the coin, the probability that the coin’s front face up is qp (qp≤12)
The question is, when Bob tosses the coin kk times, what’s the probability that the frequency of the coin facing up is even number.

If the answer is XY​​ , because the answer could be extremely large, you only need to print(X∗Y−1)mod(109+7).

Input Format

First line an integer T, indicates the number of test cases (T≤100).

Then Each line has 3 integer p,q,k(1≤p,q,k≤107) indicates the i-th test case.

Output Format

For each test case, print an integer in a single line indicates the answer.

样例输入

2
2 1 1
3 1 2
样例输出

500000004
555555560

题目题意:题目给了一种特殊的硬币,其中正面朝上的概率是q/p,我们丢硬币k次,问硬币朝上的次数是偶数的概率为多少 (要 mod 1e9+7)(*逆元)

https://www.jisuanke.com/contest/876/44177
B题,队友用矩阵快速幂的推的公式做的..
还有别人的做法http://blog.csdn.net/winter2121/article/details/78005036?locationNum=10&fps=1
用的是普通的数学技巧..

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<set>

using namespace std;

const int maxn = 20;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll mod= 1e9+7;

mat mul(mat& A,mat& B){
    mat C(2,vec(2));
    for(int i=0;i<2;i++)
        for(int k=0;k<2;k++)
            for(int j=0;j<2;j++){
            C[i][j]=(C[i][j]+A[i][k]*B[k][j])%mod;
    }
    return C;
}

ll Pow(ll a,ll b){
    ll sum=1;
    while(b){
        if(b&1) sum=sum*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return sum;
}

mat Pow(mat A,ll b){
    mat B(2,vec(2));
    B[0][0]=B[1][1]=1;
    while(b){
        if(b&1) B=mul(A,B);
        b>>=1;
        A=mul(A,A);
    }
    return B;
}

ll inv(ll x){
    return Pow(x,mod-2);    
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,k;
        scanf("%lld%lld%lld",&p,&q,&k);
        ll a=q;
        ll b=p-q;
        mat A(2,vec(2));
        A[0][1]=A[1][0]=a;
        A[0][0]=A[1][1]=b;
        A=Pow(A,k-1);
        ll Ans=(a*A[1][0]+b*A[1][1])%mod;
        ll cnt=Pow(p,k);
        Ans=(Ans*inv(cnt))%mod;
        printf("%lld\n",Ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值