2017 ACM-ICPC 亞洲區(西安賽區)網絡賽 B Coin(逆元,費馬小定理)

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})pq(pq21).

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}YX, because the answer could be extremely large, you only need to print (X * Y^{-1}) \mod (10^9+7)(XY1)mod(109+7).

Input Format

First line an integer TT, indicates the number of test cases (T \le 100T100).

Then Each line has 33 integer p,q,k(1\le p,q,k \le 10^7)p,q,k(1p,q,k107) 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 亞洲區(西安賽區)網絡賽

題意:拋k次硬幣,求正面朝上爲偶次數的概率,結果餘1e9+7
思路:設向上概率概率是 a 向下爲 b
k次扔取時,向上爲偶數的爲: C(k,0) *a^0 * b^k  + C(k,2) *a^2 *b^(k-2)... + C(k,k)*a^k*b^0
所以結果應爲: ((a+b)^k +(a-b)^k )/2
又a+b=1,a=p/q,所以應爲(((p-2q)/p)^k+1)/2
關於除法逆元有(a / b) % p = (a * inv(a) ) % p = (a % p * inv(a) % p) % p
上代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 10;
const double pi = acos(-1);
#define ll long long
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
const ll mod = 1e9 + 7;
ll q_mod(ll a, ll b)
{
	ll res = 1;
	while (b)
	{
		if (b & 1)
			res = res*a%mod;
		a = a*a%mod;
		b >>= 1;
	}
	return res;
}
int main()
{
	//freopen("Text.txt", "r", stdin);
	int t;
	ll p, q, k;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%lld%lld%lld", &p, &q, &k);
		ll ans = (p - 2 * q)*q_mod(p, mod - 2) % mod;//除法變乘法
		ans = q_mod(ans, k);
		ans = (ans + 1) % mod;
		ans = ans*q_mod(2, mod - 2) % mod;
		printf("%lld\n", ans);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章