生理周期[PKU1006]
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 132195 | Accepted: 42171 |
Description
Input
当p = e = i = d = -1时,输入数据结束。
Output
采用以下格式:
Case 1: the next triple peak occurs in 1234 days.
注意:即使结果是1天,也使用复数形式“days”。
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. 可以很简单,直接数50000个就行。也可以更一般的用线性同余方程组。
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
class Congruent_Linear_Equation_Group {
private:
#define MAX_GROUP_SIZE 10
int groupSize, dis;
int A[MAX_GROUP_SIZE], M[MAX_GROUP_SIZE];
int gcd(int a, int b) {
int k;
while (b != ) {
k = b;
b = a % b;
a = k;
}
return a;
}
/* * * * * * * * * *
* a,b=>x,y *
* ax+by=gcd(a,b) *
* * * * * * * * * */
int extended_gcd(int a, int b, int &x, int &y) {
int ans, t;
if (b == ) {
x = ;
y = ;
return a;
} else {
ans = extended_gcd(b, a % b, x, y);
t = x;
x = y;
y = t - (a / b) * y;
}
return ans;
}
public:
Congruent_Linear_Equation_Group() {
groupSize = ;
}
void clear() {
groupSize = ;
}
void addEquation(int a, int m) {
if (m == ) {
return;
}
A[groupSize] = a;
M[groupSize++] = m;
}
int getSolution() {
int at = A[], mt = M[], ap, mp, x, y, z;
for (int i = ; i < groupSize; i++) {
ap = A[i];
mp = M[i];
if ((at - ap) % gcd(mt, mp) != ) {
return -;
}
/* * * * * * * * * * * * *
* x=mt*y+at *
* x=mp*z+ap *
* => at-ap=mp*z-mt*y *
* * * * * * * * * * * * */
extended_gcd(mp, - * mt, z, y);
// => mp*z-mt*y=gcd(mp,-mt)
z = z * (at - ap) / gcd(mp, -mt);
x = mp * z + ap;
mt = mt * mp / gcd(mt, mp);
while (x <= ) {
x += mt;
}
at = x;
}
dis = mt;
return x;
}
int getNextSolution(int x) {
return x + dis;
}
};
Congruent_Linear_Equation_Group cc;
int main() {
int a, b, c, d, i = ;
while (scanf("%d%d%d%d", &a, &b, &c, &d) != EOF) {
if (a == - && b == - && c == - && d == -) {
break;
}
i++;
cc.clear();
a %= ;
b %= ;
c %= ;
cc.addEquation(a, );
cc.addEquation(b, );
cc.addEquation(c, );
int x = cc.getSolution();
if (x <= d) {
x += ;
}
while (x->d) x-=;
printf("Case %d: the next triple peak occurs in %d days.\n", i, x - d);
}
return ;
}
生理周期[PKU1006]的更多相关文章
- 生理周期(c++实现)
描述:人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23 天. 28 天和33 天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维 ...
- ACM第二题 生理周期
人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易 ...
- [POJ 1006]生理周期
Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰 ...
- 【同余方程组】POJ1006 生理周期
同余方程组: 先来看一道题目:有物不知其数,三三数之剩二:五五数之剩三:七七数之剩二.问物几何? 然后我们可以做如下变换,设x为所求的数. x%3=2 x ≡ a1(%m1 ...
- OpenJ_Bailian 4148 生理周期
生理周期 OpenJ_Bailian - 4148 Time limit1000 ms Memory limit65536 kB OS Linux SourceEast Central North A ...
- 生理周期POJ 1006
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138101 Accepted: 44225 Description 人生 ...
- POJ 1006 生理周期(中国剩余定理)
POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...
- C++基础算法学习——生理周期
人有体力.情商.智商的高峰日子,它们分别每隔23天.28天和33天出现一次.对于每个人,我们想知道何时三个高峰落在同一天.给定三个高峰出现的日子p,e和i(不一定是第一次高峰出现的日子),再给定另一个 ...
- poj1006 生理周期
生理周期 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 138947 Accepted: 44597 Descripti ...
随机推荐
- jQuery – 8.事件和事件参数
事件 (*)JQuery中的事件绑定:$("#btn").bind("click",function(){}),每次都这么调用太麻烦,所以jQuery可 ...
- MVC – 15.路由机制
15.1.路由检测插件 - RouteDebug 15.2.路由约束 15.3.命名路由 15.4.验证码 15.5.ASP.NET MVC 与 三层架构 15.6.Area区域 15.6.1.尝试将 ...
- Vs 控件错位 右侧资源管理器文件夹点击也不管用,显示异常
问题:显卡驱动异常. 缘由:驱动精灵万能显卡安装系统 解决方案:根据笔记本型号去官网下载适配显卡驱动.
- 重温WCF之WCF传输安全(十三)(1)前期准备之证书制作(转)
转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/12/2682372.html 一.WCF中的安全方式 说到安全就会涉及到认证,消息一致性和机密性 ...
- async/await 异步编程(转载)
转载地址:http://www.cnblogs.com/teroy/p/4015461.html 前言 最近在学习Web Api框架的时候接触到了async/await,这个特性是.NET 4.5引入 ...
- linux命令**50
1.ls命令 命令格式: ls [选项] [目录名] 命令功能: 列出目标目录中所有的子目录和文件. 常用参数: -a,列出所有文件包括隐藏文件 -l,列出详细信息,文件大小一般以字节大小显示 -h, ...
- Hihicoder 题目1 : Trie树(字典树,经典题)
题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...
- static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别
static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...
- SSIS Dataflow使用存储过程不能检索列名
在项目中遇到一个问题,需要在Dataflow中调用一个存储过程,然后把结果生成一个csv文件. 然而在dataflow调用存储过程中遇到了问题,SP不能正确的返回列名. 在SSMT里面明明是可以查出数 ...
- a个人经验总结2
金额 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8& ...