已知一年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. ASP.NET MVC搭建项目后台UI框架—5、Demo演示Controller和View的交互

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  2. 源映射(Source Map)详解

    一.什么是源映射 为了提高性能,很多站点都会先压缩 JavaScript 代码然后上线, 但如果代码运行时出现错误,浏览器只会显示在已压缩的代码中的位置,很难确定真正的源码错误位置. 这时源映射就登场 ...

  3. Javascript中addEventListener和attachEvent的区别

    在利用javascript为DOM Element添加事件处理程序时,如果要想下兼容IE6,7时,就不得不考虑addEventListener与attachEvent的异同. 1.首先说下addEve ...

  4. 几款开源的hybird移动app框架分析

    几款开源的Hybrid移动app框架分析 Ionic Onsen UI 与 ionic 相比 jQuery Mobile Mobile Angular UI 结论 很多移动开发者喜欢使用原生代码开发, ...

  5. CSS3与页面布局学习笔记(五)——Web Font与CSS Sprites(又称CSS精灵、雪碧图)技术

    一.web font web font是应用在web中的一种字体技术,在CSS中使用font-face定义新的字体.先了解操作系统中的字体: a).安装好操作系统后,会默认安装一些字体,这些字体文件描 ...

  6. 移动端UC /QQ 浏览器的部分私有Meta 属性

    <meta name="format-detection" content ="telephone=no"/>   格式检测 禁止识别我们页面中的数 ...

  7. (转)SQL 优化原则

    一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...

  8. position: fixed用在iframe里面失效了

    iframe真是各种坑啊,,,可是找不到别的代替 $(parent.window).scroll(function(){ $('固定元素').css({ top : $(parent.window). ...

  9. arcgis for flex或silverlight全国地图天气预报的实现

    系统架构是B/S,目前有两个不同的版本,1.开发语言是C#.silverlight,开发平台是.NET:2.开发语言是java.flex,开发平台是myeclise. 采用地图是ArcGIS全国地图, ...

  10. iOS 开发学习资料整理(持续更新)

      “如果说我看得比别人远些,那是因为我站在巨人们的肩膀上.” ---牛顿   iOS及Mac开源项目和学习资料[超级全面] http://www.kancloud.cn/digest/ios-mac ...