2017 ACM-ICPC 亞洲區(西安賽區)網絡賽 B.Coin(基本概率+二項式展開)

傳送門

Bob has a not even coin, every time he tosses the coin, the probability that the coin’s front face up is qp(qp12) .

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 XY , because the answer could be extremely large, you only need to print (XY1)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

題目大意:

Bob有一個不公平的硬幣,正反的概率是不一樣的,現在給出這個硬幣朝上的概率爲 pq ,現在求拋 k 次之後正面朝上的概率是多少,輸出的是 XY1 ,就是求分母關於MOD 的逆元。

解題思路:
根據概率所學的知識,很容易想到的是分母爲 fenmu=pk ,分子爲:fenzi=ki=0Cikqi(pq)ki(i)
根據二項式定理有:
現在有 (q+pq)k=ki=0Cikqi(pq)ki(1)
設有: (q(pq))k=ki=0Cikqi(1)ki(pq)ki(2)
那麼我們發現:
k 是奇數的時候 (1)(2)=fenzi
k 是偶數的時候 (1)+(2)=fenzi
顯然分子可求,最後在乘以分母關於 mod 的逆元就OK了。

代碼:

#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e6+5;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 1e9+7;
LL Pow(LL a, LL b){
    LL ans = 1;
    while(b){
        if(b & 1) ans = ans * a % MOD;
        b>>=1;
        a = a * a % MOD;
    }
    return ans;
}
void Exgcd(LL a, LL b, LL &x, LL &y){
    if(b == 0){
        x = 1;
        y = 0;
        return ;
    }
    LL x1, y1;
    Exgcd(b, a%b, x1, y1);
    x = y1;
    y = x1-(a/b)*y1;
}
int main(){
    //freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
    //freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
    int T; scanf("%d", &T);
    while(T--){
        LL p, q, k; scanf("%lld%lld%lld",&p,&q,&k);
        LL tmp1 = Pow(2*q-p, k);
        LL tmp2 = Pow(p, k);
        int flag = 1;
        if(k & 1) flag = -1;
        LL fenzi = (tmp2+flag*tmp1)%MOD*500000004LL%MOD;
        LL x, y;
        Exgcd(p, MOD, x, y);
        x = (x%MOD+MOD)%MOD;
        LL ans = Pow(x, k)*fenzi%MOD;
        printf("%lld\n",ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章