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

探讨了使用不均匀硬币进行多次翻转后,正面朝上次数为偶数的概率计算方法。通过数学推导给出了一种高效的计算方案,并利用费马小定理解决了大数取模的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bob has a not even coin, every time he tosses the coin, the probability that the coin's front face up isqp(qp≤12)\frac{q}{p}(\frac{q}{p} \le \frac{1}{2})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\frac{X}{Y}YX, because the answer could be extremely large, you only need to print (X∗Y−1)mod(109+7)(X * Y^{-1}) \mod (10^9+7)(XY1)mod(109+7).

Input Format

First line an integer TTT, indicates the number of test cases (T≤100T \le 100T100).

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(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.http://write.blog.youkuaiyun.com/postedit

样例输入
2
2 1 1
3 1 2
样例输出
500000004
555555560

B正确通过2017-09-16 16:15:531ms236kBc++

推一下概率。

设一次向上的概率为x,向下的概率为y,x + y == 1

设A为向上n(n <=k,为偶数)次,则易知P(A) = C(k, n)x^n * y ^(k - n)

所以有  sigma(i = 0, k)= P(偶数次) + P(奇数次) = C(k, i)x^i * y ^(k - i) = (x + y)^ k = 1

为了得到偶数的概率,可以用-x替换上式的x

有sigma(i = 0, k) = C(k, i)(-x)^i * y ^(k - i) = (-x + y)^ k = (1 - 2 * x)^k

 两式相加 = 1 + (1 - 2 * x)^k = 2 *P(偶数次)

 所以 ans = (1 + (1 - 2 * x)^k) / 2

                = (1 + (1 - 2 * q / p) ^ k) / 2

题目要求对1e9 + 7取模.除法用费马小定理求逆元(

因为 a ^ (p - 1) = 1 (mod p)

所以 a * a ^ (p - 2) = 1 (mod p)

所以此题a^(1e9 + 5)就是a的逆元


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<cstdlib>
#include<string>
#include<set>
#include<stack>
#define mod 1000000007

using namespace std;

long long p, q, k;
long long  PowerMod(long long  a, long long  b)
{
    long long c = mod;
    long long  ans = 1;
    a = a % c;
    while(b)
    {
        if(b & 1)
            ans = (ans * a) % c;
        b >>= 1;
        a = (a * a) % c;
    }
    return ans;
}

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        scanf("%lld %lld %lld", &p, &q, &k);
            long long ans = PowerMod(2, mod - 2);
            long long tmp = PowerMod(p, mod - 2);
            long long x = 1 + PowerMod(1 - 2 * q * tmp % mod, k);
            ans *= x;
            ans %= mod;
            cout << (ans + mod) % mod<< endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值