生理周期[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 ...
随机推荐
- ios框架
iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设备的操作系统. 1,Core OS: 是用FreeBSD和Mach所改写的Darwin, 是开源 ...
- NYOJ题目836画图
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsoAAAMbCAIAAADb4SQpAAAgAElEQVR4nO3du1Iq3dYG4H0T5l6IMR
- 防止别人ping自己的服务器
禁止被ping [root@GitLab ~]# echo >/proc/sys/net/ipv4/icmp_echo_ignore_all 无法被ping [root@NB ipv4]# pi ...
- android 5.1 WIFI图标上的感叹号及其解决办法
转自:http://blog.csdn.net/w6980112/article/details/45843129 第一次调试android5.1的 WIFI更改小功能 Wifi 源码的相关路径目录 ...
- sqlplus 中spool命令的简单用法
spool基本格式: spool 路径+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off ...
- 攻城狮在路上(叁)Linux(十五)--- 文件与目录的默认权限与隐藏权限
一.文件默认权限:umask <==需要被减去的权限. 1.umask指的是当前用户在新建文件或者目录时的默认权限,如0022; 2.默认情况下,用户创建文件的最大权限为666; 创建目录的最大 ...
- BOOL in Object-C
BOOL in Object-C typedef signed char BOOL; #define YES (BOOL)1 #define NO (BOOL)0 重构上页中的代码 - (BOOL)s ...
- ASP.NET Identity 3.0教程
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:我相信有些人和我一样,已经开始把ASP.NET 5用于产品开发了.不过现在最大的问题是 ...
- Codeforces Beta Round #95 (Div. 2) D.Subway
题目链接:http://codeforces.com/problemset/problem/131/D 思路: 题目的意思是说给定一个无向图,求图中的顶点到环上顶点的最短距离(有且仅有一个环,并且环上 ...
- ios换肤思想,及工具类
// 实现原理及思路:不同种类的皮肤放在不同的文件夹下,用一个plist文件存放不同控制器下的控件的背景颜色 //plist文件名称为控制器的名称,内部的数据字典的key value对自定义一个命名规 ...