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的更多相关文章

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

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

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

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

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

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

  5. CF369E. ZS and The Birthday Paradox

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

  6. Codeforces 711E ZS and The Birthday Paradox

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

  7. cf711E ZS and The Birthday Paradox

    ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that g ...

  8. 【28.57%】【codeforces 711E】ZS and The Birthday Paradox

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. codeforces 711E. ZS and The Birthday Paradox 概率

    已知一年365天找23个人有2个人在同一天生日的概率 > 50% 给出n,k ,表示现在一年有2^n天,找k个人,有2个人在同一天生日的概率,求出来的概率是a/b形式,化到最简形式,由于a,b可 ...

随机推荐

  1. 009-程序集路径Web窗体

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...

  2. iOS 电商购物车倒计时时间计算

    /** * 倒计时 * * @param endTime 截止的时间戳 * * @return 返回的剩余时间 */ - (NSString*)remainingTimeMethodAction:(l ...

  3. [Q]“获取AutoCAD安装信息时失败...”解决方法

    “获取AutoCAD安装信息时失败...”解决方法:在“setup.exe”上右键,以管理员权限运行即可.

  4. [SOJ] Babelfish

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  5. Leetcode-37-Sudoku Solver(Hard)

    此处先留空 使用搜索和回溯,递归来实现 参考:http://blog.csdn.net/zxzxy1988/article/details/8586289 描述简介,代码量最少

  6. PNG文件转png8

    png8比普通png图片会小很多,所以在开发中为了是图片加载速度更快我们可以把Png图片都转成png8 首先添加  ImageProcessor  包 private byte[] ConvertTo ...

  7. CoreJavaE10V1P3.5 第3章 Java的基本编程结构-3.5 操作符

    最基本的操作为赋值操作,= 即赋值操作符 基本的算术操作为加.减.乘.除取模.除取余数,其对应操作符为 +.-.*./.% 算术操作与赋值操作联合衍生为:+=:-=:*=:/=:%=: 由于处理器硬件 ...

  8. ASP.NET中重复表格列合并的实现方法(转自脚本之家)

    这几天做一个项目有用到表格显示数据的地方,客户要求重复的数据列需要合并,就总结了一下.NET控件GridView 和 Repeater 关于重复数据合并的方法. 这是合并之前的效果: 合并之后的效果图 ...

  9. zabbix 布署实践【4 服务器自动探索发现,并且自动关联模版】

    使用管理员登录zabbix后,在配置---自动发现--创建发现规则 如下:我们的需求是监听办公网内openstack的所有虚拟机,在其VM创建后,自动加到zabbix监控中来,并自动关联监控模版 可以 ...

  10. php error _report

    [error_reporting] => Array   (   [global_value] => 32767   [local_value] => 0   [access] =& ...