生理周期[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 ...
随机推荐
- Mysql 调用存储过程的两种方式
一,使用call语句: 如:创建 call 调用: 使用占位符,通过prepare,execute调用:
- android viewPager 切换页面时防止fragment重新加载
把限制页面数设置成应用一共的页面数就可以了 ViewPager.setOffscreenPageLimit(3);
- Index on DB2 for z/OS: DB2 for z/OS 的索引
可以创建在任何表上的索引: Unique Index:An index that ensures that the value in a particular column or set of col ...
- UISegmentedControl
1. NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3& ...
- sql server 的cpu使用率过高的分析
有哪些SQL语句会导致CPU过高? 1.编译和重编译 编译是 Sql Server 为指令生成执行计划的过程.Sql Server 要分析指令要做的事情,分析它所要访问的表格结构,也就是生成执行计划的 ...
- 【转载】PHP使用1个crontab管理多个crontab任务
转载来自: http://www.cnblogs.com/showker/archive/2013/09/01/3294279.html http://www.binarytides.com/php- ...
- 2014百度之星资格赛 1004:Labyrinth(DP)
Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 攻城狮在路上(叁)Linux(二十五)--- linux内存交换空间(swap)的构建
swap的功能是应付物理内存不足的状况,用硬盘来暂时放置内存中的信息. 对于一般主机,物理内存都差不多够用,所以也就不会用到swap,但是对于服务器而言,当遇到大量网络请求时或许就会用到. 当swap ...
- 攻城狮在路上(壹) Hibernate(十一)--- 映射实体关联关系
本文以Customer和Address类的关系为例说明一对一关联映射:以Category和Item类的关系说明多对多关联关系.一.映射一对一关联: 分两种情况:按照外键映射和按照主键映射.这两种方式的 ...
- mac下php开发环境搭建+CI框架使用
一.启动apache: apachectl start 停止: apachectl stop 配置文件: vi /etc/apache2/httpd.conf 一.修改端口 因为80端口不想被占用,8 ...