CodeForces711E ZS and The Birthday Paradox 费马小定理求逆元

来源:互联网 发布:激战2选阿苏拉捏脸数据 编辑:程序博客网 时间:2024/06/04 22:58

ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland.
In Udayland, there are 2n days in a year. ZS the Coder wants to interview k people from Udayland, each of them has birthday in one of 2n days (each day with equal probability). He is interested in the probability of at least two of them have the birthday at the same day.
ZS the Coder knows that the answer can be written as an irreducible fraction . He wants to find the values of A and B (he does not like to deal with floating point numbers). Can you help him?
Input
The first and only line of the input contains two integers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 1018), meaning that there are 2n days in a year and that ZS the Coder wants to interview exactly k people.
Output
If the probability of at least two k people having the same birthday in 2n days long year equals  (A ≥ 0,B ≥ 1, ), print the A and B in a single line.
Since these numbers may be too large, print them modulo 106 + 3. Note that A and B must be coprimebefore their remainders modulo 106 + 3 are taken.

Examples
input
3 2
output
1 8
input
1 3
output
1 1
input
4 3
output
23 128
Note

In the first sample case, there are 23 = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly , so A = 1, B = 8.
In the second sample case, there are only 21 = 2 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the probability is 1 and A = B = 1.


题意是求一年有 2^n 天,k 个人出现两人生日相同的可能性是多少。

那么,只需考虑所有人生日都各不相同的情况就好了;

当 k > 2^n , 概率为1;

否则,为 1-A( 2^n , k )/((2^n)^k);

题中要求分子分母互质,而分母为 2 的幂 ,那么两者的GCD 也为 2 的幂。

容易知道,A( 2^n , k ) 为 k的相邻数的乘积,并且第一个数为 2^n ,  那么后面的一系列数是否为2的倍数则仅仅与他们为第几个数有关,也就是 2^n 后所减去的数。

这样我们就能得到两者的GCD了,由于分子分母均需要除以他们的GCD ,并且分子分母原来的形式比除去GCD后 更容易求解,那么,用逆元的方式则显得很有必要。

而该题中,mod 为一素数,并且GCD为2的幂,与之互素,那么可以用费马小定理来求解。

最后值得一提的一点是,在求分子对mod 的模时,为一系列连续自然数的乘积,当这些数个数较多时,便容易超时,加一句 判断 当前数是否被mod 整除的语句,则可以很好的改善这一问题。


#include <iostream>#include <cstdio>#include <map>#include <set>#include <vector>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstring>#include <string>using namespace std;#define INF 0x3f3f3f3ftypedef long long LL;const int mod=1000003;LL quickpowmod(LL x,LL y,LL mo){    LL ret=1;    while(y){        if(y&1)            ret=ret*x%mo;        x=x*x%mo;        y>>=1;    }    return ret;}int main(){std :: ios :: sync_with_stdio (false);    LL n,k,ans1,ans2;    while(cin>>n>>k){        if(n<=60&&k>(1ll<<n)){            printf("1 1\n");            continue;        }        LL i=0;        LL j=k-1;        while(j){            j/=2;            i+=j;        }        ans1=1;        LL s=quickpowmod(2,n,mod);        LL t=s;        for(int i=1;i<=k-1;i++){            ans1=(ans1*(--s))%mod;            if(!ans1) break;        }        LL ss=quickpowmod(quickpowmod(2,i,mod),mod-2,mod);        ans1=(ans1*ss)%mod;        ans2=quickpowmod(t,(k-1),mod);        ans2=(ans2*ss)%mod;        ans1=(ans2-ans1+mod)%mod;        cout<<ans1<<" "<<ans2<<endl;    }    return 0;}


0 0
原创粉丝点击