Codeforces 711E 数论

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.



题意:给出2^n天,k个人,求至少有两个人同一天过生日的概率


题解:首先k>2^n  概率为1  判掉

至少有两个人同一天的概率  等价于  1-都不是一天的概率

1-(2^n-1)/(2^n)....(2^n-k+1)/(2^n)


gcd(a,b)=gcd(b-a,b)  b>a

所以gcd(2^n-t,2^n)=gcd(t,2^n)

(a-b)%m=(a%m-b%m)+m%m  a>b

所以根据这个公式  只要(2^n-i)中有一个是1000003的倍数就为0

如果没有的话  gcd(2^n-i,2^n)=gcd(i,2^n)  只要算出来每个i与2^n的公因子即可  因为分母只有2  所以只用找2就行了  记因子为t个2

有一个快速算的方法是  while(k/2)t+=k/2  k/=2   这样算的原理是  第一次算出k中有多少个是2的倍数  然后全体缩小1倍  循环下去

算出来分母  分母就是2^(nk)/(2^t) 等于 2^(nk)*inv(2^t)

分子就等于  分母-(2^n-1)*...*(2^n-k+1)*inv(2^t)


#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll mod=1000003;
ll a[70];
ll quick(ll a,ll k){//a^k%m 快速幂
	ll ans=1;
	while(k){
		if(k&1)ans=ans*a%mod;
		k/=2;
		a=a*a%mod;
	}
	return ans;
}
int main(){
	ll n,k,i;
	cin>>n>>k;
	a[0]=1;
	for(i=1;i<=63;i++)a[i]=a[i-1]*2;
	if(n<=63&&k>=a[n]+1){//如果k>2^n
		printf("1 1\n");
		return 0;
	}
	ll t=0,now;
	now=k-1;
	while(now>1){//计算t
		t+=now/2;
		now/=2;
	}
	ll mu;
	mu=quick(quick(2,n),k-1);//2^(n*k)
	mu=mu*quick(quick(2,t),mod-2)%mod;// inv(2^t)
	ll zi;
	zi=mu;//开始时候分子等于前面的  2^(n*k)/(2^t)
	ll s=1;
	for(i=-k+1;i<=-1;i++){
		if((-1+k)>=1000006){//如果这个区间有1000003的倍数  直接就为0
			s=0;
			break;
		}
		if(((quick(2,n)+i)%mod+mod)%mod==0){//算出当前的2^n+i mod m是否为0
			s=0;
			break;
		}
		s=s*((quick(2,n)+i)%mod+mod)%mod;//s=s*(2^n+i)
	}
	s=s*quick(quick(2,t),mod-2)%mod;//s=s/(2^t)
	zi=((zi-s)%mod+mod)%mod;//zi=zi-s
	printf("%lld %lld\n",zi,mu);
	return 0;
}


你可能感兴趣的:(每日一题,数论)