Codeforces #369 (Div. 2) E. 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 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 = 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.

题解:规定有2^n天,有k个小朋友,问你这些小朋友在这2^n天,至少有两个小朋友的生日在同一天的概率是多少。特判:如果人数多于天数,就一定有至少2个人是同一天的。

如果所有人的生日都不同。那么第一个人的可能是2^n/2^n,第二个人是(2^n-1)/2^n...第k个人是(2^n-(k-1))/2^n.

全部人的生日都不在同一天的概率为:

那么容斥一下,答案就是1- 。然后就是考虑对这个式子进行约分。观察式子可以发现分子分母的最大公约数肯定是2的幂次方。因为gcd(2^n-x,2^n)=gcd(x,2^n),也是说2^n-x和x的素因子标准分解式中素数2的指数相同。同理:素数2的指数个数与中的(k−1)!的指数相同。

勒让德定理(点我)可以求出(K−1)!的素因子标准分解式素数2的指数,然后可以求出分子分母的gcd。当a与p互斥时,a^(p-2)=1/a mod p 或 a^(p-1)=1 mod p。(求逆元)

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
#include <iomanip>
#include <list>
#include <stack>
#include <utility> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
const double eps = 1e-8;  
const int INF = 1e9+7; 
const ll inf =(1LL<<62) ;
const ll MOD = 1e6 + 3;  
const ll mod = (1LL<<32);
const int N =110; 
const int M=100010;
const int maxn=1001;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)
#define in freopen("in.txt","r",stdin) 
#define rep(i,j,k) for (int i = j; i <= k; i++)  
#define per(i,j,k) for (int i = j; i >= k; i--)  
#define lson x << 1, l, mid  
#define rson x << 1 | 1, mid + 1, r  
const int lowbit(int x) { return x&-x; }
//const int lowbit(int x) { return ((x)&((x)^((x)-1))); } 
int read(){ int v = 0, f = 1;char c =getchar();
while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}
while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
return v*f;}
char a[1010][6];
int q_mod(ll a,ll b)    
{  
    ll ans=1;  
    while(b>0)  
    {  
        if(b&1)    
            ans=(ans*a)%MOD;           
        a=(a*a)%MOD;  
        b>>=1;
    }  
    return ans;  
}  
int main()
{
  ll n, k;
  cin>>n>>k;
  if(n<=62&&k>1ll<<n)
  {
    puts("1 1"); 
	return 0;
  }
    ll num=0; 
	for(ll i=k-1;i;i>>=1)
	{
		num+=i/2;
	}	  
    ll b=1;
	ll a=q_mod(2,n);
    for(ll i=1;i<=k-1;i++)
	{
        ll tmp=(a-i+MOD)%MOD;
        b=b*tmp%MOD;
        if(!tmp)break;
    }
	ll ans=q_mod( q_mod(2,num), MOD-2 );
    a=q_mod(a,k-1);
    a=a*ans%MOD; 
	b=b*ans%MOD;
    b=(a-b+MOD)%MOD;
    printf("%I64d %I64d\n",b,a);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值