2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B Coin (概率计算)
传送门: 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 pq(pq≤21).
The question is, when Bob tosses the coin k times, what's the probability that the frequency of the coin facing up is even number.
If the answer is YX, because the answer could be extremely large, you only need to print (X∗Y−1)mod(109+7).
Input Format
First line an integer T, indicates the number of test cases (T≤100).
Then Each line has 3 integer 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.
样例输入
2 2 1 1 3 1 2
样例输出
500000004 555555560
题目来源
题意: 扔 k次硬币 给你 一次硬币向上的 概率为p/q 让你求 k次硬币后 向上的次数为偶数的概率:
这个地方用到高中组合知识:
设 向上概率概率是 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 <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <math.h> #include <cstring> #include <string> #include <queue> #include <stack> #include <stdlib.h> #include <list> #include <map> #include <set> #include <bitset> #include <vector> #define mem(a,b) memset(a,b,sizeof(a)) #define findx(x) lower_bound(b+1,b+1+bn,x)-b #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w",stdout) #define S1(n) scanf("%d",&n) #define SL1(n) scanf("%I64d",&n) #define S2(n,m) scanf("%d%d",&n,&m) #define SL2(n,m) scanf("%I64d%I64d",&n,&m) #define Pr(n) printf("%d\n",n) using namespace std; typedef long long ll; const double PI=acos(-1); const int INF=0x3f3f3f3f; const double esp=1e-6; const int maxn=1e6+5; const int MOD=1e9+7; const int mod=1e9+7; int dir[5][2]={0,1,0,-1,1,0,-1,0}; 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; //cout<<5*inv1(8)<<endl; cin>>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; }
123