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可 ...
随机推荐
- C语言之分支结构 if(二)
If的第三种和第四种形式(tips:也是比较常用的形式) 3).if语句第三种形式: 简单来说就是任意的if或者else里面还可以嵌套任意的if-else语句 语法: if(表达式){ if(表达式2 ...
- MyBatis 框架笔记
Mybatis 框架笔记 ------技术源于热爱! 获取更多内容请关注小编的个人微信公众平台 1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jd ...
- 【算法专题】工欲善其事必先利其器—— C++ STL中vector(向量/不定长数组)的常用方法总结
#include<iostream> #include<cstdio> #include<string> #include<vector>//不定长数组 ...
- Python第一天——入门Python(2)字符串的简单操作
数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...
- IONIC之简易购物车
HTML <div ng-app="app"> <div class="l-header"> <div class="l ...
- [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2
Just like passing in an array to *ngFor, you can pass in any value into your structural directive s ...
- BHuman文档结构
Chapter 2 : a short introduction how to build the code including the required software and how to ru ...
- Idea高级用法
一.快速打开Action: Ctrl+Shift+A 列表中会列出所有的action,对应于idea的各种操作,例如: 输入backgroud,可以为编辑器设置背景 输入restclient,可以打开 ...
- python自动化开发-2
1.python的数据类型之列表 列表是Python开发语言中最常见的数据类型之一,通过列表可以实现对数据的增删改等常用操作. 列表的定义:例子 names = ["Lucy",& ...
- angularjs的懒加载
1.angularJS懒加载依赖模块 //设置 .config [ '$ocLazyLoadProvider' ($ocLazyLoadProvider) -> # We configure o ...