CodeForces711E ZS and The Birthday Paradox 费马小定理求逆元

GNAT 2021 社区版 vs Alire:2 种主流 Ada 开发环境安装方案对比 本文对比了GNAT 2021社区版与Alire两种主流Ada开发环境的安装方案,详细介绍了GNAT Studio的安装教程及Alire的现代化工具链管理。通过全平台部署指南和功能对比,帮助开发者选择最适合的开发环境,提升Ada编程效率。 阅读详情

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


题意是求一年有 2^n 天,k 个人出现两人生日相同的可能性是多少。

那么,只需考虑所有人生日都各不相同的情况就好了;

当 k > 2^n , 概率为1;

否则,为 1-A( 2^n , k )/((2^n)^k);

题中要求分子分母互质,而分母为 2 的幂 ,那么两者的GCD 也为 2 的幂。

容易知道,A( 2^n , k ) 为 k的相邻数的乘积,并且第一个数为 2^n ,  那么后面的一系列数是否为2的倍数则仅仅与他们为第几个数有关,也就是 2^n 后所减去的数。

这样我们就能得到两者的GCD了,由于分子分母均需要除以他们的GCD ,并且分子分母原来的形式比除去GCD后 更容易求解,那么,用逆元的方式则显得很有必要。

而该题中,mod 为一素数,并且GCD为2的幂,与之互素,那么可以用费马小定理来求解。

最后值得一提的一点是,在求分子对mod 的模时,为一系列连续自然数的乘积,当这些数个数较多时,便容易超时,加一句 判断 当前数是否被mod 整除的语句,则可以很好的改善这一问题。


#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;
const int mod=1000003;
LL quickpowmod(LL x,LL y,LL mo)
{
    LL ret=1;
    while(y){
        if(y&1)
            ret=ret*x%mo;
        x=x*x%mo;
        y>>=1;
    }
    return ret;
}
int main()
{std :: ios :: sync_with_stdio (false);
    LL n,k,ans1,ans2;
    while(cin>>n>>k){
        if(n<=60&&k>(1ll<<n)){
            printf("1 1\n");
            continue;
        }
        LL i=0;
        LL j=k-1;
        while(j){
            j/=2;
            i+=j;
        }
        ans1=1;
        LL s=quickpowmod(2,n,mod);
        LL t=s;
        for(int i=1;i<=k-1;i++){
            ans1=(ans1*(--s))%mod;
            if(!ans1) break;
        }
        LL ss=quickpowmod(quickpowmod(2,i,mod),mod-2,mod);
        ans1=(ans1*ss)%mod;
        ans2=quickpowmod(t,(k-1),mod);
        ans2=(ans2*ss)%mod;
        ans1=(ans2-ans1+mod)%mod;
        cout<<ans1<<" "<<ans2<<endl;
    }
    return 0;
}


手把手教你安装 OpenClaw 小龙虾(MAC安装教程),一次成功不踩坑 本文详细介绍了在Mac上安装OpenClaw的完整流程。主要内容包括:1)准备工作(磁盘空间、网络环境、终端工具);2)安装Homebrew包管理器及常见问题解决;3)安装Node.js 22+版本(环境核心);4)Git安装与验证;5)OpenClaw项目克隆、依赖安装及运行配置;6)大模型配置方法(以MiniMax为例)。文章提供了详细的命令行操作步骤和常见错误解决方案,特别强调了网络环境要求和Apple Silicon芯片的原生支持注意事项。最后展示了验证配置成功的方法和资源获取渠道。 阅读详情

相关推荐

CDH Hue连接Hbase报错TSocket read 0 bytes

问题描述 安装好CDH集群后,在Hue中查看HBase信息时,Hue界面报错如下: Api 错误:TSocket read 0 bytes Hue日志报错: exceptions_renderable ERROR Potential trace: [('/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hue/apps/hbase/src/hbase/api.py', 46, 'query', 'return getattr(self

邢为栋 3496

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

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 fo

十分残念的博客 694

Frigate进阶玩法:如何用MQTT和Python脚本扩展你的自定义目标检测算法

本文详细介绍了如何通过MQTT和Python脚本扩展Frigate的自定义目标检测算法,无需修改源码即可实现特殊场景(如工业设备异常、农业病虫害识别)的高效检测。文章涵盖Frigate配置、外部检测服务构建、高级集成技巧及性能优化实践,帮助开发者灵活扩展智能监控能力。

weixin_42662293的博客 356

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 megabytes input standard input output standard output ZS the Coder has recently fo

HandsomeHow的博客 774

从零到一:移远EC200UCN_LA OpenCPU开发中的‘避坑’实战指南

本文详细介绍了移远EC200UCN_LA 4G通信模块的OpenCPU开发实战指南,涵盖开发环境搭建、项目构建、通信接口开发、调试技巧及固件升级等关键环节。通过避坑经验和实用技巧,帮助开发者高效完成二次开发,提升4G通信模块的应用开发效率。

weixin_29159711的博客 509

SRAM compiler中Power Gating与Retention的实战配置指南

本文深入解析了在SRAM compiler中配置Power Gating(电源门控)与Retention(数据保持)功能的实战指南。针对低功耗芯片设计,文章详细阐述了如何根据应用场景(如移动AP、IoT设备)权衡开关类型、尺寸、唤醒时序以及保持电压,以实现静态功耗优化与数据可靠性的最佳平衡,并强调了后端实现与签核检查的关键要点。

weixin_29204121的博客 299

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

[Codeforces #369 (Div. 2)E. ZS and The Birthday Paradox]勒让德定理+逆元题目链接:[Codeforces #369 (Div. 2)E. ZS and The Birthday Paradox] 题意描述:这个题目是基于生日悖论为背景的一道题目。某个星球一年有2N2^N天,有K个人,问存在两个或两个以上的人同一天生日的概率,要求写成约分之后的

Fighting! 1216

基于YOLOv8的MVTec AD缺陷检测系统设计与实现

速度快,精度高;支持 anchor-free 架构;支持实例分割和图像分类;模块化代码,易于部署与改进;完美兼容 ONNX、TensorRT 等推理框架;官方支持多平台训练和推理(CPU/GPU)。YOLOv8 是工业视觉检测落地非常理想的选择。MVTec AD(Anomaly Detection)数据集包括15类工业对象,每个对象包括多个子类缺陷(例如 “scratch”, “crack”, “hole”)及正常图像。类别缺陷子类bottlecablecapsule。

YOLO 937

Codeforces 711E ZS and The Birthday Paradox

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 fo

nare123的博客 485

最新Python爬虫实战(入门爬虫篇)——案例15:微博热搜数据采集(详细爬虫思路截图+抓包动图演示+完整爬虫代码+详细注释)

本次案例通过 Python 爬虫技术,成功实现了对微博热搜 API 接口数据的批量爬取与 Excel 保存。代码采用模块化设计,包含接口请求、JSON 解析、数据提取、Excel 持久化等完整流程;通过模拟真实浏览器的请求头和 cookies,有效规避基础反爬策略,保证请求的稳定性。整个爬虫逻辑遵循“API请求 → JSON解析 → 数据提取 → 保存到Excel”的标准流程,与异步加载类网站的爬取范式高度一致。AJAX 接口抓包与分析。

努力让自己发光,对的人才能迎着光而来 7万+

YOLOv11改进 - 卷积Conv | SPD-Conv空间深度转换卷积优化空间信息编码,攻克小目标检测难题

本文介绍了一种名为SPD-Conv的新型CNN构建块及其在YOLOv11中的结合。传统CNN在处理低分辨率图像或小物体时,因使用步长卷积和池化层导致细粒度信息丢失、性能下降。SPD-Conv由空间到深度(SPD)层和非步长卷积(Conv)层组成,完全消除卷积步长和池化层,避免信息损失。SPD层重排特征图元素将空间信息转换到深度维度,非步长卷积层进一步处理特征图。我们将SPD-Conv集成进YOLOv11,实验证明其在低分辨率图像和小物体检测任务上显著优于现有模型。

魔改工程师的博客 1152

Codeforces711 E. ZS and The Birthday Paradox(数论)

题意: 解法: 当k>2n时,显然概率=1,下面只考虑k<=2n的情况.容易想到将问题转化为1−(k个人生日互不相同的概率).k个人生日互不相同的概率=A(2n,k)2nk=∏i=2n−k+12ni2nk=∏i=2n−k+12n−1i2n(k−1)分母可以直接快速幂.对于分子部分,由于需要对1e6+3取模,那么当分子存在1e6+3时,取模就会变成0,观察到分子的区间为[2n−1−k+1,2n−1],区间长度=k−1,当k−1>=1e6+3时,一定存在一个1e6+3的倍数,当k−1<

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 311

HotSpot G1 GC 的分区记账、SATB 并发标记与停顿目标调优解析

线上GC毛刺该调停顿目标、加大新生代还是换收集器?本文从HotSpot G1的设计出发,梳理分区与巨型对象、卡表与记忆集、写屏障与细化线程、停顿预测、SATB并发标记、自适应IHOP、混合回收规则及Full GC兜底,并注明JDK8至JDK27的版本差异。文中在JDK8与JDK17上实测:停顿目标从200ms降到20ms,伊甸区缩小四成、P99下降但仍超目标;巨型对象会反复触发又撤销并发标记;JDK17并行Full GC平均停顿约为JDK8的三成。最后给出日志解读、参数清单、调优流程与选型。

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 170万+

ZS and The Birthday Paradox

问题:http://codeforces.com/contest/711/problem/E 概率计算问题,需结合数论知识化简运算规模 关键知识点: 1. gcd(a, b) = gcd(b - a, b or a) (if b > a) 2. x | 2^i ===>  2^n - x | 2^i (n >= i) 从而将求能整除 2^n - x的最大2次幂转换为求能整除x的最大

lubovbyc的专栏 396

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

CodeForces - 711E

这是一道非常好的数论题目: 题解: 知识点: 1.鸽巢原理 2.逆元 3.勒让德定理(请自行百度) 4.gcd转换公式 5.暴力 一开始我知识列了公式,但是就没法做下去了,其实,这一题的突破点在于gcd公式的灵活运用,我在这一方面还是欠缺啊。 除此之外,pow函数在long long的运算中,不能直接代入,而要用一个变量来保存,我在这个上面debug了好长时间。。。。 ...

Ivanzn的博客 311

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

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

239

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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

[传送门](http://codeforces.com/contest/711/problem/E) E. ZS and The Birthday Paradoxtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputZS the Coder has r

代码改变世界 1585

[Codeforces 711E] ZS and The Birthday Paradox (数学+Legendre公式)

Codeforces - 711E 生日攻击的模型,现在有 2^n天,k个人,问不冲突的概率 其中 n和 k都是在 LL范围内的数,并且要输出既约分数 很容易就列出计算式,难点就在于约分 算式大概是 1−ab=b−ab1-\frac a b = \frac {b-a} b这个形式的 然后可得 gcd(b−a,b)=gcd(a,b)\gcd(b-a, b) = \gcd(a, b) 所

MisDeer's blog 436

CF369E. ZS and The Birthday Paradox

1 /* 2 cf369E. ZS and The Birthday Paradox 3 http://codeforces.com/contest/711/problem/E 4 抽屉原理+快速幂+逆元+勒让德定理+费马小定理+欧拉定理+数论 5 题解:https://amoshyc.github.io/ojsolution-build/cf/cf369/pe.h...

dianan9456的博客 241

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 input standard input output standard output ZS the Coder has recently fo

LzyRapX 906

Codeforces 711E ZS and The Birthday Paradox(数学)

这题真是个不错的题,一年有2n天,有k个人,至少两人同一天生日的概率,化成最简分数,上下mod1e6+3这题真是个不错的题,一年有2^n天,有k个人,至少两人同一天生日的概率,化成最简分数,上下mod 1e6+3 首先公式很快可以搞出来1−(2n−1)...(2n−(k−1))2n(k−1)首先公式很快可以搞出来1-\frac{(2^n-1)...(2^n-(k-1))}{2^{n(k-1)}}

Miracle_ma的专栏 986

CodeForces 711 E.ZS and The Birthday Paradox(组合数学)

Description 一年2^n天,k个人,问这k个人中至少有两个人生日是同一天的概率 Input 两个整数n和k(1<=n<=1e18,2<=k<=1e18) Output 将答案表示为最简分数形式A/B,输出A B,结果过大把A和B模1e6+3后输出 Sample Input 3 2 Sample Output 1 8 Solution Code#include<cs

ZSQ 503

codeforces 711E 勒让德定理 逆元

要先化简再取模 公式很好推主要是化简 用到两条性质 1. gcd(2^n - k, 2^n) = gcd(k, 2 ^ n) 因为当k为奇数 2^n - k 为奇数 那等式两边都为1 当k为偶数时 2^n - k 为偶数 k = b * 2^k1 b为奇数 2^n - k = 2^k1*(2^(n-k1)-b) 2^(n-k1)-b 是奇数,所以gcd(2^n - k, 2^n) = gc

acblacktea的博客 734

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

ZS and The Birthday Paradox Time Limit: 20 SecMemory Limit: 512 MB Description    Input    Output    Sample Input   4 3 Sample Output   23 128 HINT    Solution    Code 1 ...

weixin_34405354的博客 177

Codeforces-711E-ZS and The Birthday Paradox(高级数论)

转载于:https://www.cnblogs.com/GrowingJlx/p/6642778.html

abcd12341abcd1234的博客 143

Light oj 1104 Birthday Paradox (生日悖论----概率)

已知星球上有多少天,问至少邀请多少人参加生日party能使party上至少两个人 同一天生日的概率至少为0.5。 算法: 就是概率公式。参看wiki:生日悖论  计算机率的方法是,首先找出p(n)表示n个人中,每个人的生日日期都不同的概率。假如n > 365,根据鸽巢原理其概率为0,假设n ≤ 365,则概率为:      因为第二个人不能跟第一个人有相同的

虽然冲动永远比坚持容易~ 2644
上一篇: CSU1803 2016 同余
下一篇: LightOJ1007 Mathematically Hard 欧拉函数+前缀和
Megumin
博客等级 码龄10年 3粉丝 89原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值