POJ1006 中国剩余定理

简介:

这题用到了中国剩余定理 即有方程 x=p(mod 23) x=e(mod 28) x=i(mod 33)  运用中国剩余定理求x

x-d即为答案 注意边界就可以了

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

void exgcd(long long a,long long b,long long &d,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        d=a;
        return;
    }
    exgcd(b,a%b,d,x,y);
    long long temp=x;
    x=y;
    y=temp-(a/b)*y;
}
long long getniyuan(long long b,long long m)
{
    long long ans,gc,y;
    exgcd(b,m,gc,ans,y);
    return (ans%m+m)%m;
}
int main()
{
    long long e,i,p,d,ans,t=0;
    while(~scanf("%lld%lld%lld%lld",&p,&e,&i,&d))
    {
        if(p==-1&&e==-1&&i==-1&&d==-1)
            break;
        ans=33*28*p*getniyuan(33*28,23)+23*33*e*getniyuan(23*33,28)+28*23*i*getniyuan(28*23,33);
        ans=(ans%21252+21252)%21252;
        ans=(ans-d+21252)%21252;
        if(ans==0)
            ans=21252;
        cout<<"Case "<<++t<<": the next triple peak occurs in "<<ans<<" days."<<endl;
    }
    return 0;
}


目录
相关文章
|
11月前
|
Java C++
poj 1503 高精度加法
把输入的数加起来,输入0表示结束。 先看我Java代码,用BigINteger类很多东西都不需要考虑,比如前导0什么的,很方便。不过java效率低点,平均用时600ms,C/C++可以0ms过。
33 1
|
4月前
Pseudoprime numbers(POJ-3641 快速幂)
Pseudoprime numbers(POJ-3641 快速幂)
|
11月前
poj 1185 炮兵阵地 (状态压缩dp)
如果你是刚刚开始做状态压缩dp,我建议你先看看 poj 3254 Corn Fields 这是一道比这一题更简单,更容易入门的题目。 还有在代码中我用了一个很巧妙的方法求一个数二进制数中1的个数 具体请看我博客中 x& (x - 1)==0 这篇文章 链接 。
36 1
|
网络架构
POJ-1005,I Think I Need a Houseboat(数学题)
POJ-1005,I Think I Need a Houseboat(数学题)
POJ-3641,Pseudoprime numbers(快速幂)
POJ-3641,Pseudoprime numbers(快速幂)
HDU-1370,Biorhythms(中国剩余定理)
本题主要就是应用中国剩余定理。
poj-2909-哥德巴赫猜想
Description For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2 This conjecture has not been proved nor refused yet.
792 0