CodeForces-711E ZS and The Birthday Paradox(勒让德定理+抽屉原理)

E. ZS and The Birthday Paradox
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 of2n 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 ≥ 0B ≥ 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 coprime before their remainders modulo106 + 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 = 1B = 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.

传送门:http://codeforces.com/problemset/problem/711/E

这道题勒让德定理,费马小定理,逆元,GCD,抽屉原理应有尽有!!

勒让德定理,在正数n!的素因子标准分解式中,素数p的指数=∑[n/p^k](k>=1)

http://baike.baidu.com/link?url=xSo6p2xcu62FqIPBovuFUMXbe4u3JV8G9emkaDP5GpDCshL1Eal3aer-LRGo5SGGSM8FbxBUlJ2_zT5ArYHoHK

题解:①所有生日的不同组合有B=2^(kn)种,每个人的生日两两不相同的组合有A=(2^n)(2^n-1)(2^n-2)...(2^n-k+1).因此至少有两个人生日相同的组合有B-A种,答案就是(B-A)/B.②然后再是分子分母约分,我们可以发现B的素因数只有2,因此我们就要找出A的素因数2的指数,这个可以用勒让德定理求得,但是勒让德定理是要求A为阶乘,显然A不是阶乘,于是我们要把A化成阶乘的形式.我们可以发现对于x<=2^n,x中2的指数=(2^n-x)中2的指数,于是可以把A化成(k-1)!(注意A的第一项2^n可以和B约分).③A和B应该是约分后再取模运算,因此要先乘以他们的最大公因数的逆元.④然后根据费马小定理,求B的时候,先把B中2的指数与mod-1取模然后再用快速幂求,⑤最后我们要把A求出来,由于k很大,不能直接把A的每一项直接相乘,但是题目中的mod很小,根据抽屉原理,k个连续的数相乘,如果k>=mod,那么这k个数中必定有一个是mod的倍数,此时A%mod=0,因此我们可以把求A这一步的复杂度简化为O(mod),

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define FIN freopen("in.txt","r",stdin);
#define FOUT freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
typedef pair PII;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e6 + 3;
LL pow(LL a,LL b){
    LL ret=1;
    while(b){
        if(b&1) ret=ret*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ret;
}
int main(){
    LL n,k;
   // freopen("in.txt","r",stdin);
    scanf("%I64d%I64d",&n,&k);
    if(n=k) {flag=0;break;}
        }
        if(flag){
            printf("1 1\n");
            return 0;
        }
    }
    LL cnt=0,c=2ll;
    while(c<=k-1){
        cnt+=(k-1)/c;
        c*=2ll;
    }
    LL A,tmp=((n%(mod-1)*((k-1)%(mod-1))-cnt)%(mod-1)+(mod-1))%(mod-1);
    LL B=pow(2ll,tmp);
    if(k>mod) A=0;
    else{
        LL t=pow(2ll,n);
        A=pow(pow(2ll,cnt),mod-2);
        for(LL i=1;i


你可能感兴趣的:(初等数论)