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

探讨了当一枚不均匀硬币被抛掷k次时,正面朝上次数为偶数的概率计算问题,并提供了一段C++代码实现,用于解决此类问题。

Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up is p/q(p/q1/2).

The question is, when Bob tosses the coin k times, what's the probability that the frequency of the coin facing up is even number.

If the answer is Y/X, because the answer could be extremely large, you only need to print (XY^(−1))mod(109+7).

Input Format

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

Then Each line has 3 integer 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


组合数学题

附大佬的推导结论:


#include<bits/stdc++.h>
#include <ctime>
using namespace std;
typedef long long ll;
const int MAXN = 1 * 1e5 + 500;

const long long M = 1000000007;
long long quickpow(long long a, long long b)
{
    if (b < 0)
    {
        return 0;
    }
    long long ret = 1;
    a %= M;
    for (; b; b >>= 1, a = (a * a) % M)
        if (b & 1)
        {
            ret = (ret * a) % M;
        }
    return ret;
}
long long inv(long long a)
{
    return quickpow(a, M - 2);
}
int main()
{
    ///clock_t start_time = clock();

    ///clock_t end_time = clock();
    ///cout << "Running time is: " << static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
    std::ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
    {
        int p, q, k;
        cin >> p >> q >> k;
        ll ans = 0;
        ll Inv = quickpow(p, k);
        Inv = inv(Inv);
        ll hh = inv(2);

        ll a1 = quickpow(p, k);
        ll a2 = quickpow(2 * q - p, k);
        if (k % 2 == 1)
        {
            ans = (a1 * Inv % M - a2 * Inv % M + M) % M;
            ans = ans * hh % M;
        }
        else
        {
            ans = (a1 * Inv + a2 * Inv) % M;
            ans = ans * hh % M;
        }
        cout << ans << endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值