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 of 2ndays (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.

首先是求概率,考慮第一個人的生日是1,第二個人與第一個人不同的概率是1*(2^n-1/2^n)可以得出有k個人時不同的概率是P=A(2^n,k)/2^(n*k),相同的概率是1-P; 

先考慮展P的展開得到分子=(2^n-1)(2^n-2)..(2^n-k+1),分母爲2^(n-1)*k,因爲當k>=MOD時分子整除MOD所以只需要求分母乘上gcd(分子,分母)的逆元后的數值,當k<MOD時分子分母需要同時乘上gcd(分子,分母)的逆元.

之後問題就轉化爲如何求gcd了,當k<MOD時可以通過暴力。

當k>=MOD時可以通過勒讓德定理(Lr(n!)=sum[n/2^p])求得

之後便是注意當k>2^n時,由於抽屜原理,要輸出1 1

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long
#define F(x,a,b) for (ll x=a;x<=b;x++)
#define MOD 1000003
ll _fast(ll k){
    if (k==0) return 1;if (k&1){return _fast(k-1)*2%MOD;}
    else {ll t=_fast(k/2);return t*t%MOD;}
}
bool jud(ll n,ll k){
    ll t=1;
    while(n)
    {
        t*=2;
        if (t>=k) return 0;
        n--;

    }
    return 1;
}
int main()
{
    ll n,k;
    cin>>n>>k;
    ll N=_fast(n);
    if (jud(n,k)) {printf("1 1");return 0;}
    if (k<MOD)
    {
      ll cnt=0;
      F(i,1,k-1)
      {
          ll t=i;
          while ((t&1)==0)
          {
              t=t>>1;
              cnt++;
          }
      }
        ll gcd=MOD-cnt-1;
        ll ans=1;
        F(i,1,k-1)
        {
            ans=ans*(N-i+MOD)%MOD;
        }
        ans=ans*_fast(gcd)%MOD;
        ll mo=_fast(n%(MOD-1)*(k-1)%(MOD-1))*_fast(gcd)%MOD;
        cout<<(mo-ans+MOD)%MOD<<" "<<mo<<endl;
    }
    else
    {

       ll t=2;ll cnt2=0;
       while (t<=k-1)
       {
           cnt2+=(k-1)/t;
           t=t*2;
       }
       ll gcd=MOD-cnt2%(MOD-1)-1;
       ll xx=n%(MOD-1)*((k-1)%(MOD-1));
       ll mo=_fast(xx)%MOD*(_fast(gcd)%MOD)%MOD;
       cout<<mo<<" "<<mo<<endl;

    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章