传送门: 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).
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.
For each test case, print an integer in a single line indicates the answer.
2 2 1 1 3 1 2
500000004 555555560
题目的意思很简单,就是让你扔硬币,给你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;
}