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

题目链接:

**题意:**人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天。一个周期内有一天为峰值,在这一

天,人在对应的方面(体力,情感或智力)表现最好。通常这三个周期的峰值不会是同一天。现在给出三个日

期,分别对应于体力,情感,智力出现峰值的日期。然后再给出一个起始日期,要求从这一天开始,算出最少

再过多少天后三个峰值同时出现。

思路:解一个线性同余方程。crt的模板题。

关于crt的讲解:中国剩余定理学习笔记

几年前就A过了,现在重新写题解复习一下。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :Thu 13 Oct 2016 08:00:04 PM CST
 4File Name :code/poj/1006.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int p,e,i,d;
34int a[5],m[5];
35void exgcd(int a,int b,int &x,int &y)
36{
37    if (b==0)
38    {
39	x = 1;
40	y = 0;
41	return;
42    }
43    exgcd(b,a%b,x,y);
44    int tmp = x;
45    x = y;
46    y = tmp - a/b*y;
47}
48int crt(int a[],int m[],int n)
49{
50    int M = 1;
51    int ans = 0 ;
52    for ( int i = 1 ; i <=  n; i++) M*=m[i];
53
54    for ( int i = 1 ; i <= n ; i++)
55    {
56	int x,y;
57	int Mi = M/m[i];
58	exgcd(Mi,m[i],x,y);
59	ans = ( ans + Mi * x * a[i])%M;
60    }
61    if (ans<0) ans+=M;
62    return ans;
63}
64int main()
65{
66	#ifndef  ONLINE_JUDGE 
67	freopen("code/in.txt","r",stdin);
68  #endif
69
70	int cas = 0 ;
71	m[1] = 23;
72	m[2] = 28;
73	m[3] = 33;
74	int T = 21252;
75	while (~scanf("%d%d%d%d",&p,&e,&i,&d))
76	{
77	    if (p==-1&&e==-1&&i==-1&&d==-1) break;
78	    a[1] = p;
79	    a[2] = e;
80	    a[3] = i;
81	    int ans = crt(a,m,3);
82	    if (ans-d<=0) ans += T; 
83	    printf("Case %d: the next triple peak occurs in %d days.\n",++cas,ans-d);
84	}
85
86  #ifndef ONLINE_JUDGE  
87  fclose(stdin);
88  #endif
89    return 0;
90}