ZS and The Birthday Paradox
ZS and The Birthday Paradox
题目链接:http://codeforces.com/contest/711/problem/E
数学题(Legendre's formula)
这题是以生日悖论(如果有23个或23个以上的人,那么至少有两个人的生日相同的概率要大于50%)为背景的数学题。公式本身不难推,主要是对大数的处理。
首先,我们需要找到分子和分母的公因数,这里用到了Legendre's formula(举个例子:10!的因数中质数3的个数为[10/3+10/(3^2)])。因为若2^n-x与2^n有公因数,那么x与2^n有相同的公因数,所以求(2^n)(2^n-1)(2^n-2)*...*(2^n-k+1)与(2^n)^k的公因数,就转化为了求(k-1)!与(2^n)^k的公因数。
之后就是分别求分子和分母:
对于分母来说,只需要用快速幂(也可以用费马小定理)就可以很容易的求出,再乘上公因数的逆元即可;
而对于分子来说,稍微有点麻烦:
1.如果k>=M,即(2^n)(2^n-1)(2^n-2)*...*(2^n-k+1)中至少有连续的M个整数,
那么(2^n)(2^n-1)(2^n-2)*...*(2^n-k+1)一定为M的倍数,所以它被M求模后余0;
2.如果k<M,因为M很小,所以枚举一下,就可以求出分子,再乘上公因数的逆元即可。
总的时间复杂度为O(M+lgk+lgn)
(感谢游少半夜教我证明Orz)
证明:(A/B)modM=(A*(BmodM)')modM,其中B'为B在M下的逆元
令B=b1*b2*b3*...*bn,则((BmodM)')modM
=((b1*b2*b3*...*bn mod M)')modM
=(b1modM*b2modM*...*bn mod M)'modM
=(b1*b2*...*bn)modM*(b1modM*b2modM*...*bn mod M)'modM*(b1*b2*...*bn)'modM
=(b1modM*b2modM*...*bn mod M)*(b1modM*b2modM*...*bn mod M)'modM*(b1*b2*...*bn)'modM
=1*(b1*b2*...*bn)'modM=(b1*b2*...*bn)'modM
=b1*b1'modM*b2*b2'modM...bn*bn'mod M*(b1*b2*...*bn)'modM
=(b1*b2*...*bn)modM*(b1'modM*b2'modM...bn'mod M)*(b1*b2*...*bn)'modM
=b1'modM*b2'modM...bn'mod M
代码如下:
#include<cstdio>
#include<iostream>
#define M (long long)(1e6+3)
using namespace std;
typedef long long LL;
LL n,k,cnt,molecular,numerator,x,y;
LL exGCD(LL a,LL b){
if(b==){
x=,y=;
return a;
}
LL r=exGCD(b,a%b);
LL temp=x;
x=y;
y=temp-(a/b)*y;
return r;
}
LL mod(LL a,LL b){
LL base=a,temp=;
while(b){
if(b&)temp=(temp*base)%M;
base=(base*base)%M;
b>>=;
}
return temp;
}
int main(void){
cin>>n>>k;
if(n<=&&k>(((LL))<<n)){//总人数大于总天数
cout<<""<<" "<<""<<endl;
return ;
}
LL temp=;
while(k->=temp){//根据Legendre's formula,求出(k-1)!的因数中质数2的个数
cnt+=((k-)/temp);
temp<<=;
}
if(cnt){//若有公因数,则求出公因数的逆元
LL gcd=mod(,cnt);
exGCD(gcd,M);
x=(x+M)%M;
}else x=;//若没有公因数,则令x=1,来消除对后面计算的影响
temp=mod(,n);//计算2^n
numerator=(mod(temp,k-)*x)%M;//计算(2^n)^(k-1)
if(k>=M)molecular=;//若分子出现连续的M个整数,则分子一定为M的倍数
else{
molecular=;
for(LL i=;i<k;++i){
molecular=((temp-i+M)%M*molecular)%M;//计算分子
}
molecular=(molecular*x)%M;//除以公因数
}
molecular=(numerator-molecular+M)%M;
cout<<molecular<<" "<<numerator<<endl;
}
ZS and The Birthday Paradox的更多相关文章
- 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 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 ...
- CF369E. ZS and The Birthday Paradox
/* cf369E. ZS and The Birthday Paradox http://codeforces.com/contest/711/problem/E 抽屉原理+快速幂+逆元+勒让德定理 ...
- Codeforces 711E ZS and The Birthday Paradox
传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st ...
- cf711E ZS and The Birthday Paradox
ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that g ...
- 【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可 ...
随机推荐
- 采用apicloud开发移动端项目心得体会
作为第一批吃螃蟹的,来说一说apicloud做移动端项目的一些体会. 刚开始接到项目,需要移动端开发两个项目,公司人员里面没有一个会原生android或者ios开发的,目前能出的技术也就是web,后端 ...
- Java实践 — SSH远程执行Shell脚本(转)
原文地址:http://www.open-open.com/lib/view/open1384351384024.html 1. SSH简介 SSH是Secure Shell的缩写,一 ...
- PWA - 渐进式网络应用初认识
Progressive Web Apps 简称PWA,是一种接近原生用户体验的渐进增强的web-app.从浏览器演进而来,沉浸式的体验,改进web的性能低下等.是Google 在2015年提出,今年才 ...
- 数据持久层框架iBatis, Hibernate 与 JPA 比较
在本文中我们介绍并比较两种最流行的开源持久框架:iBATIS和Hibernate,我们还会讨论到Java Persistence API(JPA).我们介绍每种解决方案并讨论其所规定的品质,以及在广泛 ...
- express 4.x 文件上传
1.安装文件上传模块: npm install multiparty --save 2.在routes/index.js 中添加: // 引用模块 let multiparty = require(& ...
- python3中str的函数
# 转换# 'capitalize',# 'lower',# 'upper',# 'casefold',# 'swapcase', 大小写互换# 'title',# 'strip',# 'rstrip ...
- unity3d打开对话框
最近一直在忙项目,没时间更新博客,这两天趁空封装windows下的打开对话框,支持多选.其他系统可以用ngui或者ugui封装一个. 这里就不上封装的源码了提供dll供小伙伴们使用,如果有需要源码请请 ...
- FTP上传下载工具(FlashFXP) v5.5.0 中文版
软件名称: FTP上传下载工具(FlashFXP) 软件语言: 简体中文 授权方式: 免费试用 运行环境: Win 32位/64位 软件大小: 7.4MB 图片预览: 软件简介: FlashFXP 是 ...
- CodeForces 670B Game of Robots
简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...
- [转]Mac常用软件推荐
https://github.com/hzlzh/Best-App