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

来源:互联网 发布:根号在c语言中怎么表示 编辑:程序博客网 时间:2024/06/15 19:06

Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up isp/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 integerp,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.

样例输入

22 1 13 1 2

样例输出

500000004555555560


组合数学题

附大佬的推导结论:


#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;}


阅读全文
0 0
原创粉丝点击