本篇博客就不具体讲解中国剩余定理。想要具体了解,弄懂中国剩余定理,推荐以下博客,耐心看:
https://www.cnblogs.com/kindleheart/p/8877548.html
https://blog.youkuaiyun.com/d_x_d/article/details/48466957
题目:Biorhythms
链接:http://poj.org/problem?id=1006
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define mod 21252
void Extend_gcd(int a, int b, int &x, int &y){//扩展欧几里得算法
if(!b){
x = 1;
y = 0;
return;
}
Extend_gcd(b, a%b, x, y);
int k = x;
x = y;
y = k - a/b*y;
}
int CRT(int a[], int b[], int n){//中国剩余定理
int ans = 0, x, y, M = 1, N;
for(int i = 0; i < n; i++){
M *= a[i];
}
for(int i = 0; i < n; i++){
N = M / a[i];
Extend_gcd(N,a[i], x, y);
ans = (ans + N * x * b[i]) % mod;
}
return ans;
}
int main(){
int a[3] = {23,28,33}, b[15];
int d, cn = 0;
while(scanf("%d %d %d %d", &b[0], &b[1], &b[2], &d) && d != -1){
int k = CRT(a, b, 3)-d;
cn++;
if(k <= 0)k += mod;
printf("Case %d: the next triple peak occurs in %d days.\n", cn, k);
}
}