E. ZS and The Birthday Paradox

题目连接:

http://www.codeforces.com/contest/711/problem/E

Description

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.

Sample Input

3 2

Sample Output

1 8

Hint

题意

有\(2^n\)天,有\(k\)个小朋友,问你这些小朋友在这n天,至少有两个小朋友的生日在同一天的概率是多少,分子分母 mod 1e6+3

题解:

首先容斥,这个很简单。

最难的就是约分,然后我们考虑约分这个玩意儿,他肯定是除以gcd,显然gcd是2的幂,分母的幂显然比分子多,那么我统计一下分子有多少个2 就好了

如果k>=mod,显然答案为0,否则我就暴力。

然后就完了。

特判掉,人比天数多的情况

代码

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e6+3;
long long quickpow(long long m,long long n,long long k)//返回m^n%k
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
long long gcd(long long a,long long b)
{
if(b==0)return a;
return gcd(b,a%b);
}
int main()
{
long long n,k;
cin>>n>>k;
if(n<62&&k>(1LL<<n))return puts("1 1"),0;
long long num = n;
for(long long i=1;i<62;i++)
num+=(k-1)/(1LL<<i);
long long A=1;
if(k<mod)
{
for(long long i=1;i<=k;i++)A=A*(quickpow(2,n,mod)-i+mod+1)%mod;
A=A*quickpow(quickpow(2,mod-2,mod),num,mod)%mod;
}
else
A=0;
long long B = quickpow(quickpow(2,n,mod),k,mod)*quickpow(quickpow(2,mod-2,mod),num,mod)%mod;
cout<<(B-A+mod)%mod<<" "<<B<<endl;
}

Codeforces Round #369 (Div. 2) E. ZS and The Birthday Paradox 数学的更多相关文章

  1. Codeforces Round #369 (Div. 2)E

    ZS and The Birthday Paradox 题目:一年有2^n天,有k个人,他们的生日有冲突的概率是多少?答案用最简分数表示,分子分母对1e6+3取模.1 ≤ n ≤ 10^18, 2 ≤ ...

  2. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  3. Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

    Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...

  4. Codeforces Round #369 (Div. 2) D. Directed Roads 数学

    D. Directed Roads 题目连接: http://www.codeforces.com/contest/711/problem/D Description ZS the Coder and ...

  5. Codeforces Round #369 (Div. 2) C. Coloring Trees 动态规划

    C. Coloring Trees 题目连接: http://www.codeforces.com/contest/711/problem/C Description ZS the Coder and ...

  6. Codeforces Round #369 (Div. 2) B. Chris and Magic Square 水题

    B. Chris and Magic Square 题目连接: http://www.codeforces.com/contest/711/problem/B Description ZS the C ...

  7. Codeforces Round #369 (Div. 2) A. Bus to Udayland 水题

    A. Bus to Udayland 题目连接: http://www.codeforces.com/contest/711/problem/A Description ZS the Coder an ...

  8. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  9. Codeforces Round #369 (Div. 2) A. Bus to Udayland (水题)

    Bus to Udayland 题目链接: http://codeforces.com/contest/711/problem/A Description ZS the Coder and Chris ...

随机推荐

  1. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

  2. 一致性哈希算法介绍,及java实现

    应用场景 在做服务器负载均衡时候可供选择的负载均衡的算法有很多,包括: 轮循算法(Round Robin).哈希算法(HASH).最少连接算法(Least Connection).响应速度算法(Res ...

  3. gcc初步窥探

    由于没有上过Linux编程这门课,所以Linux学得很水啊!!用来用去都是ls -al ; cd .. ;这些渣命令,尤其gcc都不知道什么东西来的,所以先学一下吧. 一.程序的编译过程 对于GUN编 ...

  4. ASP.NET MVC3-Music Store中英文教程 [下载]

    翻译原文档名: MVC Music Store版本: ASP.NET MVC3概述Mvc Music Store 是一个为WEB开发人员一步一步介绍和解释如何使用MVC和Visual Web开发的应用 ...

  5. 关于Spring mvc注解中的定时任务的配置

    关于spring mvc注解定时任务配置 简单的记载:避免自己忘记,不是很确定我理解的是否正确.有错误地方望请大家指出. 1,定时方法执行配置: (1)在applicationContext.xml中 ...

  6. 如何解决Mac只能登QQ不能联网

    如何解决Mac只能登QQ不能联网,路由正常,Wifi帐号密码正确,但wifi中断不能联网的问题.  

  7. 第8月第16天 django pil

    1. https://github.com/chaonet/forum/ sudo  easy_install --find-links http://www.pythonware.com/produ ...

  8. Aho-Corasick 多模式匹配算法、AC自动机详解

    Aho-Corasick算法是多模式匹配中的经典算法,目前在实际应用中较多. Aho-Corasick算法对应的数据结构是Aho-Corasick自动机,简称AC自动机. 搞编程的一般都应该知道自动机 ...

  9. py-faster-rcnn代码阅读1-train_net.py & train.py

    # train_net.py#!/usr/bin/env python # -------------------------------------------------------- # Fas ...

  10. 底板芯片组与内存映射(Motherboard Chipsets and the Memory Map) 【转】

    转自:http://blog.chinaunix.net/uid-25909619-id-4194650.html 底板芯片组与内存映射 我打算写一些关于计算机内部构造(computer intern ...