codeforces ZS and The Birthday Paradox
来源:互联网 发布:鬼脚七在淘宝里的职位 编辑:程序博客网 时间:2024/05/16 15:20
/*
费马小定理求逆元+勒让德定理求n!有多少个2+容斥
考虑至少两个人生日在同一天的逆命题,所有人生日都不在同一天,即
(2^n-1)(2^n-2)...(2^n-(k-1))/2^(n-1)k
对这个式子约分,容易看出对大公约数肯定是2的num次方
由gcd(2^n-x,2^n)=gcd(x,2^n)可知
求num的个数即求(k-1)!中2的个数(勒让德定理)
求出2^num后,由费马小定理求它的逆元,乘上分子分母
*/
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define ll long long
const ll mod = 1e6+3;
费马小定理求逆元+勒让德定理求n!有多少个2+容斥
考虑至少两个人生日在同一天的逆命题,所有人生日都不在同一天,即
(2^n-1)(2^n-2)...(2^n-(k-1))/2^(n-1)k
对这个式子约分,容易看出对大公约数肯定是2的num次方
由gcd(2^n-x,2^n)=gcd(x,2^n)可知
求num的个数即求(k-1)!中2的个数(勒让德定理)
求出2^num后,由费马小定理求它的逆元,乘上分子分母
*/
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define ll long long
const ll mod = 1e6+3;
ll pow_mod(ll a,ll b)
{
ll res=1;
while(b)
{
if(b&1)
res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
{
ll res=1;
while(b)
{
if(b&1)
res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
int main()
{
ll n,k;
scanf("%I64d %I64d",&n,&k);
if(n<=62 && k>(1ll << n))
{
printf("1 1\n");
return 0;
}
{
ll n,k;
scanf("%I64d %I64d",&n,&k);
if(n<=62 && k>(1ll << n))
{
printf("1 1\n");
return 0;
}
ll num=0;
for(ll i=k-1;i;i>>=1)
num+=i/2;
for(ll i=k-1;i;i>>=1)
num+=i/2;
ll a=pow_mod(2,n);
ll b=1;
for(ll i=1;i<=k-1;i++)
{
ll tmp=(a-i+mod)%mod;
b=b*tmp%mod;
if(tmp==0)
break;
}
ll ans=pow_mod( pow_mod(2,num),mod-2 );
a=pow_mod(a,k-1);
a=a*ans%mod;
b=b*ans%mod;
b=(a-b+mod)%mod;
printf("%I64d %I64d\n",b,a);
}
ll b=1;
for(ll i=1;i<=k-1;i++)
{
ll tmp=(a-i+mod)%mod;
b=b*tmp%mod;
if(tmp==0)
break;
}
ll ans=pow_mod( pow_mod(2,num),mod-2 );
a=pow_mod(a,k-1);
a=a*ans%mod;
b=b*ans%mod;
b=(a-b+mod)%mod;
printf("%I64d %I64d\n",b,a);
}
0 0
- codeforces ZS and The Birthday Paradox
- ZS and The Birthday Paradox
- codeforces Round#369 div2-E ZS and The Birthday Paradox
- CodeForces 369 div2 E. ZS and The Birthday Paradox 数论
- Codeforces 711E ZS and The Birthday Paradox(数学)
- Codeforces 711E. ZS and The Birthday Paradox
- Codeforces 711E ZS and The Birthday Paradox
- 【28.57%】【codeforces 711E】ZS and The Birthday Paradox
- 【数论】codeforce711E ZS and The Birthday Paradox
- cf/codeforces#369-E - ZS and The Birthday Paradox- 数学+gcd+逆元+勒让德定理
- [Codeforces #369 (Div. 2)E. ZS and The Birthday Paradox]勒让德定理+逆元
- Codeforces #369 (Div. 2) E. ZS and The Birthday Paradox (勒让德定理+逆元)
- CodeForces-711E ZS and The Birthday Paradox(勒让德定理+抽屉原理)
- codeforces Round#369 div2-E ZS and The Birthday Paradox(逆元,组合)★
- [Codeforces 711E] ZS and The Birthday Paradox (数学+Legendre公式)
- Codeforces 711E ZS and The Birthday Paradox 数论(Legendre's定理)
- CodeForces 711 E.ZS and The Birthday Paradox(组合数学)
- CodeForces711E ZS and The Birthday Paradox 费马小定理求逆元
- php或jqery 返回上一页并刷新
- 【Spring】——AOP设计基本原理
- 2#plsql 查询数据库乱码
- fragment 嵌套Fragment,出现空白的问题
- 又见数塔
- codeforces ZS and The Birthday Paradox
- Android Studio 非法字符: \65279
- jdbcTemplate
- HTTP 协议初探之--历史演变和设计思路
- CoordinatorLayout与滚动的处理
- 使用Timer代替Handler
- Go 语言简介(上)— 语法
- QML动画与过度
- Android进程的五个等级