Bob has a not even coin, every time he tosses the coin, the probability that the coin’s front face up is
The question is, when Bob tosses the coin
If the answer is
Input Format
First line an integer
Then Each line has
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
題目大意:
Bob有一個不公平的硬幣,正反的概率是不一樣的,現在給出這個硬幣朝上的概率爲
解題思路:
根據概率所學的知識,很容易想到的是分母爲
根據二項式定理有:
現在有
設有:
那麼我們發現:
當
當
顯然分子可求,最後在乘以分母關於
代碼:
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e6+5;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 1e9+7;
LL Pow(LL a, LL b){
LL ans = 1;
while(b){
if(b & 1) ans = ans * a % MOD;
b>>=1;
a = a * a % MOD;
}
return ans;
}
void Exgcd(LL a, LL b, LL &x, LL &y){
if(b == 0){
x = 1;
y = 0;
return ;
}
LL x1, y1;
Exgcd(b, a%b, x1, y1);
x = y1;
y = x1-(a/b)*y1;
}
int main(){
//freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
//freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
int T; scanf("%d", &T);
while(T--){
LL p, q, k; scanf("%lld%lld%lld",&p,&q,&k);
LL tmp1 = Pow(2*q-p, k);
LL tmp2 = Pow(p, k);
int flag = 1;
if(k & 1) flag = -1;
LL fenzi = (tmp2+flag*tmp1)%MOD*500000004LL%MOD;
LL x, y;
Exgcd(p, MOD, x, y);
x = (x%MOD+MOD)%MOD;
LL ans = Pow(x, k)*fenzi%MOD;
printf("%lld\n",ans);
}
return 0;
}