CF369E. ZS and The Birthday Paradox
/*
cf369E. ZS and The Birthday Paradox
http://codeforces.com/contest/711/problem/E
抽屉原理+快速幂+逆元+勒让德定理+费马小定理+欧拉定理+数论
题解:https://amoshyc.github.io/ojsolution-build/cf/cf369/pe.html 坑点:
1、long long 类型的常量一定要加LL,否则1<<n只在int范围内
2、带模的题目,最后一定要判断是否答案为负,答案为负数要加mod
*/
#include <cstdio>
#include <algorithm>
using namespace std;
const int mod=;
long long n,k;
long long Legendre(long long n,long long p)//勒让德定理:O(logn) 算出n!中有多少个p
{
long long ans=;
while(n>)
{
ans+=n/p;
n/=p;
}
return ans;
}
long long pow(long long base,long long n)
{
long long ans=;
base=base%mod;//先取模防止爆long long
while(n>)
{
if(n&)
ans=(ans*base)%mod;
base=(base*base)%mod;
n>>=;
}
return ans;
}
int main()
{
//freopen("cf711E.in","r",stdin);
scanf("%I64d%I64d",&n,&k);
if(n<= && k>(1LL<<n))//抽屉原理
{
printf("1 1\n");
return ;
}
long long gcd=Legendre(k-,);
long long p=,q;//p/q;
q=((n%(mod-))*((k-)%(mod-))-gcd%(mod-))%(mod-)+mod-;//欧拉函数降幂
//q=(n%(mod-1))*((k-1)%(mod-1))+mod-1-gcd; this is a wrong way!!!!!!
q=pow(,q)%mod;//q=2^( n(k-1)-gcd ) <=> 2^((n(k-1)-gcd)%phi(mod)+phi(mod) );
if(k->=mod)//抽屉原理得出在分子中必定存在一个%mod=0,标程大坑,不能直接输出1 1,即此处不约分。
p=;
else
{
long long val=pow(,n);
for(long long i=;i<=k-;i++)
{
p=(p*((val-i))%mod)%mod;
}
if(gcd)
{
p=(p*pow(pow(,gcd),mod-))%mod;
//p=(p+mod)/pow(2,gcd);
}
}
p=q-p;
if(p<)//判断是否为负
p+=mod;
printf("%I64d %I64d\n",p,q);
return ;
}
CF369E. 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 ...
- ZS and The Birthday Paradox
ZS and The Birthday Paradox 题目链接:http://codeforces.com/contest/711/problem/E 数学题(Legendre's formula) ...
- 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 ...
- 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可 ...
随机推荐
- 关于PHP浮点数之 intval((0.1+0.7)*10) 为什么是7
PHP是一种弱类型语言, 这样的特性, 必然要求有无缝透明的隐式类型转换, PHP内部使用zval来保存任意类型的数值, zval的结构如下(5.2为例): struct _zval_struct { ...
- oc5--方法
// main.m // 第一个OC类-方法2 #import <Foundation/Foundation.h> // 1.编写类的声明 @interface Iphone : NSOb ...
- poj 3913(水)
Description You have devised a new encryption technique which encodes a message by inserting between ...
- 【NOIP 2009】 靶形数独
[题目链接] https://www.luogu.org/problemnew/show/P1074 [算法] 搜索 + 剪枝 [代码] #include<bits/stdc++.h> u ...
- Ubuntu下安装sublime text3并汉化
转载请注明出处:果冻栋吖 通过ppa安装,打开终端,输入以下命令: sudo add-apt-repository ppa:webupd8team/sublime-text- sudo apt-get ...
- BZOJ 4547 矩阵快速幂
思路: 肯定每回只加最大值和次大值 如果 一开始的最大值>0且次大值<0 那就一直加 加到次大值>0 搞一个矩阵 推斐波那契数列 求和 就好- //By SiriusRen #inc ...
- 4、Collection接口功能测试(所有的All方法)
package cn.itcast_01; import java.util.ArrayList; import java.util.Collection; /* * 所有带All的方法:(听完就忘) ...
- Excel数据迁移到SQL Server遇到的若干问题
系统环境为:Windows Server 2008 r2 SQL Server 2012 1.建表过程中,如果用图形化的方式修改表结构会遇到问题: '不允许保存更改.您所做的更改要求删除并重新创建以下 ...
- SQL触发器 inset自学经验
本人建立了一个特价汇网站,想要记录每个商品的点击量和整个网站的访问量,于是就想用sql 触发器来实现 drop trigger tgr_cg_records_update_column create ...
- MacType 缺字问题【转】