题目链接
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)qp(qp≤12)pq(pq≤21).
The question is, when Bob tosses the coin kkk 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)(X∗Y−1)mod(109+7)(X∗Y−1)mod(109+7).
Input Format
First line an integer T, indicates the number of test cases (T≤100T≤100T≤100).
Then Each line has 333 integer p,q,k(1≤p,q,k≤107)p,q,k(1\le p,q,k \le 10^7)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.
题意:
给你一枚不均匀的硬币, 正面朝上的概率是 q / p;现在扔 k 次, 求正面朝上次数为偶数次的概率。
思路:
计算方式类似于二项分布的概率的计算, 可以得出 ∑ki=0C(k,i)∗(qp)i∗(1−qp)k−i,i是奇数 , 观察式子我们发现分母岁固定的 : pk , 分子是二项式的偶数项, 至于计算二项式的偶数项就很简单了 即是, qk+(p−2∗q)k2 ,最后就是除法逆元了,
代码:
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int maxn = 1;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
LL qpow(LL x, LL n){
x %= mod;
LL ans = 1;
while(n){
if(n&1) ans = ans * x % mod;
x = x * x % mod;
n >>= 1;
}return ans;
}
int main(){
int t, k, p, q;
scanf("%d", &t);
while(t--){
scanf("%d %d %d", &p, &q, &k);
LL t = qpow(p, k);
LL tt = (qpow(p, k) + qpow( p - 2 * q, k)) % mod;
LL ans = tt * qpow(t * 2, mod -2) % mod;
printf("%lld\n", (ans % mod +mod) % mod);
}
return 0;
}