已知一年365天找23个人有2个人在同一天生日的概率 > 50%

给出n,k ,表示现在一年有2^n天,找k个人,有2个人在同一天生日的概率,求出来的概率是a/b形式,化到最简形式,由于a,b可能非常大,对a,b分别%(10^6+3)

注意,这道题是先化到最简,再分别取模

首先,特判 k > 2^n 时,a = 1,b = 1

没有2个人同一天生日的概率为:

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

所以a,b化简之前分别是:

a = 2nk-n - (2n - 1) * (2n - 2) * ... * (2n - k + 1)

b = 2nk-n

化简,就是要求gcd(a,b)

可以肯定,gcd(a,b)肯定是2d这种形式

由于k < 2n,d实际上就等于(k-1)!分解素因子后2的个数

如果k >= P,化简取模后有a = b,(但是取模后不能再继续化简为a = b = 1)

如果k < P,log(k)求得d,则:

b = 2nk-n-d,快速幂可求,由于n * k会超过long long,可以先 % phi(P),欧拉定理

a = 2nk-n-d - (2n - 1) * (2n - 2) * ... * (2n - k + 1) / 2d

后面这个可以先暴力O(k)求,再乘以2d的逆元

注意 a = (a + P) % P防止a < 0 的情况

代码:

  //File Name: cf711E.cpp
//Created Time: 2017年01月06日 星期五 00时05分30秒 #include <bits/stdc++.h>
#define LL long long
using namespace std;
const int P = (int)1e6 + ;
LL qp(LL x,LL y){
LL res = ;
for(;y>;y>>=){
if(y & ) res = res * x % P;
x = x * x % P;
}
return res;
}
LL cal_phi(LL n){
LL res = n;
for(int i=;i*i<=n;++i){
if(n % i == ){
res -= res / i;
while(n % i == )
n /= i;
}
}
if(n > ) res -= res / n;
return res;
}
void solve(LL n,LL k,LL &a,LL &b){
LL now = ;
for(LL j=;j<=n;++j){
now *= ;
if(now >= k) break;
}
if(now < k) a = ,b = ;
else{
// puts("fffff");
LL d = ;
now = k - ;
while(now >= ){
d += now / ;
now /= ;
}
LL phi = P - ;
b = qp(,((n % phi) * (k % phi) - n % phi - d % phi + * phi) % phi);
if(k >= P) a = b;
else{
now = qp(,n % phi);
a = ;
for(LL i=;i<k;++i)
a = (now - i + P) % P * a % P;
now = qp(,d % phi);
a = a * qp(now,P - ) % P;
a = (b - a + P) % P;
}
}
}
int main(){
LL n,k;
cin >> n >> k;
LL a,b;
solve(n,k,a,b);
printf("%lld %lld\n",a,b);
return ;
}

codeforces 711E. ZS and The Birthday Paradox 概率的更多相关文章

  1. Codeforces 711E ZS and The Birthday Paradox 数学

    ZS and The Birthday Paradox 感觉里面有好多技巧.. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Codeforces 711E ZS and The Birthday Paradox

    传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st ...

  3. Codeforces 711E ZS and The Birthday Paradox(乘法逆元)

    [题目链接] http://codeforces.com/problemset/problem/711/E [题目大意] 假设一年有2^n天,问k个小朋友中有两个小朋友生日相同的概率. 假设该概率约分 ...

  4. 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 ...

  5. 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 ...

  6. ZS and The Birthday Paradox

    ZS and The Birthday Paradox 题目链接:http://codeforces.com/contest/711/problem/E 数学题(Legendre's formula) ...

  7. CF369E. ZS and The Birthday Paradox

    /* cf369E. ZS and The Birthday Paradox http://codeforces.com/contest/711/problem/E 抽屉原理+快速幂+逆元+勒让德定理 ...

  8. Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

    除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...

  9. 【Codeforces711E】ZS and The Birthday Paradox [数论]

    ZS and The Birthday Paradox Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample ...

随机推荐

  1. entityframework学习笔记--005-给code first一个正确的解释

    在微软官方关于ef7的介绍中强调,ef7将舍弃database first.model first,只保留code first的使用.这引起了很多人的担忧,担忧源自对code first的错误理解.因 ...

  2. node-inspector调试工具应用

    一.起因 想调试某些开源的nodejs项目,然后就选择了node-inspector插件. 他的优点: 1.可以借用chrome或firefox浏览器进行调试,与前端调试融合. 2.配置简单 二.必备 ...

  3. js事件处理、事件对象

    事件类型分类: 1 添加在html结构中的事件 <div id="div1" onclick="alert('append click event in html' ...

  4. jquery定时滑出可最小化的底部提示层

    效果预览:http://keleyi.com/keleyi/phtml/jqtexiao/index.htm当打开页面或者刷新页面后等待两秒钟,会在底部滑出可最小化的提示层.滑出层半透明,可关闭再现. ...

  5. js for循环中i++ 和 ++i有什么区别?

    平时都是这样写的for循环, for(var i = 0; i < 20 ; i++){ .... } 但我看有的人这样写 for (var i = 0; i < 20 ; ++i) { ...

  6. 轻松掌握:JavaScript组合模式

    组合模式 组合模式:将一组对象组合成树形结构,并统一对待组合对象和叶对象,忽略它们之间的不同(因为叶对象也可以也可以包含叶对象而成为组合对象),组合模式中的对象只能是一对多的关系,不能出现多对一. 基 ...

  7. elasticsearch GIS空间查询问题解决

    在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询     我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...

  8. fragment 监听返回

    @Override public void onResume() { super.onResume(); getView().setFocusableInTouchMode(true); getVie ...

  9. IOS 多线程分类以及多线程的相关操作

    直接附上援助链接:http://www.cnblogs.com/kenshincui/p/3983982.html 分享内容还关联到了生产者与消费者模式(其实看明白了整片文章,也就理解了生产者与消费者 ...

  10. [AlwaysOn Availability Groups]AlwaysOn等待类型

    AlwaysOn等待类型 当排查AlwaysOn延迟,等待统计信息可以在DMV中查看累计的AlwaysOn等待类型. 查看AlwaysOn等待类型 SELECT * FROM sys.dm_os_wa ...