ICPC2017网络赛(西安)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 \frac{q}{p}(\frac{q}{p} \le \frac{1}{2})pq(pq21).

The question is, when Bob tosses the coin 

kktimes, 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 亚洲区(西安赛区)网络赛

【解析】:

概率计算,但最终结果不用除法,而用逆元。

只算分子即可,分母就是p的k次幂

分子的计算


【代码】:

#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll qpow(ll n,ll m){n%=mod;ll ans=1;while(m){if(m%2)
		ans=(ans*n)%mod;m/=2;n=(n*n)%mod;}return ans;}
ll inv(ll b){
	return b==1?1:(mod-mod/b)*inv(mod%b)%mod;
}
int main()
{
	ll t,p,q,k;
	cin>>t;
	while(t--)
	{
		scanf("%lld%lld%lld",&p,&q,&k);
		ll zi=qpow(p,k)+qpow(p-2*q,k);//分子
		zi=zi%mod*inv(2)%mod;
		ll mu=qpow(p,k)%mod;//分母
		
		ll ans=zi*inv(mu)%mod;
		printf("%lld\n",ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值