• hdu 1370 中国剩余定理


    题意 :求一个最小的数对 23 38 33 于i p e

    zsd: 因为23 38 33 两两互素所以可以用中国剩余定理

    是中共剩余定理的经典模板

    #include <iostream>  
    using namespace std;  
    int exgcd(int a,int b,int &x,int &y)  
    {  
        if(a==0)  
        {  
            x=0;  
            y=1;  
            return b;  
        }  
        int g = exgcd(b%a,a,x,y);  
        int tem = y;  
        y=x;  
        x=tem-(b/a)*y;  
        return g;  
    }  
    int inv(int a,int n)//求逆元  
    {  
        int x,y;  
        exgcd(a,n,x,y);  
        return (x%n+n)%n;  
    }  
    int main()  
    {  
        int N,P,E,I,D,ans;  
        const int m1=23,m2=28,m3=33,M1=28*33,M2=23*33,M3=23*28,m=23*28*33;  
        const int M11 = inv(M1,m1),M22 = inv(M2,m2),M33 = inv(M3,m3);//求mi的逆元   
        scanf("%d",&N);  
        while(scanf("%d%d%d%d",&P,&E,&I,&D)!=EOF)  
        {  
            if(P==-1&&E==-1&&I==-1&&D==-1)break;  
            //a[i]*(mi*mi(逆)mod ni)因为在求逆元的时候已经mod ni了   
            //(mi*mi(逆)mod ni)是ci   
            ans = (P*M1*M11 + E*M2*M22 + I*M3*M33)%m;  
            ans -= D;  
            if(ans<=0) ans+=m;  
            printf("Case %d: the next triple peak occurs in %d days.
    ",N++,ans);  
            //cout<<"Case "<<N++<<": the next triple peak occurs in "<<i<<" days.
    ";  
        }  
        return 0;  
    }  
  • 相关阅读:
    Linux的学习--系统目录
    PHP内核的学习--创建PHP扩展
    PHP的学习--连接MySQL的三种方式
    MIME Type
    颜色的命名
    JavaScript的学习--生成二维码
    MySQL的学习--触发器
    Google Guava之--cache
    java代码调用oracle存储过程
    oracle序列
  • 原文地址:https://www.cnblogs.com/zhangdashuai/p/3702236.html
Copyright © 2020-2023  润新知