Codeforces 711 E. ZS and The Birthday Paradox(数学)——Codeforces Round #369 (Div. 2)

原创 2016年08月30日 19:12:22
[传送门](http://codeforces.com/contest/711/problem/E)
E. ZS and The Birthday Paradox
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland.

In Udayland, there are 2n days in a year. ZS the Coder wants to interview k people from Udayland, each of them has birthday in one of 2n days (each day with equal probability). He is interested in the probability of at least two of them have the birthday at the same day.

ZS the Coder knows that the answer can be written as an irreducible fraction . He wants to find the values of A and B (he does not like to deal with floating point numbers). Can you help him?

Input

The first and only line of the input contains two integers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 1018), meaning that there are 2n days in a year and that ZS the Coder wants to interview exactly k people.

Output

If the probability of at least two k people having the same birthday in 2n days long year equals (A ≥ 0, B ≥ 1, ), print the A and B in a single line.

Since these numbers may be too large, print them modulo 106 + 3. Note that A and B must be coprime before their remainders modulo 106 + 3 are taken.

Examples
Input
3 2
Output
1 8
Input
1 3
Output
1 1
Input
4 3
Output
23 128
Note

In the first sample case, there are 23 = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly , so A = 1, B = 8.

In the second sample case, there are only 21 = 2 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the probability is 1 and A = B = 1.


题目大意:

一年有 2n(n1018) 天,然后有 k(k1018) 个小朋友,现在问你的是:这 k 个小朋友中至少有两个

小朋友生日相同的概率是多少,假设这个概率约分后为 pq,输出 p,q1000003 取模

解题思路:

首先我们来分析一下题目,因为题目中说 至少 两个小朋友生日相同的概率,那么我们从反面来考虑,那么也就是说所有

的小朋友的生日都不相同的概率是多少,那么我们这个可以列出一个式子:

A(2n,k)(2n)k(1)

那么我来解释一下这个式子:首先因为有 k 个小朋友,所以有 k个生日,然后一个小朋友生日是某一天的概率是

12n,所以 k 个小朋友的生日的概率就是 1(2n)k,那么我们现在保证每个小朋友的生日都不是

相同的,所以就是从 2n 天中选 k 个,然后有 k 种排列,所以就是 A(2n,k),所以我们要做的就是化简 (1) 式,

那么我们通过观察可以得出结论:分子和分母的最大公约数一定是 2i,所以我们只要将 GCD 求出来了,然后分子分母分

别除以最大公约数就行了,因为分子的 2 肯定是比分母少的,所以只要求出分子中有多少 2 就行了,这个问题就是相当于

n! 后面有多少个 0,因为分子展开为:

A(2n,k) = (2n)(2n1)(2n2) ... (2nk+1)

首先可以先将 2n 与 分母约分一下,然后找的是 (k1)! 有多少个 2 ,每次除以 2,这个就不细说了,那么找到

GCD 以后,我们就进行计算,因为分母好操作就是 n(k1) 注意用快速乘法小心爆 long long,然后就是分子了,其

实我们仔细观察一下的话,会发现如果 MOD < k 的 时候分子一定是 0,因为分子一直是 (2ni) 这样的,剩下的暴

力做就行了,得到分子之后别忘记除以 GCD,这个地方就要通过逆元来实现了,还有需要注意的是当 2n<k 的时候 直

接输出 1 1 就行了 最后不要忘记 概率是 1ans

My Code

