2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin

题意:抛k次硬币,硬币正面朝上的概率是q/p,问出现偶数次正面朝上的概率是多少,答案取模1e9+7
思路:经过数学推导最后得到 (p^k+(p-2*q)^k)/(2*p^k) 之后取模1e9+7,用到了快速幂和逆元
#include <iostream>
#include <cstddef>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>
#define S second
#define F frist
using namespace std;
typedef long long ll;
const ll mod=1000000007;

ll pow_mod(ll a,ll b)   // 快速幂取模
{
    ll ans=1;
    while (b)
    {
        if (b&1) ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
ll ext_gcd(ll a, ll b, ll &x, ll &y)    //扩展欧几里得
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll d = ext_gcd(b, a % b, x, y);
    ll tmp = x;
    x = y;
    y = tmp-a/b*y;
    return d;
}
ll inv(ll a, ll mod)  {              // 逆元计算
    ll x, y;
    ext_gcd(a, mod, x, y);
    return (x % mod + mod) % mod;

}
int main()
{
    ll p,q,k;
    int t;
    cin >> t;
    while(t--)
    {
        cin >> p >> q >> k;
        q=pow_mod(p-2*q,k);
        p=pow_mod(p,k);
        q=(p+q)%mod;
        p=(p*2)%mod;
        cout << (inv(p,mod)*q)%mod << endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值