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

2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B Coin(逆元,费马小定理)

2017-09-30 18:27 375 查看
Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up is \frac{q}{p}(\frac{q}{p}
\le \frac{1}{2})​p​​q​​(​p​​q​​≤​2​​1​​).
The question is, when Bob tosses the coin kk times,
what's the probability that the frequency of the coin facing up is even number.

If the answer is \frac{X}{Y}​Y​​X​​,
because the answer could be extremely large, you only need to print (X
* Y^{-1}) \mod (10^9+7)(X∗Y​−1​​)mod(10​9​​+7).

Input Format

First line an integer TT,
indicates the number of test cases (T
\le 100T≤100).

Then Each line has 33 integer 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.

样例输入

2
2 1 1
3 1 2


样例输出

500000004
555555560



题目来源

2017
ACM-ICPC 亚洲区(西安赛区)网络赛
题意:抛k次硬币,求正面朝上为偶次数的概率,结果余1e9+7
思路:设向上概率概率是 a 向下为 b

k次扔取时,向上为偶数的为: C(k,0) *a^0 * b^k  + C(k,2) *a^2 *b^(k-2)... + C(k,k)*a^k*b^0

所以结果应为: ((a+b)^k +(a-b)^k )/2

又a+b=1,a=p/q,所以应为(((p-2q)/p)^k+1)/2

关于除法逆元有(a / b) % p = (a * inv(a) ) % p = (a % p * inv(a) % p) % p
上代码:
#include<iostream>
a9d4
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 10;
const double pi = acos(-1);
#define ll long long
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
const ll mod = 1e9 + 7;
ll q_mod(ll a, ll b)
{
ll res = 1;
while (b)
{
if (b & 1)
res = res*a%mod;
a = a*a%mod;
b >>= 1;
}
return res;
}
int main()
{
//freopen("Text.txt", "r", stdin);
int t;
ll p, q, k;
scanf("%d", &t);
while (t--)
{
scanf("%lld%lld%lld", &p, &q, &k);
ll ans = (p - 2 * q)*q_mod(p, mod - 2) % mod;//除法变乘法
ans = q_mod(ans, k);
ans = (ans + 1) % mod;
ans = ans*q_mod(2, mod - 2) % mod;
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  逆元 费马小定理
相关文章推荐