Coin 2017 西安网络赛 快速幂 + 二项式定理

题目链接


Bob has a not even coin, every time he tosses the coin, the probability that the coin’s front face up is qp(qp12)qp(qp12)pq(pq21).

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 ​​, because the answer could be extremely large, you only need to print (XY1)mod(109+7)(XY1)mod(109+7)(XY1)mod(109+7).

Input Format

First line an integer T, indicates the number of test cases (T100T100T100).

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≤10​7​​) indicates the i-th test case.

Output Format

For each test case, print an integer in a single line indicates the answer.


题意:

给你一枚不均匀的硬币, 正面朝上的概率是 q / p;现在扔 k 次, 求正面朝上次数为偶数次的概率。

思路:

计算方式类似于二项分布的概率的计算, 可以得出 ki=0C(k,i)(qp)i(1qp)kii , 观察式子我们发现分母岁固定的 : pk , 分子是二项式的偶数项, 至于计算二项式的偶数项就很简单了 即是, qk+(p2q)k2 ,最后就是除法逆元了,

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;

typedef long long LL;
const int maxn = 1;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

LL qpow(LL x, LL n){
    x %= mod;
    LL ans = 1;
    while(n){
        if(n&1) ans = ans * x % mod;
        x = x * x % mod;
        n >>= 1;
    }return ans;
}

int main(){
    int t, k, p, q;
    scanf("%d", &t);
    while(t--){
        scanf("%d %d %d", &p, &q, &k);
        LL t = qpow(p, k);
        LL tt = (qpow(p, k) + qpow( p - 2 * q, k)) % mod;
        LL ans = tt * qpow(t * 2, mod -2) % mod;
        printf("%lld\n", (ans % mod +mod) % mod);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值