zoukankan      html  css  js  c++  java
  • POJ 1006 Biorhythms (中国剩余定理)

    题目链接http://poj.org/problem?id=1006 题目大意:poj有译文~抽象成数学模型就是求解同余方程组: x = p (mod 23) x = e (mod 28) x = i (mod 33)   (中国剩余定理)设m1,m2,……,mr是两两互素的正整数.则同于方程组 x = a1 (mod m1) x = a2 (mod m2) …… x = ar (mod mr) 有模M = m1*m2*……*mr的唯一解.   并且给出了求解的过程: 令Mk = M / mk = m1 m2……mk-1 mk+1……mr ,因为 j != k 时(mj, mk) = 1, 所以可知(Mk, mk) = 1. 并且我们可以求得Mk模mk的一个逆yk,所以Mk yk = 1 (mod mk). 现在构造和x = a1M1y1 + a2M2y2 + …… + arMryr. 此x就是r个同余方程的联立解.并且可以证明联立解模M唯一的(中国剩余定理的详细证明在《初等数论及其应用》P117).  
    #include 
    using namespace std;
    int mo(int a, int b, int c){
        int num = a * b;
        int k = num;
        while(1){
            if (k % c == 1){
                return k;
            }
            k += num;
        }
    }
    int main() {
        int p, e, i, d, day;
        int ca = 0;
        while(cin >> p >> e >> i >> d) {
            if (p == -1 && e == -1 && i == -1 && d == -1){
                return 0;
            }
            p %= 23;
            e %= 28;
            i %= 33;
            int day = (mo(23, 28, 33) * i + mo(23, 33, 28) * e + mo(28, 33, 23) * p) % 21252 - d;
            if (day <= 0){
                day += 21252;
            }
            cout << "Case " << ++ca << ": the next triple peak occurs in " << day << " days.\n";
        }
        return 0;
    }
    
     
    举杯独醉,饮罢飞雪,茫然又一年岁。 ------AbandonZHANG
  • 相关阅读:
    Docker Get Started VI
    Docker Get Started V
    Docker Get Started III
    Docker Get Started IV
    Docker Get Started II
    Docker Get Started I
    贝叶斯公式
    LRU缓存
    二进制中1的个数
    2.准备工作之Gradle
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/4113975.html
Copyright © 2011-2022 走看看