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

2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B Coin (概率计算)

2017-10-05 13:59 405 查看
传送门:  https://nanti.jisuanke.com/t/17115

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 亚洲区(西安赛区)网络赛

题目的意思很简单,就是让你扔硬币,给你p,q.向上的概率为q/p;然后问

你向上次数为偶数的概率。

这个地方用到高中组合知识:(组合数学,概率方面的问题)

设 向上概率概率是 a  向下为b  则  a+b =1;

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

2) +C(k,4)*a^4 *b^(k-4) ......... + C(k,k)*a^k*b^0

(a+b)^k   展开为  : C(k,0)*a^0 *b^k,+ C(k,1)*a^1*b^(k-1)

 +C(k,2)*a^2*b^(k-2) .......+C(k,k)*a^k*b^0

(a-b) ^k 展开为:  C(k,0) *a^0 *(-b)^k +C(k,1)^a* (-b)^(k-1)

 .........+C(k,k)*a^k*(-b^0)

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

题意 要求逆元:

求逆元方法;:(三种)

ll inv_exgcd(ll a,ll n){lld,x,y;ex_gcd(a,n,d,x,y);return d==1?

(x+n)%n:-1;}

ll inv1(ll b){returnb==1?1:(MOD-MOD/b)*inv1(MOD%

b)%MOD;}

ll inv2(ll b){return qpow(b,MOD-2);}

求逆元 应用 费马小定理的那个 即为:

ll inv2(ll b){return qpow(b,MOD-2);}

若用 带/ 法的 求逆元  则会出现 /0 的情况 WA

#include<stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#define LL long long
#define INf 0x3f3f3f3f
const int MOD=1e9+7;
using namespace std;
LL qpow(LL x,LL n)
{
LL res=1;
for(;n;n>>=1)
{
if(n&1)
res=(res*x)%MOD;
x=(x*x)%MOD;
}
return res;
}
LL inv2(LL b)
{
return qpow(b,MOD-2);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
LL p,q,k;
scanf("%lld %lld %lld",&p,&q,&k);
LL a=qpow(p,k);
LL sum=(a+qpow((p-2*q),k))%MOD;;
sum=sum*(inv2(2*a)%MOD);
printf("%lld\n",sum%MOD);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: