time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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 2^3 = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly 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.

【题解】



每个人的生日都有2^n个可能;然后有k个人;

要求的是k个人里面至少有两个人的生日相同的概率;

正难则反;

求出所有人的生日都不同的概率p;

再用1减去p就可以了;

p=A(2^n,k)/((2^n)^k);

A是排列数;

写成乘法的形式就变成

p=(2^n)(2^n-1)(2^n-2)···(2^n-(k-1))/((2^n)^k)

分子分母同时除2^n;

(2^n-1)(2^n-2)···*(2^n-(k-1))/((2^(k-1))^n);

然后就是约分了;

分子的2的数目比分母少;

那么公约数就是2^temp次方了;

temp是分子的所含的因子2的个数;

而(2^n-1)···(2^n-(k-1))中

2^n-i的2因子的个数显然是i决定的,i有几个2因子,这项就有几个2因子;

比如2^n-2,2有一个2因子,所以这项有1个2因子;

实际上就可以转化为(k-1)!的2因子的个数;

有个关于阶乘(k-1)!素因子p的公式;

temp = ∑(k-1)/i;

其中i = p^1、p^2、p^3..p^m;

且2^m<= k-1;

求出来个数就好;

设为temp;

则可以约掉的数就是2^temp

分子和分母都要除2^temp;

但是要求余?

除法求余?

求乘法逆元!

乘法逆元?

比如要求(a/b)%p;

且(b*k)%p==1;

则(a/b)%p == (a*k)%p;

这个k就是b的乘法逆元。(可能有定义不对的地方。谅解下);

同时a/b一定要为整数;

证明:

因为(b*k)%p=1

所以b*k = p*x+1;

k = (p*x+1)/b;

则(a*k)%p=(apx/b+a/b)%p = ((a/b)*x*p)%p+(a/b)%p;

因为b能够整除a,所以a/b为整数,又乘上了p,则%p不就为0吗;

则(a*k)%p == (a/b)%p;

如何求这个k

b*k = p*x+1;

->k*b+(-x)*p=1

;

即解一个二元一次方程组;

->用扩展欧几里得算法求解;

扩展欧几里得算法?

ax+by=gcd(a,b);

这里如果a和b互质(因为p是质数而b是肯定小于p的(因为要取余嘛),所以b和p肯定是互质的);

ax+by=1

这里进行一下递推;



x1a+y1b=gcd(a,b);

x2b+y2(a%b) = gcd(b,a%b);

而又欧几里得算法gcd(a,b)==gcd(b,a%b);

所以x1a+y1b=x2b+y2(a%b);

a%b可以写出a-(a/b)*b 这里的/是整除



x1a+y1b=x2b+y2(a-(a/b)*b)

x1a+y1b=x2b+y2a-y2(a/b)*b

x1a+y1b=y2a+(x2-(a/b)*y2)*b

->x1=y2

->y1 =x2-(a/b)*y2

根据这个递推式

可以写出扩展欧几里得算法的程序

void ex_gcd(LL a,LL b,LL &x,LL &y)
{
if (b == 0)//gcd(a,b)==gcd(a,0)==a;所以要使得xa+yb==gcd(a,b)只要让x==1,y==0即可
{
x = 1;y = 0;
return;
}
ex_gcd(b,a % b,x,y);
LL temp = y;
y = x-(a/b)*temp;
x = temp;
}

我们只要执行

ex_gcd(2^temp,p,ni,useless);

我们要的是这个方程的k

k*b+(-x)*p=1

所以最后得到的ni就是k,也即2^temp的乘法逆元;

对于分母直接乘上这个ni。就表示除去了公约数;

对于分子

(2^n-1)(2^n-2)···*(2^n-(k-1))

如果k-1>=mod;则我们最少得到了连续的mod个数;

则这里面肯定有mod的倍数;

所以此时分子为0;

直接输出 分母-‘0’ 分母即可;

对于k-1小于mod的情况,这个时候k很小了。直接暴力求解

(2^n-1)(2^n-2)···*(2^n-(k-1))%mod即可;

然后输出 (分母-分子+mod)%mod 分母 即可;

