POJ1006 中国剩余定理

定义:


若$m_1,m_2,m_3,\cdots,m_n$是两两互素的正整数,则同余方程组
$\begin{array}{c}
     x\equiv a_1( mod\,m_1) \\
     x\equiv a_2( mod\,m_2) \\
     x\equiv a_3( mod\,m_3) \\
\cdots\cdots\cdots\cdots \\
     x\equiv a_n( mod\,m_n) \\
\end{array}$
有模$M=m_1m_2m_3\cdots m_n$的唯一解,即为中国剩余定理。解为
$x\equiv (a_1M_1M_1^{-1}+a_2M_2M_2^{-1}+\cdots +a_nM_nM_n^{-1})mod\,M$
其中$M_i=\frac{M}{m_i}$,而$M_i^ {-1}$是$M_i$的逆元。


POJ1006 Biorhythms

给你三个周期分别是23,28,33(互质),并且峰值不是一天,给出起始的时间,求出再过最少多少天后三个峰值同时出现。

#include "iostream"
 #include "stdio.h"
 using namespace std;
int s[] = {23,28,33};
 void ext_gcd(int a, int b, int &d, int& x, int& y) {
     if (!b){x = 1; y = 0; d = a;}
     else {
         ext_gcd(b, a%b, d, y, x);
         y -= x*(a/b);
     }
 }
int CRT(int a[],int m[],int n) {
     int M = 1;  
     int ans = 0;  
     for(int i = 0; i < n; i++)  M *= m[i];  
     for(int i = 0; i < n; i++) {  
         int x, y, d;  
         int Mi = M/m[i];  
         ext_gcd(Mi, m[i], d, x, y);  
         ans = (ans + Mi *x*a[i]) % M;  
     }  
     if(ans < 0) ans += M;  
     return ans;  
 }  
int main(int argc, char const *argv[])
 {
     int Kcase = 0;
     int a[5];
     while (scanf("%d%d%d%d", &a[0], &a[1], &a[2], &a[3]) != EOF) {
         if (a[0]==-1&&a[1]==-1&&a[2]==-1&&a[3]==-1) break;
         int ans = CRT(a, s, 3);
         if (ans <=a[3]) ans += 21252;
         printf("Case %d: the next triple peak occurs in %d days.\n", ++Kcase, ans - a[3]);
     }
     return 0;
 }

转载于:https://www.cnblogs.com/cniwoq/p/7265906.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值