POJ 1006 中国剩余定理

【题意】:

给定p,e,i,d,求解

(x + d) % 23 = p 
(x + d) % 28 = e
(x + d) % 33 = i

x最小正整数值

【知识点】:

中国剩余定理

【题解】:

典型的 xmodmi = ai模型,其中mi间两两互素。但该题式子较少,也可以直接自己化简带入值。

【代码】:

 1 #include <map>
 2 #include <set>
 3 #include <cmath>
 4 #include <ctime>
 5 #include <queue>
 6 #include <stack>
 7 #include <cstdio>
 8 #include <string>
 9 #include <vector>
10 #include <cstring>
11 #include <sstream>
12 #include <iostream>
13 #include <algorithm>
14 #include <bitset>
15 #include <climits>
16 #include <complex>
17 using namespace std;
18
19 #define wh while
20 #define inf (int)(~0u/2)
21 #define FOR(i, n) for(int i = 0; i < n; i++)
22 #define FOR1(i, n) for(int i = 1; i < n; i++)
23 #define FOR2(i, n) for(int i = 0; i <= n; i++)
24 #define REP(i,n) for(int i = 1; i <= n; i++)
25 #define FORI(it,n) for(typeof(n.begin()) it = n.begin(); it != n.end(); it++)
26 #define sf scanf
27 #define pf printf
28 #define frs first
29 #define sec second
30 #define psh push_back
31 #define mkp make_pair
32 #define PB(x) push_back(x)
33 #define MP(x, y) make_pair(x, y)
34 #define clr(abc,z) memset(abc,z,sizeof(abc))
35 #define lt(v) v << 1
36 #define rt(v) v << 1 | 1
37 #define mid ((l + r) >> 1)
38 #define lson l, mid, v << 1
39 #define rson mid + 1, r, v << 1 | 1
40 #define fre freopen("1.txt", "r", stdin)
41
42 typedef long long LL;
43 typedef long double LD;
44
45 LL exgcd(LL a, LL b, LL& x, LL& y){
46     if(b == 0){
47         x = 1; y = 0; return a;
48     }
49     else{
50         LL r = exgcd(b, a % b, y, x);
51         y -= x * (a / b);
52         return r;
53     }
54 }
55
56 int chinese(int a[], int m[], int n){
57     int M = 1, ret = 0;
58     for(int i = 0; i < n; i++) M *= m[i];
59     for(int i = 0; i < n; i++){
60         LL x, y;
61         int ta = M / m[i], tb = m[i];
62         exgcd(ta, tb, x, y);
63         ret = (ret + ta * x * a[i]) % M;
64     }
65     return (ret + M) % M;
66 }
67
68 int main(){
69     int p, e, i, d, it = 0;
70     while(sf("%d%d%d%d", &p, &e, &i, &d) != EOF){
71         if(p == -1 && e == -1 && i == -1 && d == -1) break;
72         int a[] = {p, e, i}, m[] = {23, 28, 33};
73         int ans = chinese(a, m, 3);
74         ans = (ans - d + 21252) % 21252;
75         if(ans == 0) ans = 21252;
76         pf("Case %d: the next triple peak occurs in %d days.\n", ++it, ans);
77     }
78 }

POJ 1006 中国剩余定理,布布扣,bubuko.com

时间: 2024-08-05 11:12:39

POJ 1006 中国剩余定理的相关文章

转载----POJ 1006 中国剩余定理

本文为转载,源地址:   http://blog.csdn.net/dongfengkuayue/article/details/6461298 POJ 1006   Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78980   Accepted: 23740 Description Some people believe that there are three cycles in a perso

Biorhythms POJ - 1006 中国剩余定理

定理证明:https://blog.csdn.net/d_x_d/article/details/48466957 https://blog.csdn.net/lyy289065406/article/details/6648551 关键思路在于构造一个每部分膜另外一个不包含在部分内的部分的余数是1  然后把各部分分别乘以对应的余数 其中题目给出的第一天天数  就是  (n+d)%xn 的余数 不用处理 1 2 #include<iostream> 3 using namespace std;

poj 1006(剩余定理)

生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111426   Accepted: 34702 Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易高度集中.因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天.对于每个人,我们想知道

poj 2891 中国剩余定理

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 20640   Accepted: 6946 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

solution for 1006 中国剩余定理

一.要求 http://poj.org/problem?id=1006 Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 124113   Accepted: 39154 Description Some people believe that there are three cycles in a person's life that start the day he or she is born.

POJ 1006:Biorhythms 中国剩余定理

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 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,

acm数论之旅--中国剩余定理

中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这个答案 5*7*inv(5*7,  3) % 3  =  1 3*7*inv(3*7,  5) % 5  =  1 3*5*inv(3*5,  7) % 7  =  1 这3个式子对不对,别告诉我逆元你忘了(*′?`*),忘了的人请翻阅前几章复习 然后两边同乘你需要的数 2 * 5*7*inv(5*

数论E - Biorhythms(中国剩余定理,一水)

E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status 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

gcd,扩展欧几里得,中国剩余定理

1.gcd: int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题:有一正整数ans,对于每一对数,都有:(ans-a[i])mod m[i]=0.求此数最小为多少. 输入样例: 1 10 2 3 1 2 3 2 3 5 8 1 2 3 4 5 6 7 8 97 89 67 61 59 53 47 88 12 1 2 3 4 5 6 7 8 9