[POJ1006]Biorhythms(中国剩余定理)

11 篇文章 0 订阅
11 篇文章 0 订阅

题目描述

传送门

题解

中国剩余定理模板题
注意统计ni的时候如果想用y来统计那么应该是1-mi*y,因为用扩欧求的是不带符号的

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

int Case,N,M,d,a,b,x,y,n[5],m[5],c[5];

void exgcd(int a,int b,int &x,int &y)
{
    if (!b) x=1,y=0;
    else exgcd(b,a%b,y,x),y-=a/b*x;
}
int main()
{
    m[1]=23;m[2]=28;m[3]=33;M=m[1]*m[2]*m[3];
    while (~scanf("%d%d%d%d",&c[1],&c[2],&c[3],&d))
    {
        if (c[1]==-1&&c[2]==-1&&c[3]==-1&&d==-1) return 0;
        for (int i=1;i<=3;++i)
        {
            a=M/m[i];b=m[i];
            exgcd(a,b,x,y); 
            x=((x%b)+b)%b;
            if (!x) x+=b;
            n[i]=a*x;           
        }
        N=0;
        for (int i=1;i<=3;++i)
            N+=n[i]*c[i];
        N-=d;
        N=(N%M+M)%M;
        if (!N) N+=M;
        printf("Case %d: the next triple peak occurs in %d days.\n",++Case,N);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值