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})pq(pq≤21).
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 XYfrac{X}{Y}YX, 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(109+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(1le p,q,k le 10^7)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.
样例输入
1
2
32 2 1 1 3 1 2
样例输出
1
2500000004 555555560
题目来源
2017 ACM-ICPC 亚洲区(西安赛区)网络赛
题意:
计算扔k次硬币,正面朝上的次数为偶数的概率。再将答案mod(1e9+7)
解析:
正面朝上概率为a,反面朝上概率为b
a+b=1;
ans=C(k,0)*a^0*b^k+C(k,2)*a^2*b^(k-2)+...
(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
(b-a)^k= C(k,0) *(-a)^0 *b^k +C(k,1)*(-a)* b^(k-1) .........+C(k,k)*(-a)^k*b^0
可得ans=((a+b)^k+(b-a)^k)/2 再将a=p/q,b=(1-p/q)带入
得((p^k)+(p-2q)^k)/(2*p^k)
除法的模运算就是求乘法逆元
x*(x^-1)=1(mod p) 乘法逆元性质
d=(x/y)(mod p) => y*d=x(mod p) => y*y^-1*d=x*y^-1(mod p) => d=x*y^-1(mod p)
求乘法逆元的三种方法
http://blog.csdn.net/rain722/article/details/53170288
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const long long int N = 1e9+7; long long p,q,k; long long zi,mu; long long quickmulti(long long a,long long b) { long long ans=1; while(b) { if(b&1) ans = ((ans%N)*(a%N)) % N; a = ((a%N)*(a%N)) % N; b=b>>1; } return ans; } long long inv2(long long b) { return quickmulti(b,N-2); } int main() { int t; long long x,y,d,x0; scanf("%d",&t); while(t--) { scanf("%lld%lld%lld",&p,&q,&k); zi=(quickmulti(p,k)+quickmulti(p-2*q,k))%N; mu=(2*quickmulti(p,k))%N; long long ans=(zi*inv2(mu))%N; printf("%lldn",ans); } return 0; }
最后
以上就是悦耳咖啡最近收集整理的关于计蒜客 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B coin(求乘法逆元)的全部内容,更多相关计蒜客内容请搜索靠谱客的其他文章。
发表评论 取消回复