数论(3)中国剩余定理及求解模线性方程组

TQL移步大神讲解
POJ1006
AC:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>

#define ll long long
#define INF 0x3f3f3f3f
#define EPS 1E-10

using namespace std;

void exgcd(int a,int b,int &g,int &x,int &y)
{
    if(!b)
    {
        g=b;    x=1;    y=0;
    }
    else
    {
        exgcd(b,a%b,g,y,x);     y-=x*(a/b);
    }
}
int main()
{
    const int M=23*28*33;
    /*
    int x,y,g;
    exgcd(M/23,23,g,x,y);
    cout<<(x%23+23)%23<<endl;

    exgcd(M/28,28,g,x,y);
    cout<<(x%28+28)%28<<endl;

    exgcd(M/33,33,g,x,y);
    cout<<(x%33+33)%33<<endl;
    */
    int p,e,i,d;
    int k=0;
    while(1)
    {
        scanf("%d%d%d%d",&p,&e,&i,&d);
        if(p==-1 && e==-1 && i==-1 &&d==-1)
            break;
        int s=(M/23*6*p+M/28*19*e+M/33*2*i-d+M)%M;
        if(!s)
            s=21252;
        printf("Case %d: the next triple peak occurs in %d days.\n",++k,s);

    }

    return 0;
}

 
posted @ 2021-04-06 21:28  DuJunlong  阅读(11)  评论(0编辑  收藏  举报  来源