CC

自然万物都趋向从有序变得无序

0%

Coin 2017 西安网络赛 快速幂 + 二项式定理

题目链接

题目


Bob has a not even coin, every time he tosses the coin, the probability that the coin’s front face up is qp(qp12)qp(qp12)pq(pq21).qp(qp≤12)\frac{q}{p}(\frac{q}{p} \le \frac{1}{2})​p​​q​​(​p​​q​​≤​2​​1​​).
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\frac{X}{Y}​​​, because the answer could be extremely large, you only need to print (XY1)mod(109+7)(XY1)mod(109+7)(XY1)mod(109+7).(X∗Y^{−1})mod(10^9+7)(X * Y^{-1}) \mod (10^9+7)(X∗Y​−1​​)mod(10​^9​​+7).

Input Format

First line an integer T, indicates the number of test cases$ (T≤100T \le 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≤10​7​​) 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 次, 求正面朝上次数为偶数次的概率。

思路:

计算方式类似于二项分布的概率的计算, 可以得出$ \sum _{i = 0}^{k} C(k, i) * (\frac{q}{p} )^i * (1 - \frac{q}{p} )^{k-i}, i 是奇数,, 观察式子我们发现分母岁固定的 : p ^ k$, 分子是二项式的偶数项, 至于计算二项式的偶数项就很简单了 即是, qk+(p2q)k2\frac{q ^k + (p - 2 * q) ^k}{2},最后就是除法逆元了,

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
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;
}