/**
2016 - 08 - 30 晚上
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 1e3+5;
const LL MOD = 1e6+3;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
LL Scan_LL()///输入外挂
{
    LL res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
int Scan_Int()///输入外挂
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}
void Out(LL a)///输出外挂
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}
LL Multi(LL a, LL b)
{
    LL ans = 0;
    while(b)
    {
        if(b & 1)
            ans = (ans+a)%(MOD-1);
        b>>=1LL;
        a = (a+a)%(MOD-1);
    }
    return ans;
}
LL quick(LL a, LL b)
{
    LL ans = 1;
    while(b)
    {
        if(b & 1)
            ans = (ans*a) % MOD;
        b>>=1LL;
        a = (a*a) % MOD;
    }
    return ans;
}
void Exgcd(LL a, LL b, LL &x, LL &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    LL x1, y1;
    Exgcd(b, a%b, x1, y1);
    x = y1;
    y = x1 - (a/b)*y1;
}
int main()
{
    LL Inv, y;
    Exgcd(2, MOD, Inv, y);
    Inv = (Inv%MOD+MOD)%MOD;
    LL n, k;
    while(cin>>n>>k)
    {
        if(n < 63LL)
        {
            if(k > (1LL<<n))
                puts("1 1");
            else
            {
                LL ans = 0, fm = k, tmp;
                fm--;
                while(fm)
                {
                    fm>>=1LL;
                    ans += fm;
                }
                fm = k-1LL;
                fm = Multi(n, fm);
                fm = (fm-ans)%(MOD-1);
                fm = (fm%(MOD-1)+(MOD-1))%(MOD-1);
                fm = quick(2LL, fm);
                if(k > MOD)
                {
                    cout<<fm<<" "<<fm<<endl;
                    continue;
                }
                tmp = quick(2LL, n);
                LL fz = 1;
                for(LL i=1; i<k; i++)
                    fz = fz*(tmp-i)%MOD;
                fz = (fz%MOD+MOD)%MOD;
                fz = fz*quick(Inv, ans)%MOD;
                fz = fm - fz;
                fz = (fz%MOD+MOD)%MOD;
                cout<<fz<<" "<<fm<<endl;
            }
        }
        else
        {
            LL ans = 0, fm = k, tmp;
            fm--;
            while(fm)
            {
                fm>>=1LL;
                ans += fm;
            }
            /** 2^ans 是最大公约数 **/
            fm = k-1LL;
            fm = Multi(n, fm);
            fm = (fm-ans)%(MOD-1);
            fm = (fm%(MOD-1)+(MOD-1))%(MOD-1);
            fm = quick(2LL, fm);
            if(k > MOD)
            {
                cout<<fm<<" "<<fm<<endl;
                continue;
            }
            tmp = quick(2LL, n);
            LL fz = 1;
            for(LL i=1; i<k; i++)
                fz = fz*(tmp-i)%MOD;
            fz = fz*quick(Inv, ans)%MOD;
            fz = fm - fz;
            fz = (fz%MOD+MOD)%MOD;
            cout<<fz<<" "<<fm<<endl;
        }
    }
    return 0;
}
版权声明:编写不易,转载请注明出处,谢谢。 举报

相关文章推荐

codeforces Round#369 div2-E ZS and The Birthday Paradox

题意: 一年中有2^n天,问有k个人至少有两个人生日在同一天的概率,用分数表示。其中n,k 题解:1e6+3为素数。直接求不好求,我们可以总数-不成立的数。不成立的即为k个人中没有一个人的生日在同一...

CodeForces 369 div2 E. ZS and The Birthday Paradox 数论

E. ZS and The Birthday Paradox time limit per test 2 seconds memory limit per test 256 m...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

Codeforces #369 (Div. 2) E. ZS and The Birthday Paradox (勒让德定理+逆元)

E. ZS and The Birthday Paradox time limit per test 2 seconds memory limit per test 256 megabytes...

CodeForces-711E ZS and The Birthday Paradox(勒让德定理+抽屉原理)

E. ZS and The Birthday Paradox time limit per test 2 seconds memory limit per test 256 m...

Codeforces Problem 711E ZS and The Birthday Paradox(抽屉原理+乘法逆元+费马小定理+组合数+快速幂+概率论)

Codeforces Problem 711E ZS and The Birthday Paradox(抽屉原理+乘法逆元+费马小定理+组合数+快速幂+概率论)

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

time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandar...

【数论】codeforce711E ZS and The Birthday Paradox

题目大意: 给n和k。 求2^n天,k个人,其中至少两人同一天生日的概率。 1 ≤ n ≤ 1018, 2 ≤ k ≤ 1018 输出分子和分母 MOD 1e6+3 求MOD前,...

【Codeforces Round #369 (Div. 2)】Codeforces 711B Chris and Magic Square

模拟

Codeforces Round #369 (Div. 2) B. Chris and Magic Square【数学,模拟】

题意:输入一个n*n的矩阵,有一个位置为0,问你能否在0处填上一个正整数,使得该矩阵的每一行,每一列,主对角,副对角线上的和都相等,即构成一个幻方。 由于要构成一个幻方,所以只用根据行的和求出要填的数...

codeforces#369Div2(711B)Chris and Magic Square

codeforces#369Div2BChris and Magic Square
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)