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

来源:互联网 发布:淘宝双11怎么抢购 编辑:程序博客网 时间:2024/05/16 03:54

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;}
阅读全文
0 0
原创粉丝点击