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;

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值