2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin
2017-09-16 22:14
309 查看
Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up is
qp(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 XY\frac{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).
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(1\le p,q,k \le 10^7)p,q,k(1≤p,q,k≤107)
indicates the i-th test case.
题意:给你一枚特殊的硬币,这枚硬币朝上的概率是q/p,现在抛k次,问你偶数次朝上的概率是多少。
解题思路:二项分布偶数项的和公式,分数取余公式,两个一起用就得了~!
(a/b)%mod,等价于(a*b^(mod-2))%mod 这里要用逆元去求这个公式
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
typedef long long int ll;
ll power(ll a, ll b, ll c)
{ ll res = 1;
a %= c;
while (b)
{
if (b & 1)
res = (res * a) % c;
a = (a * a) % c;
b >>= 1;
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
ll p,q;
ll k;
ll mod=1000000007;
while(t--)
{
scanf("%lld%lld%lld",&p,&q,&k);
ll po=(1+power(((p-2*q)*power(p,mod-2,mod)%mod),k,mod))%mod*power(2,mod-2,mod)%mod;
printf("%lld\n",po);
}
return 0;
}
qp(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 XY\frac{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(1\le 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.样例输入
2 2 1 1 3 1 2
样例输出
500000004 555555560
题目来源
2017 ACM-ICPC 亚洲区(西安赛区)网络赛题意:给你一枚特殊的硬币,这枚硬币朝上的概率是q/p,现在抛k次,问你偶数次朝上的概率是多少。
解题思路:二项分布偶数项的和公式,分数取余公式,两个一起用就得了~!
(a/b)%mod,等价于(a*b^(mod-2))%mod 这里要用逆元去求这个公式
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
typedef long long int ll;
ll power(ll a, ll b, ll c)
{ ll res = 1;
a %= c;
while (b)
{
if (b & 1)
res = (res * a) % c;
a = (a * a) % c;
b >>= 1;
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
ll p,q;
ll k;
ll mod=1000000007;
while(t--)
{
scanf("%lld%lld%lld",&p,&q,&k);
ll po=(1+power(((p-2*q)*power(p,mod-2,mod)%mod),k,mod))%mod*power(2,mod-2,mod)%mod;
printf("%lld\n",po);
}
return 0;
}
相关文章推荐
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛b题Coin(矩阵快速幂)
- 计蒜客 17115 Coin(2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛: B. Coin
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B Coin(逆元,费马小定理)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B题 coin
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B.Coin(数学推公式)
- Coin (2017 ACM-ICPC 亚洲区(西安赛区)网络赛B题)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin(构造二项式定理 or dp矩阵优化)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin 二项式定理
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 - Coin(二项式展开)
- 计蒜客17115 Coin 数学 2017 ACM-ICPC 亚洲区(西安赛区)网络赛
- 【2017 ACM-ICPC 亚洲区(西安赛区)网络赛】 B. Coin
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛_Coin
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B.Coin(基本概率+二项式展开)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin(矩阵快速幂)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Coin (概率公式+快速幂)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B Coin (概率计算)
- B. Coin 数学/组合数 2017 ACM-ICPC 亚洲区(西安赛区)网络赛