cf711E ZS and The Birthday Paradox
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 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?
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.
If 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.
3 2
1 8
1 3
1 1
4 3
23 128
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 = 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.
想要知道答案是啥还是很容易的,用高中知识即可知道1 - { A(2^n,k) / 2^(nk) } 即是所求,A(a,b)是排列数。
问题是要先化简再上下同时取模
可以先证明如果有大数A、B,假设(A%mod)/(B%mod)==p/q,那么1-A/B=(B-A)/B=(q-p)/q。(在模mod意义下)
所以只要知道A(2^n,k)/2^(nk),就能知道答案了
先化简。
分式下面只有2,所以gcd=2^t,t不知道,但是显然t是由上面A(2^n,k)决定。
考虑(2^n)(2^n-1)...(2^n-k+1)有多少个因子2。如果把2^n单独考虑,剩下(2^n-1)...(2^n-k+1)的2的因子数跟1~k-1的2的因子数一样多。因为任取个(2^n-s),它能和s对应
所以因子数就是n+(k-1)/2+(k-1)/4+...+(k-1)/2^p,除到(k-1)/2^p=0为止
得到了gcd=2^t的t之后,只要上下同时乘(2关于mod的逆元)乘t次,就完成了化简。
然后注意到当k>=mod时,(2^n)(2^n-1)(2^n-mod+1)%mod==0,由鸽巢原理这是显然的。
所以当k<mod,A(2^n,k)%mod暴力算,k>=mod,A(2^n,k)%mod==0。
而2^(nk)%mod是容易算的。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
#define mod 1000003
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL n,k;
LL rev_2;
LL bit[];
inline LL quickpow(LL a,LL b,LL MOD)
{
LL s=;
while (b)
{
if (b&)s=(s*a)%MOD;
a=(a*a)%MOD;
b>>=;
}
return s;
}
int main()
{
rev_2=quickpow(,mod-,mod);bit[]=;for (int i=;i<;i++)bit[i]=bit[i-]<<;
n=read();k=read();
if (n<=&&k>bit[n]){puts("1 1");return ;}
if (k==){puts("0 1");return ;}
LL mx=quickpow(,n,mod),now=mx;
LL ans1=,ans2=;
LL te=k-,sum=n;
while (te)
{
sum+=te/;
te>>=;
}
LL sv=k;
for (LL i=;i<=min(sv,(LL)mod);i++)
{
k--;
ans2=(ans2*mx)%mod;
ans1=(ans1*now)%mod;
now--;if (!now)now+=mod;
}
while (k%(mod-)!=)ans2=(ans2*mx)%mod,k--;
ans1=(ans1*quickpow(rev_2,sum,mod))%mod;
ans2=(ans2*quickpow(rev_2,sum,mod))%mod;
printf("%lld %lld\n",(ans2-ans1+mod)%mod,ans2);
//ans=1-{ (2^n*(2^n-1)*(2^n-2)*...*(2^n-k+1))/(2^n)^k }
}
cf 711E
cf711E ZS and The Birthday Paradox的更多相关文章
- CF369E. ZS and The Birthday Paradox
/* cf369E. ZS and The Birthday Paradox http://codeforces.com/contest/711/problem/E 抽屉原理+快速幂+逆元+勒让德定理 ...
- codeforces 711E E. ZS and The Birthday Paradox(数学+概率)
题目链接: E. ZS and The Birthday Paradox. time limit per test 2 seconds memory limit per test 256 megaby ...
- ZS and The Birthday Paradox
ZS and The Birthday Paradox 题目链接:http://codeforces.com/contest/711/problem/E 数学题(Legendre's formula) ...
- Codeforces 711E ZS and The Birthday Paradox 数学
ZS and The Birthday Paradox 感觉里面有好多技巧.. #include<bits/stdc++.h> #define LL long long #define f ...
- Codeforces Round #369 (Div. 2) E. ZS and The Birthday Paradox 数学
E. ZS and The Birthday Paradox 题目连接: http://www.codeforces.com/contest/711/problem/E Description ZS ...
- 【Codeforces711E】ZS and The Birthday Paradox [数论]
ZS and The Birthday Paradox Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample ...
- Codeforces 711E ZS and The Birthday Paradox
传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st ...
- 【28.57%】【codeforces 711E】ZS and The Birthday Paradox
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 711E. ZS and The Birthday Paradox 概率
已知一年365天找23个人有2个人在同一天生日的概率 > 50% 给出n,k ,表示现在一年有2^n天,找k个人,有2个人在同一天生日的概率,求出来的概率是a/b形式,化到最简形式,由于a,b可 ...
随机推荐
- codevs 1277 生活大爆炸 2012年CCC加拿大高中生信息学奥赛
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description Sheldon and Leonard are physicists wh ...
- 51nod 算法马拉松17 解题报告 以后不能赛中写题解(查逐梦者抄袭本人代码...
B题(数学题: 问(1+sqrt(2)) ^n 能否分解成 sqrt(m) +sqrt(m-1)的形式 如果可以 输出 m%1e9+7 否则 输出no n<=1e18 刚看题没思路 暴力一下 ...
- 兼容IE6\7\8浏览器的html5标签的几个方案
html5大行其道的时代已经到来.如果你还在等待浏览器兼容,说明你已经与web脱节几条街了.当然,这得益于移动客户端的蓬勃发展.如果还在纠结于,是否应该掌握html5和css3技术时,请狠狠的抽自己几 ...
- linx vim 文件操作 ubuntu server 软件源
mv /etc/danted.conf /etc/danted.conf.bak sudo wget https://files.cnblogs.com/files/marklove/danted.t ...
- C++模板 · 为什么要引入模板机制?
刚学过类模板时,很不理解,甚至觉得这简直没有用,在自己骗自己嘛!明明很方便的东西,偏偏要加个类模板来回折腾.可能因为我们刚开始写的程序很简单,有时候,可能程序复杂一点,对理解一些概念更有帮助. 今天在 ...
- LuoguP1351 联合权值 (枚举)
题目链接 枚举每个点,遍历和他相邻的点,然后答案一边更新就可以了. 最大值的时候一定是两个最大值相乘,一边遍历一边记录就好了. 时间复杂度.\(O(n)\) #include <iostream ...
- Linux rm删除文件未释放空间问题分析
问题描述: 在自己的虚拟机上做实验时出现空间不足情况,检查发现之前的kafka集群测试日志在几天写了 25G,于是进入 /data/kafka01/logs 目录执行 “rm -rf *” 删除所有测 ...
- MySQL 上移/下移/置顶
在编写网站系统时,难免会用到上移.下移.置顶的功能,今天小编就介绍一下我的思路. 首先,需要一张数据表: CREATE TABLE `a` ( `id` ) NOT NULL AUTO_INCREME ...
- 使Linux支持exFAT和NTFS格式的磁盘
Linux支持exFAT和NTFS Linux系统默认可以自动识别到fat32格式的盘,但fat32支持的文件不能大于4G,所以只能将移动硬盘和U盘格式化为NTFS和exFAT这两种格式的,对于U盘最 ...
- perl:split函数用法
本文和大家重点讨论一下Perl split函数的用法,Perl中的一个非常有用的函数是Perl split函数-把字符串进行分割并把分割后的结果放入数组中.这个Perl split函数使用规则表达式( ...