您的位置:首页 > 理论基础 > 计算机网络

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

2017-09-16 21:49 288 查看
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})​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}​Y​​X​​,
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(10​9​​+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≤10​7​​)
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.csdn.net/postedit

样例输入

2
2 1 1
3 1 2


样例输出

500000004

555555560

B正确通过2017-09-16 16:15:531ms236kBc++
推一下概率。

设一次向上的概率为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;
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm-icpc 网络 亚洲 c++
相关文章推荐