传送门

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 $2^n$ days in a year. ZS the Coder wants to interview $k$ people from Udayland, each of them has birthday in one of $2^n$ 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 $\dfrac{A}{B}$. 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 \le n \le 10^18, 2 \le k \le 10^18$), meaning that there are $2^n$ 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 $2^n$ days long year equals ($A \ge 0, B \ge 1, \gcd(A,B)=1$), print the $A$ and $B$ in a single line.

Since these numbers may be too large, print them modulo $10^6 + 3$. Note that $A$ and $B$ must be coprime before their remainders modulo $10^6 + 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 $2^3 = 8$ days in Udayland. The probability that $2$ people have the same birthday among $2$ people is clearly $\frac{1}{8}$ , so $A = 1, B = 8$.

In the second sample case, there are only $2^1 = 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$.


Solution

首先注意到$10^6+3$是个素数.

不难想到求任意两人生日都不冲突的概率更为简单, 答案是$\dfrac{A_{2n}{k}}{2^{nk}}$, 展开化简得

$$ \frac{(2n-1)(2n-2)\cdots(2n-(k-1))}{2{n(k-1)}} $$

这里我们需要注意:

$$\dfrac{a}{b}既约\Longleftrightarrow \dfrac{a}{b-a}既约,\ (b>a)$$

因为$\gcd(a, b)=\gcd(a, b-a)$.

接着要对此式进行约分, 也就是求分子的素因子分解形式中2的幂次. 这里有个key observation:

$$2^n-x中2的幂次和x中2的幂次相同.$$

所以问题转化成求$(k-1)!$中所包含的2的幂次.

而$n!$中包含的素数$p$的幂次, 记作$\nu_p(n!)$, 为:

$$\nu_p(n!)=\sum_{i\ge 1}[\frac{n}{p^i}]$$

上式也称作Legendre's formula.

注意: 如果$p$不是素数, 这个结论是不成立的.

Implementation

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; // return the max k s.t. p^k | n!
LL get_exponent(LL n, int p){
LL res=0;
for(LL x=p; n>=x; res+=n/x, x*=p);
return res;
} LL Pow(LL x, LL n, int p){
LL res=1;
for(; n; x*=x, x%=p, n>>=1)
if(n&1) res*=x, res%=p;
return res;
}
// return 2^{(k-1)*n}
LL calc2(LL n, LL k, int p){
return Pow(Pow(2, k-1, p), n, p);
}
// x cannot be divided by p
LL inv(LL x, int p){
return Pow(x, p-2, p);
}
// A(n, k)%p, p is small
// n>=k
LL calc3(LL n, LL k, int p){
if(k>=p) return 0;
LL res=1;
for(; k; ){
res*=n, res%=p;
if(n==0) break;
--k, --n;
}
return res;
} int main(){
LL n, k;
cin>>n>>k;
int p=1000003; if(log2(k)>n){
puts("1 1");
}
else{
LL cnt=get_exponent(k-1, 2); //error-prone
LL x=Pow(2, cnt, p); LL y=(Pow(2, n, p)+p-1)%p;
LL t=inv(x, p);
LL num=calc3(y, k-1, p)*t%p;
LL den=calc2(n, k, p)*t%p; num=(den-num+p)%p; //error-prone
cout<<num<<' '<<den<<endl;
}
}

Pitfalls

最近写代码总是犯各种各样的傻逼错误. 比如这次我把函数calc2()中的return Pow(Pow(2, k-1, p), n, p);写成了Pow(Pow(2, k-1, p), n, p);. 这样在本地竟然把3个样例都过了. 然后交上去竟然WA on T1.

历尽千辛万苦才找到bug. 我的编译器(g++ 5.4.0)没报错, 可能是自动识别了这个错误. 避免这个问题的方法是编译时加上-Wall选项.

g++ main.cpp -o main -Wall -std=c++14 && ./main <in

