codeforces 711E. ZS and The Birthday Paradox 概率
已知一年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 概率的更多相关文章
- Codeforces 711E ZS and The Birthday Paradox 数学
ZS and The Birthday Paradox 感觉里面有好多技巧.. #include<bits/stdc++.h> #define LL long long #define f ...
- Codeforces 711E ZS and The Birthday Paradox
传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st ...
- Codeforces 711E ZS and The Birthday Paradox(乘法逆元)
[题目链接] http://codeforces.com/problemset/problem/711/E [题目大意] 假设一年有2^n天,问k个小朋友中有两个小朋友生日相同的概率. 假设该概率约分 ...
- 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 ...
- 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 ...
- ZS and The Birthday Paradox
ZS and The Birthday Paradox 题目链接:http://codeforces.com/contest/711/problem/E 数学题(Legendre's formula) ...
- CF369E. ZS and The Birthday Paradox
/* cf369E. ZS and The Birthday Paradox http://codeforces.com/contest/711/problem/E 抽屉原理+快速幂+逆元+勒让德定理 ...
- Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题
除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃 ...
- 【Codeforces711E】ZS and The Birthday Paradox [数论]
ZS and The Birthday Paradox Time Limit: 20 Sec Memory Limit: 512 MB Description Input Output Sample ...
随机推荐
- Java Enumeration接口
Enumeration接口定义 Enumeration接口与Iterator接口用法比较 一. 1.Enumeration接口定义 public interface Enumeration<E& ...
- B-Tree索引在sqlserver和mysql中的应用
在谈论数据库性能优化的时候,通常都会提到“索引”,但很多人其实并没有真正理解索引,也没有搞清楚索引为什么就能加快检索速度,以至于在实践中并不能很好的应用索引.事实上,索引是一种廉价而且十分有效的优化手 ...
- 软件公司为何要放弃MongoDB?
本文转至:http://database.51cto.com/art/201503/469510_all.htm(只作转载, 不代表本站和博主同意文中观点或证实文中信息) Olery成立于2010年, ...
- 设计模式-观察者模式(Observer Model)
文 / vincentzh 原文连接:http://www.cnblogs.com/vincentzh/p/6031844.html LZ刚刚开始心热的开启了博客之路,想着记点流水账,可帝都的天都冷成 ...
- JAVA中常说的三大框架指:SSH
即:spring.Struts.hibernate Spring:功能强大的组件粘合济,能够将你的所有的Java功能模块用配置文件的方式组合起来(还让你感觉不到spring的存在)成为一个完成的应用 ...
- 如何在Mac OSX系统下安装Tomcat
1. 下载Tomcat(地址:tomcat.apache.org),选择适合的版本(这里选择6.0.35),点击"Download",之后在新页面点击"Core下的&qu ...
- 动画在webapp中的现状
webapp的一大优势便是在view切换时候可以拥有媲美与native的动画效果,但是很多时候那只是一种想法,真正的情况却不是这样 产生此问题的原因有: ① 手机CPU烂! ② 手机显卡烂!就算四核其 ...
- 一步一步HTML5粒子编辑器
写在前面 大家阅读此文之前,可以先看一篇MiloYip的文章:用JavaScript玩转游戏物理(一)运动学模拟与粒子系统,看完之后再看此文,更加容易理解. MiloYip使用的粒子是canvas中绘 ...
- Synchronization Service Manager
You can use this UI Shell to check the User Profile log for the SharePoint. It's stored in this pa ...
- 不同环境下文件上传Uncaught SyntaxError: Unexpected end of input
很奇怪的问题,相同的代码和相同的数据,在两台linux服务器上执行文件上传,一台正常上传,一台在ftl页面 报:Uncaught SyntaxError: Unexpected end of inpu ...