#include <cstdio>
#include <algorithm>
#define LL long long using namespace std; const LL mod = 1e6+3;
const int INF = 63; LL n,k,tmp = 0,ni,fz,fm; LL ksm(LL x,LL y)
{
if (y == 0)
return 1;
LL temp =ksm(x,y>>1);
temp = (temp*temp)%mod;
if (y&1)
temp = (temp*x)%mod;
return temp;
} void ex_gcd(LL a,LL b,LL &x,LL &y)
{
if (b == 0)
{
x = 1;y = 0;
return;
}
ex_gcd(b,a % b,x,y);
LL temp = y;
y = x-(a/b)*temp;
x = temp;
} int main()
{
scanf("%I64d%I64d",&n,&k);
bool flag = false;
if (n >= 63)
flag = true;
else
{
LL temp = 1;
for (int i = 1;i <= n;i++)
{
temp = temp *2;
if (temp >=k)
{
flag = true;
break;
}
}
}
if (!flag)
{
puts("1 1");
return 0;
}
LL i;
for (i = 2;i <= (k-1);i<<=1)
tmp+=(k-1)/i;
tmp = ksm(2,tmp);
LL fm = ksm(ksm(2,k-1),n);
LL nu;//这个nu变量是没用的
ex_gcd(tmp,mod,ni,nu);
ni = (ni + mod) %mod;//求出来的ni是乘法逆元
fm = (fm * ni)%mod;
if (k-1>= mod)
printf("%I64d %I64d\n",fm,fm);
else//暴力求解分子
{
LL a = ksm(2,n);
LL fz = 1;
for (i = 1;i <= k-1;i++)
fz = (fz*((a-i+mod) % mod))%mod;
fz=(fz*ni)%mod;
fz = (fm-fz+mod)%mod;
printf("%I64d %I64d\n",fz,fm);
}
return 0;
}

【28.57%】【codeforces 711E】ZS and The Birthday Paradox的更多相关文章

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

  2. 【28.57%】【codeforces 615C】 Running Track

    time limit per test1 second memory limit per test512 megabytes inputstandard input outputstandard ou ...

  3. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  4. 【57.97%】【codeforces Round #380A】Interview with Oleg

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【75.28%】【codeforces 764B】Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【30.93%】【codeforces 558E】A Simple Task

    time limit per test5 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  8. 【77.78%】【codeforces 625C】K-special Tables

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. 【codeforces 760A】Petr and a calendar

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

随机推荐

  1. 硬件——nrf51822第三篇,按键控制小灯

    现象是按键按下,小灯亮,按键抬起,小灯灭. 从这一节我们细致剖析gpio口的设置: nrf51822片上一共有32个数字引脚,分为4个port,如下: port 0 pin 0-7 port 1 pi ...

  2. 嵌入式Qt-4.8.6显示中文并且改变字体大小和应用自己制作的字体库

    问题: QT4.8.6在移植到开发板上的时候,中文支持是必不可少的,如何让QT支持中文,如何制作QT支持的字体文件,如何使QT UI编辑器中的字号与开发板中的字号一致. 详解: 1>如何让QT支 ...

  3. (转)c++ 中的using namespace std是什么意思,什么时候用

    使用std命名空间 98年以后的c++语言提供一个全局的命名空间namespace,可以避免导致全局命名冲突问题.举一个实例,请注意以下两个头文件: // one.hchar func(char);c ...

  4. PHP回调函数--call_user_func_array

    我这是抄的 感谢 https://www.cnblogs.com/zzl-21086595/p/4547519.html 全局函数的回调 这里的全局函数的意思,是直接使用function定义的函数,它 ...

  5. ITFriend网站内测公测感悟

    4月份做出网站Demo,就开始让用户使用了. 最初的黄色版界面,被吐槽得比较厉害. 关于界面,每个人都有自己的看法,只是喜欢和不喜欢的人比例不一样. 后来,花3400元请了个设计师,设计了一套界面,整 ...

  6. 【例题5-5 UVA 12096 】The SetStack Computer

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用set来解决这个问题. 考虑如何表示 { {{}} }这个集合 我们可以把{}这个集合和一个数字映射->1 然后把1加入到某 ...

  7. 【u252】泽泽在巴西

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 泽泽帮助了英国某街道尽量减少酸雨的伤害,街道办主任非常感激他,就把他领到一扇门前,告诉他这扇门能通往好 ...

  8. poj 2063 Investment ( zoj 2224 Investment ) 完全背包

    传送门: POJ:http://poj.org/problem?id=2063 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  9. 机器学习算法笔记1_2:分类和逻辑回归(Classification and Logistic regression)

    形式: 採用sigmoid函数: g(z)=11+e−z 其导数为g′(z)=(1−g(z))g(z) 如果: 即: 若有m个样本,则似然函数形式是: 对数形式: 採用梯度上升法求其最大值 求导: 更 ...

  10. js进阶 12-4 jquery键盘事件如何使用

    js进阶 12-4 jquery键盘事件如何使用 一.总结 一句话总结:键盘和鼠标都是外设输入设备,所以函数很像,所以使用就像鼠标事件click一样 1.jquery键盘事件有哪三个? 1(up和do ...