Codeforces 711E ZS and The Birthday Paradox的更多相关文章

  1. Codeforces 711E ZS and The Birthday Paradox 数学

    ZS and The Birthday Paradox 感觉里面有好多技巧.. #include<bits/stdc++.h> #define LL long long #define f ...

  2. Codeforces 711E ZS and The Birthday Paradox(乘法逆元)

    [题目链接] http://codeforces.com/problemset/problem/711/E [题目大意] 假设一年有2^n天,问k个小朋友中有两个小朋友生日相同的概率. 假设该概率约分 ...

  3. codeforces 711E. ZS and The Birthday Paradox 概率

    已知一年365天找23个人有2个人在同一天生日的概率 > 50% 给出n,k ,表示现在一年有2^n天,找k个人,有2个人在同一天生日的概率,求出来的概率是a/b形式,化到最简形式,由于a,b可 ...

  4. 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 ...

  5. 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 ...

  6. ZS and The Birthday Paradox

    ZS and The Birthday Paradox 题目链接:http://codeforces.com/contest/711/problem/E 数学题(Legendre's formula) ...

  7. CF369E. ZS and The Birthday Paradox

    /* cf369E. ZS and The Birthday Paradox http://codeforces.com/contest/711/problem/E 抽屉原理+快速幂+逆元+勒让德定理 ...

  8. 【Codeforces711E】ZS and The Birthday Paradox [数论]

    ZS and The Birthday Paradox Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample ...

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

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. lecture16-联合模型、分层坐标系、超参数优化及本课未来的探讨

    这是HInton的第16课,也是最后一课. 一.学习一个图像和标题的联合模型 在这部分,会介绍一些最近的在学习标题和描述图片的特征向量的联合模型上面的工作.在之前的lecture中,介绍了如何从图像中 ...

  2. 我的权限系统设计实现MVC4 + WebAPI + EasyUI + Knockout(五)框架及Web项目的组件化

    一.组件化印象 1.先给大家看一张截图 如果我告诉大家,这就是一个web管理系统发布后的所有内容,你们会不会觉得太简洁了,只有一个web.config.一个Global.asax文件,其它的都是dll ...

  3. 你真的理解 new 了吗?

    开篇先提几个问吧,如果你对这些问题都清楚了,那说明对于 new  这个关键字已经掌握得很好了,也不再需要花时间来阅读本文了, 1   new  一个class  与 new   一个Struct有什么 ...

  4. 深入理解OOP(四): 多态和继承(抽象类)

    在本文中,我们讨论OOP中的热点之一:抽象类.抽象类在各个编程语言中概念是一致的,但是C#稍微有些不一样.本文中我们会通过代码来实现抽象类,并一一进行解析. 深入理解OOP(一):多态和继承(初期绑定 ...

  5. P值,“差异具有显著性”和“具有显著差异”

      P值是论文中最常用的一个统计学指标,可是其误用.解释错误的现象却很常见.因此,很有必要说明p值的意义.用法及常见错误.   P值指的是比较的两者的差别是由机遇所致的可能性大小.P值越小,越有理由认 ...

  6. JS日历制作获取时间

    1.直接获取 var myDate = new Date(); myDate.getYear(); 获取当前年份(2位) myDate.getFullYear(); 获取完整的年份(4位,1970-? ...

  7. 【心得】在脱离TFS的情况下,如何解除TFS绑定?

    我们知道在有TFS的情况下,在文件-源代码管理-高级中可以解除TFS绑定. 但是如果我们出差去了,拿着笔记本电脑,打开解决方案的时候,会总是提示无法连接TFS,并且在源代码管理处尝试解除的时候也提示无 ...

  8. [转] Java序列化与反序列化

    原文地址:http://blog.csdn.net/wangloveall/article/details/7992448 Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java ...

  9. [转]NullPointerException异常

    原文地址:http://blog.csdn.net/javaeeteacher/article/details/4285488 顾名思义,NullPointerException是空指针异常.但是在J ...

  10. swift 学习(二)基础知识 (函数,闭包,ARC,柯里化,反射)

    函数 func x(a:Int, b:Int)  {}   func x(a:Int, b:Int) -> Void {}  func x(a:Int, b:Int) ->(Int,Int ...