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

题意:一枚硬币,投一次正面朝上概率为q/p, 求投k次,正面朝上的次数为偶数次的概率X/Y,输出 (XY1)mod(109

+7)


思路:

2017 ACM-ICPC 亚洲区(西安赛区)网络赛 B. Coin(概率)_第1张图片


代码:

#include
using namespace std;
const int mod = 1e9+7;
typedef long long ll;
ll p, q, n;

ll qmod(ll x, ll q)
{
    ll res = 1;
    while(q)
    {
        if(q%2) res = res*x%mod;
        x = x*x%mod;
        q /= 2;
    }
    return res;
}

int main(void)
{
    int _;
    cin >> _;
    while(_--)
    {
        scanf("%lld%lld%lld", &p, &q, &n);
        ll ni2 = qmod(2LL, mod-2);
        ll fm = qmod(p, n);
        ll fz = qmod(p-2*q, n);
        ll nifm = qmod(fm, mod-2);
        ll tmp = (1+fz*nifm%mod)%mod*ni2%mod;
        printf("%lld\n", tmp);
    }
    return 0;
}


你可能感兴趣的:(数学)