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

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

2017-09-16 22:14 309 查看
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)\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.

样例输入

2
2 1 1
3 1 2


样例输出

500000004
555555560


题目来源

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

题意:给你一枚特殊的硬币,这枚硬币朝上的概率是q/p,现在抛k次,问你偶数次朝上的概率是多少。

解题思路:二项分布偶数项的和公式,分数取余公式,两个一起用就得了~!

(a/b)%mod,等价于(a*b^(mod-2))%mod   这里要用逆元去求这个公式

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
typedef long long int ll;

ll power(ll a, ll b, ll c)
{ ll res = 1;
a %= c;
while (b)
{
if (b & 1)
res = (res * a) % c;
a = (a * a) % c;
b >>= 1;
}
return res;
}

int main()
{

int t;
scanf("%d",&t);

ll p,q;
ll k;

ll mod=1000000007;

while(t--)
{
scanf("%lld%lld%lld",&p,&q,&k);

ll po=(1+power(((p-2*q)*power(p,mod-2,mod)%mod),k,mod))%mod*power(2,mod-2,mod)%mod;

printf("%lld\n",po);

}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: