中国剩余定理算法详解 + POJ 1006 Biorhythms 生理周期

中国剩余定理在信息领域的应用 写这边博文是听了台湾张真诚老师的一次讲座,感觉比较有趣。回来后自己查查资料加总结一点就想写下来,坚信好记性不如烂笔头,何况我记性比较差...... 中国剩余定理(Chinese Reminder Thereom) 中国剩余定理是数论中的一个关于一元线性同余方程组的定理,说明了一元线性同余方程组有解的准则以及求解方法。也称为孙子定理,古有“韩信点兵”、“孙子定理”、求一术(宋 沈括)“鬼谷算”( 阅读详情
先放上POJ的题目:

Biorhythms

Time Limit:  1000MS
Memory Limit: 10000K

Description

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier. 
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak. 

Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form: 

Case 1: the next triple peak occurs in 1234 days. 

Use the plural form ``days'' even if the answer is 1.

Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

Source


题目翻译:

Description

人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。

Input

输入四个整数:p, e, i和d。 p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于p, e, 或 i。 所有给定时间是非负的并且小于365, 所求的时间小于21252。 

当p = e = i = d = -1时,输入数据结束。

Output

从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。 

采用以下格式: 
Case 1: the next triple peak occurs in 1234 days. 

注意:即使结果是1天,也使用复数形式“days”。



中国剩余定理(CRT,Chinese Reminder Theory,又称孙子定理)

介绍:(参考《初等数论及其应用》)




PS:例中数据与实际的物不知数问题略有出入。


此题相当于解方程组

x+d≡p(mod 23)

x+d≡e(mod 28)

x+d≡i(mod 33)

由于23,28,33两两互素,所以M=23*28*33=21252,M1=924,M2=759,M3=644.

则有924y1≡1(mod 23),由924/23=(40,5,1,3)计算得y1=6(mod 23)

则有759y2≡1(mod 28),由759/28=(27,9,2,1)计算得y2=19(mod 28)

则有644y3≡1(mod 33),由644/33=(19,1,1,16)计算得y3=2(mod 33)

因此x+d≡(5544p+14421e+1288i)(mod 21252)

即x=(5544*p+14421*e+1288*i-d)%21252

本题所求的是最小整数解,避免x为负,因此最后结果为x=(x+21252)%21252
那么最终求解x的表达式就是:

x=(5544*p+14421*e+1288*i-d+21252)%21252

(若x为0,就令x为21252)


完整代码:

/*16ms,248KB*/

#include<cstdio>
using namespace std;

int main()
{
	int p, e, i, d, icase = 0, x;
	while (scanf("%d%d%d%d", &p, &e, &i, &d), ~p)
	{
		x = (5544 * p + 14421 * e + 1288 * i - d + 21252) % 21252;
		if (x == 0) x = 21252;
		printf("Case %d: the next triple peak occurs in %d days.\n", ++icase, x);
	}
	return 0;
}


****转载请注明来源: http://blog.youkuaiyun.com/synapse7/article/details/9946013****

POJ 1006 Biorhythms(中国剩余定理,63 ms) 一、题目描述 Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 154722 Accepted: 50271(2020/1/13) Description Some people believe that there are three cycles in a person’s life that sta... 阅读详情

相关推荐

East Central North America 1999

这是East Central North America 1999的比赛题目 及题解,内含测试数据

@总结 - 1@ 多项式乘法 —— FFT

目录 @0 - 参考资料@ @1 - 一些概念@ @2 - 傅里叶正变换@ @3 - 傅里叶逆变换@ @4 - 迭代实现 FFT@ @5 - 参考代码实现@ @6 - 快速数论变换 NTT@ @7 - 任意模数 NTT@ ...

weixin_30360497的博客 528

【STM32】从零构建串口IAP框架:BootLoader与App双向跳转实战解析

本文详细解析了STM32串口IAP框架的构建,重点阐述了BootLoader与App双向跳转的实战原理与实现步骤。内容涵盖Flash分区规划、中断向量表重定向、通信协议设计及固件升级流程,并针对跳转失败、程序跑飞等常见问题提供了解决方案,帮助开发者掌握产品级远程升级的核心技术。

wind6的博客 838

POJ1006(中国剩余定律)

题意:已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n 。 解题思路:模板题 tp=((tp-d)%M+M)%M;讲一下意义吧。(tp-d)%M距离d最近的差,再加上一个M,因为(x=x0+k*M) #include<cstdio> #include<cstring> #include<iostream> #define ...

qq_39861441的博客 775

MTK metadata中AF模块的移除与配置调整

本文详细介绍了在MTK平台Camera开发中如何移除metadata中的AF(自动对焦)模块。通过系统清理MTK_CONTROL_AF_REGIONS、MTK_CONTROL_MAX_REGIONS等关键配置项,并调整对焦模式及硬件特性声明,确保定制化设备在禁用自动对焦功能后的稳定运行。

q1w3e5r7t9y的博客 570

无线Mesh组网全攻略:从原理、部署到调优与排障

对于布线困难的大户型、复式楼或园区,传统的无线中继往往面临带宽减半和链路不稳定的问题。无线Mesh网络通过多跳转发与自组织路由,让每个节点无需网线即可协作扩展覆盖,并依靠动态路径选择实现自愈。其核心技术涉及802.11s、HWMP路由协议,以及802.11k/v/r协同的无缝漫游,能有效提升终端切换体验。在实际部署中,设备选型、回程频段规划与布点位置直接决定网络质量。围绕无线Mesh的组网原理、设备选型、配置调优与故障排查,提供一套工程实践参考,帮助用户在复杂环境下构建稳定高效的无缝覆盖。

weixin_34098209的博客 272

51nod1348 乘积之和 分治+NTT+中国剩余定理

Description 给出由N个正整数组成的数组A,有Q次查询,每个查询包含一个整数K,从数组A中任选K个(K &lt;= N)把他们乘在一起得到一个乘积。求所有不同的方案得到的乘积之和,由于结果巨大,输出Mod 100003的结果即可。例如:1 2 3,从中任选1个共3种方法,{1} {2} {3},和为6。从中任选2个共3种方法,{1 2} {1 3} {2 3},和为2 + 3 + 6 ...

olahiuj的博客 402

半导体测试(一)

探针台是一种用于在晶圆上进行配合探针卡进行电路量测的设备,在测试过程中,探针工作站会将探针与晶圆上的每个die进行接触,以进行电性测量。在这个过程中,每个die都被测试以确保它能基本满足器件的特征或设计规格书(Specification),通常包括电压、电流、时序和功能的验证。晶圆是一个圆形的硅片,在这个半导体的基础之上,建立了许多独立的单个的电路;在所有的die都被探测(Probed)之后,晶圆被切割成独立的dice,这就是常说的die saw,所有被标示为失效的die都被报废,。

m0_37606066的博客 8431

POJ1006 Biorhythms —— 中国剩余定理

题目链接:https://vjudge.net/problem/POJ-1006 Biorhythms Time Limit:1000MS Memory Limit:10000K Total Submissions:141576 Accepted:45491 Description Some people b...

weixin_30716725的博客 122

数码管动态扫描避坑指南:如何用188芯片实现50Hz无闪烁显示?

本文深入解析188数码管动态扫描技术,提供工业级无闪烁显示方案。通过优化轮询扫描机制、硬件电路设计和软件架构,实现50Hz高刷新率显示,解决亮度不均和鬼影问题,适用于医疗设备和工业控制面板等高要求场景。

hhh00的博客 975

【知识分享】IBIS 建模指南 04

文章详细阐述了将缓冲器数据转换为IBIS模型格式的核心步骤与规范要求。主要内容包括:1)IBIS文件三大结构(文件头信息、组件引脚映射、缓冲器行为描述)及其关键词定义;2)四种关键I-V表(上下拉、电源/地钳位)的数据处理规则,强调需避免重复计算效应;3)切换特性描述方法,指出V-T波形表优于Ramp数据的三大优势;4)差分缓冲器通过[DiffPin]关键词实现建模的特殊流程。文中特别强调数据外推校验、参考电压基准转换、工艺角数据排序等易错环节的处理技巧,为创建符合IBIS 4.0规范的精确模型提供指导。

一只豌豆象的博客 2054

POJ1006 Biorhythms 题解

孙子定理 中国剩余定理 中国余数定理 POJ1006题解 Biorhythms题解

Nothing gonna but our heart change 1243

【ARFoundation自学04】AR Tracked Image 图像追踪识别与对应类的调用

1.添加组件 2.创建图像识别库 3.创建识别后追踪的物体(UI、模型等)

leoysq的博客 2560

POJ-1006 Biorhythms 中国剩余定理

POJ-1006 Biorhythms 文章目录POJ-1006 Biorhythms思路:推导细节AC代码 由于题目过于冗长,在此只描述大致题意:人出生就伴随:体力,情感和智力三个生理周期,他们的周期分别为23天,28天,33天,每个周期内有一天为该阶段的峰值,这一天人在对应的方面的表现的最好。一般情况下,这三天不会在同一天,现在分别给出三个日期,分别对应体力§,情感(e),智力(i)出现峰值的日期,以及一个起始日期(d),问:从起始日期开始,最少经过多少天三个方面同时到达峰值。 思路: 关于中国剩余

碳碳双键 212

【软考之软件设计师】学习笔记

interface 接口implements 实现接口(与接口配套使用)接口里要是有方法定义,子类就会实现。

云轩阁 2533

jre9下载 jdk9

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

poj 1006 Biorhythms (中国剩余定理模板)

http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同...

deyou1988的专栏 273

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

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

Biorhythms(中国剩余定理)

http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/这篇博客写的很棒!登录后复制 #include<stdio.h> #include<string.h> #include<stdlib.h> ...

weixin_40245000的博客 97

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

POJ 1006 Biorhythms 生理周期 中国剩余定理

链接 : POJ 1006 Biorhythms 中文: POJ 1006 生理周期我通过 ζёСяêτ - 小優YoU 的博客学习, 不再赘述。我的代码:#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std;int main() { int p, e, i

QER 的博客 605

poj 1006 Biorhythms 生理周期

问题 Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 102540   Accepted: 31642 Description Some people believe that there are three cycles in a pers

u011422119的专栏 590

Poj1006 Biorhythms 【中国剩余定理】

Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods ...

qq_28640193的博客 427

POJ 1006 - Biorhythms (中国剩余定理)

B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1006 Description 人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一

Ritchie 306

POJ 1006 Biorhythms【中国剩余定理】

Description Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and the

Joeferyの博客 445

POJ1006 Biorhythms(生理周期,中国剩余定理详述)

Description   人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出

SimonCoder的专栏 2379

【Poj 1006】生理周期Biorhythms--CRT

原题参见 http://poj.org/problem?id=1006 其实主要研究一是因为最近看书看到三个周期变换突然想起来这个题,再就是对于CRT例题稍作整理,日后再深入理解研究下,这里先暂时摆一摆网上大神的解法,内容部分感谢这里的一些分析问题描述 人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天。一个周期内有一天为峰值,在这一天,人在对应的方面(体力,情感或智力)表现

Loi_Chairmanの博客 698
上一篇: POJ 2992 Divisors(质因数计数公式)
下一篇: Codeforces Beta Round #1 / 1A Theatre Square (简单数学)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值