您的位置:首页 > 其它

Codeforces 711 E. ZS and The Birthday Paradox(数学)——Codeforces Round #369 (Div. 2)

2016-08-30 19:12 435 查看
[传送门](http://codeforces.com/contest/711/problem/E)

E. ZS and The Birthday Paradoxtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputZS 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 2n 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?
InputThe 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.
OutputIf the probability of at least two k people having the same birthday in 2n days long year equals

(A ≥ 0, B ≥ 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.
ExamplesInput
3 2
Output
1 8
Input
1 3
Output
1 1
Input
4 3
Output
23 128
NoteIn 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 = 1, B = 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.

题目大意:

一年有 2n(n≤1018) 天,然后有 k(k≤1018) 个小朋友,现在问你的是:这 k 个小朋友中至少有两个

小朋友生日相同的概率是多少,假设这个概率约分后为 pq,输出 p,q对 1000003 取模

解题思路:

首先我们来分析一下题目,因为题目中说 至少 两个小朋友生日相同的概率,那么我们从反面来考虑,那么也就是说所有

的小朋友的生日都不相同的概率是多少,那么我们这个可以列出一个式子:

A(2n,k)(2n)k——(1)

那么我来解释一下这个式子:首先因为有 k 个小朋友,所以有 k个生日,然后一个小朋友生日是某一天的概率是

12n,所以 k 个小朋友的生日的概率就是 1(2n)k,那么我们现在保证每个小朋友的生日都不是

相同的,所以就是从 2n 天中选 k 个,然后有 k 种排列,所以就是 A(2n,k),所以我们要做的就是化简 (1) 式,

那么我们通过观察可以得出结论:分子和分母的最大公约数一定是 2i,所以我们只要将 GCD 求出来了,然后分子分母分

别除以最大公约数就行了,因为分子的 2 肯定是比分母少的,所以只要求出分子中有多少 2 就行了,这个问题就是相当于

n! 后面有多少个 0,因为分子展开为:

A(2n,k) = (2n)∗(2n−1)∗(2n−2)∗ ... ∗(2n−k+1)

首先可以先将 2n 与 分母约分一下,然后找的是 (k−1)! 有多少个 2 ,每次除以 2,这个就不细说了,那么找到

GCD 以后,我们就进行计算,因为分母好操作就是 n∗(k−1) 注意用快速乘法小心爆 long long,然后就是分子了,其

实我们仔细观察一下的话,会发现如果 MOD < k 的 时候分子一定是 0,因为分子一直是 (2n−i) 这样的,剩下的暴

力做就行了,得到分子之后别忘记除以 GCD,这个地方就要通过逆元来实现了,还有需要注意的是当 2n<k 的时候 直

接输出 1 1 就行了 最后不要忘记 概率是 1−ans 。

My Code:

/**
2016 - 08 - 30 晚上
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 1e3+5;
const LL MOD = 1e6+3;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL Scan_LL()///输入外挂
{
LL res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
int Scan_Int()///输入外挂
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
}
void Out(LL a)///输出外挂
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
LL Multi(LL a, LL b)
{
LL ans = 0;
while(b)
{
if(b & 1)
ans = (ans+a)%(MOD-1);
b>>=1LL;
a = (a+a)%(MOD-1);
}
return ans;
}
LL quick(LL a, LL b)
{
LL ans = 1;
while(b)
{
if(b & 1)
ans = (ans*a) % MOD;
b>>=1LL;
a = (a*a) % MOD;
}
return ans;
}
void Exgcd(LL a, LL b, LL &x, LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
LL x1, y1;
Exgcd(b, a%b, x1, y1);
x = y1;
y = x1 - (a/b)*y1;
}
int main()
{
LL Inv, y;
Exgcd(2, MOD, Inv, y);
Inv = (Inv%MOD+MOD)%MOD;
LL n, k;
while(cin>>n>>k)
{
if(n < 63LL)
{
if(k > (1LL<<n))
puts("1 1");
else
{
LL ans = 0, fm = k, tmp;
fm--;
while(fm)
{
fm>>=1LL;
ans += fm;
}
fm = k-1LL;
fm = Multi(n, fm);
fm = (fm-ans)%(MOD-1);
fm = (fm%(MOD-1)+(MOD-1))%(MOD-1);
fm = quick(2LL, fm);
if(k > MOD)
{
cout<<fm<<" "<<fm<<endl;
continue;
}
tmp = quick(2LL, n);
LL fz = 1;
for(LL i=1; i<k; i++)
fz = fz*(tmp-i)%MOD;
fz = (fz%MOD+MOD)%MOD;
fz = fz*quick(Inv, ans)%MOD;
fz = fm - fz;
fz = (fz%MOD+MOD)%MOD;
cout<<fz<<" "<<fm<<endl;
}
}
else
{
LL ans = 0, fm = k, tmp;
fm--;
while(fm)
{
fm>>=1LL;
ans += fm;
}
/** 2^ans 是最大公约数 **/
fm = k-1LL;
fm = Multi(n, fm);
fm = (fm-ans)%(MOD-1);
fm = (fm%(MOD-1)+(MOD-1))%(MOD-1);
fm = quick(2LL, fm);
if(k > MOD)
{
cout<<fm<<" "<<fm<<endl;
continue;
}
tmp = quick(2LL, n);
LL fz = 1;
for(LL i=1; i<k; i++)
fz = fz*(tmp-i)%MOD;
fz = fz*quick(Inv, ans)%MOD;
fz = fm - fz;
fz = (fz%MOD+MOD)%MOD;
cout<<fz<<" "<<fm<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: