Coin(2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B)

Problem Description

Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up is \frac{q}{p}(\frac{q}{p} \le \frac{1}{2})).

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 \frac{X}{Y}​, because the answer could be extremely large, you only need to print (X * Y^{-1}) \mod (10^9+7).

Input

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

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

Output

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

​​​​​​​Sample Input

2
2 1 1
3 1 2

​​​​​​​Sample Output

500000004
555555560

题意:t 组数据,每组给出 p、q、k 三个数,现已知抛出一个硬币正面朝上的概率为 q/p,问抛 k 次硬币后,正面朝上为偶数次的概率,要求对答案模 1E9+7

思路:

设 a=q/p 为每次抛硬币正面朝上的概率,则 b=1-a 为每次抛硬币反面朝上的概率

那么出现偶数次的概率为:Y=C_k^0b^k+C_k^2a^2b^{k-2}+...+C_k^ka^k

即 (b+ax)^k 展开式与 (b-ax)^k 中 x 的偶次方的系数,那么出现偶数次的概率为:Y=\frac{(a+b)^k+(b-a)^k}{2}=\frac{1+(1-2a)^k}{2}

因此,出现奇数次的概率为:1-Y=\frac{1-(1-2a)^k}{2}

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); } // (a/b)%MOD=(a%MOD * getInv(b)%MOD)%MOD
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        LL p,q,k;
        scanf("%lld%lld%lld",&p,&q,&k);
        LL invP=getInv(p,MOD); // p的逆元
        LL inv2=getInv(2,MOD); // 2 的逆元

        LL res=(q%MOD*invP%MOD)%MOD; // q/p;
        res=2*res%MOD; // 2q/p
        res=(1-res%MOD+MOD)%MOD; // 1-2q/p
        res=quickModPow(res,k,MOD); //(1-2q/p)^k
        res=(1+res%MOD)%MOD; // 1+(1-2q/p)^k
        res=(res%MOD*inv2%MOD)%MOD; // (1+(1-2q/p)^k)/2
        printf("%lld\n",res);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值