zoukankan      html  css  js  c++  java
  • POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

    POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

    题意分析

    (1)

    不妨设日期为x,根据题意可以列出日期上的方程:

    (2)

    化简可得:

    (3)

    根据中国剩余定理求解即可。

    代码总览

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    typedef int ll;
    ll p,e,i,d;
    void exgcd(ll a, ll b, ll& d, ll& x, ll &y)
    {
        if(!b){
            d = a,x = 1,y = 0;
        }else{
            exgcd(b, a % b, d, y, x);
            y -= x * (a / b);
        }
    }
    int CRT(int a[], int m[])
    {
        int ans = 0;
        int M = 1;
        int x,y,gcd;
        for(int i = 1; i<=3;++i)M*=m[i];
        for(int i =1 ;i<=3;++i){
            int Mi = M/m[i];
            exgcd(Mi,m[i],gcd,x,y);
            ans = (ans + Mi * a[i] * x) % M;
        }
        if(ans<=0) ans+=M;
        if(ans<d) ans+=M;
        return ans;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int kase = 0;
        while(scanf("%d %d %d %d",&p,&e,&i,&d)){
            if(p == -1 && e == -1 && i == -1 && d == -1) break;
            int a[] = {0,p,e,i};
            int m[] = {0,23,28,33};
            int ans = CRT(a,m);
            printf("Case %d: the next triple peak occurs in %d days.
    ",++kase,ans-d);
        }
        return 0;
    }
    
  • 相关阅读:
    JAVA this
    JAVA static关键字
    子类实例化 super
    TCP/IP概述
    Java多态
    植物大战僵尸:寻找阳光掉落Call调用
    JVM总结+个人整理补充--转
    已知微分方程通解求微分方程
    Redis安装过程中的问题
    对称阵和反对称阵
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367081.html
Copyright © 2011-2022 走看看