Codeforces #711E: ZS the Birthday Paradox 题解

根据生日攻击理论的算法,我们很容易得到计算答案的公式,用1减去没有人生日相同的概率

ans==12n12n2n22n...2nk+12n1Πk1i=12ni2n(1)(2) (1) a n s = 1 − 2 n − 1 2 n ⋅ 2 n − 2 2 n ⋅ . . . ⋅ 2 n − k + 1 2 n (2) = 1 − Π i = 1 k − 1 2 n − i 2 n

首先我们要考虑约分
因为分母只有2这一个因子,所以我们只要考虑分子里面有多少个2就可以了
显然 2ni 2 n − i 所含的2的数目与i所含的2的数目相同
所以我们只要求1~k-1中有多少个2,我们只要枚举2,4,8…一个一个统计一遍就好
然后我们发现了一个重要的性质
如果 k1e6+3 k ≥ 1 e 6 + 3 ,因为分子是若干个连续的数相乘,所以其中必然有一个是1e6+3的倍数,又因为gcd只有因子2,所以约分完之后分子仍然是1e6+3的倍数,取模之后必为0
这种情况分子就是分母,把分母用快速幂求一下就好
如果 k1e6+3 k ≤ 1 e 6 + 3 ,就暴力算一下就好

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <utility>
#include <cctype>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#define LL long long
#define LB long double
#define x first
#define y second
#define Pair pair<int,int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define LOWBIT(x) x & (-x)
using namespace std;

const int MOD=1e6+3;
const LL LINF=2e16;
const int INF=1e9;
const int magic=348;
const double eps=1e-10;
const double pi=3.14159265;

inline int getint()
{
    char ch;int res;bool f;
    while (!isdigit(ch=getchar()) && ch!='-') {}
    if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
    while (isdigit(ch=getchar())) res=res*10+ch-'0';
    return f?res:-res;
}

inline LL getLL()
{
    char ch;LL res;bool f;
    while (!isdigit(ch=getchar()) && ch!='-') {}
    if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
    while (isdigit(ch=getchar())) res=res*10+ch-'0';
    return f?res:-res;
}

LL n,k;
LL Gcd,Inv;
LL Up,Down;

inline LL quick_pow(LL x,LL y)
{
    x%=MOD;LL res=1;
    while (y)
    {
        if (y&1) res=(res*x)%MOD,y--;
        x=(x*x)%MOD;y>>=1;
    }
    return res;
}

inline void calc_gcd()
{
    Gcd=1;Inv=quick_pow(2,MOD-2);
    for (register LL i=2;i<=k-1;i<<=1)
        Gcd=(Gcd*quick_pow(Inv,(k-1)/i))%MOD;
}

namespace brute
{
    inline void solve()
    {
        LL i,cnt=0,starter=quick_pow(2,n);
        for (i=(starter-1+MOD)%MOD,Up=1,cnt=1;cnt<=k-1;i=(i-1+MOD)%MOD,cnt++) Up=(Up*i)%MOD;
        Up=(Up*Gcd)%MOD;
        Down=quick_pow(quick_pow(2,n),k-1);Down=(Down*Gcd)%MOD;
        Up=(Down-Up+MOD)%MOD;
        printf("%lld %lld\n",Up,Down);
    }
}

namespace big
{
    inline void solve()
    {
        Down=quick_pow(quick_pow(2,n),k-1);Down=(Down*Gcd)%MOD;
        printf("%lld %lld\n",Down,Down);
    }
}

int main ()
{
    n=getLL();k=getLL();
    if (n<=63 && (1ll<<n)<k) {printf("1 1\n");return 0;}
    calc_gcd();
    if (k<=1e6+48) {brute::solve();return 0;}
    big::solve();return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值