Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up isqp(qp≤12)\frac{q}{p}(\frac{q}{p} \le \frac{1}{2})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\frac{X}{Y}YX, because the answer could be extremely large, you only need to print (X∗Y−1)mod(109+7)(X * Y^{-1}) \mod (10^9+7)(X∗Y−1)mod(109+7).
Input Format
First line an integer TTT, 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≤107) indicates the i-th test case.
Output Format
For each test case, print an integer in a single line indicates the answer.http://write.blog.youkuaiyun.com/postedit
样例输入
2 2 1 1 3 1 2
样例输出
500000004
555555560
B | 正确通过 | 2017-09-16 16:15:53 | 1ms | 236kB | c++ |
推一下概率。
设一次向上的概率为x,向下的概率为y,x + y == 1
设A为向上n(n <=k,为偶数)次,则易知P(A) = C(k, n)x^n * y ^(k - n)
所以有 sigma(i = 0, k)= P(偶数次) + P(奇数次) = C(k, i)x^i * y ^(k - i) = (x + y)^ k = 1
为了得到偶数的概率,可以用-x替换上式的x
有sigma(i = 0, k) = C(k, i)(-x)^i * y ^(k - i) = (-x + y)^ k = (1 - 2 * x)^k
两式相加 = 1 + (1 - 2 * x)^k = 2 *P(偶数次)
所以 ans = (1 + (1 - 2 * x)^k) / 2
= (1 + (1 - 2 * q / p) ^ k) / 2
题目要求对1e9 + 7取模.除法用费马小定理求逆元(
因为 a ^ (p - 1) = 1 (mod p)
所以 a * a ^ (p - 2) = 1 (mod p)
所以此题a^(1e9 + 5)就是a的逆元
)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<map> #include<cstdlib> #include<string> #include<set> #include<stack> #define mod 1000000007 using namespace std; long long p, q, k; long long PowerMod(long long a, long long b) { long long c = mod; long long ans = 1; a = a % c; while(b) { if(b & 1) ans = (ans * a) % c; b >>= 1; a = (a * a) % c; } return ans; } int main() { int t; cin >> t; while(t--) { scanf("%lld %lld %lld", &p, &q, &k); long long ans = PowerMod(2, mod - 2); long long tmp = PowerMod(p, mod - 2); long long x = 1 + PowerMod(1 - 2 * q * tmp % mod, k); ans *= x; ans %= mod; cout << (ans + mod) % mod<< endl; } return 0; }