HDU1788 Chinese remainder theorem again【中国剩余定理】

HDU1788 Chinese remainder theorem again【中国剩余定理】

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1788


题目大意:

题眼下边的描写叙述是多余的。。。

一个正整N除以M1余M1-a,除以M2余M2-a。除以M3余M3-a。

即除以Mi余Mi-a(a < Mi < 100),求满足条件的最小的数。


思路:

这是一道中国剩余定理的基础题。由题目得出N % Mi + a = Mi,即得:N + a = 0(mod Mi)。也

就是全部的Mi都能整除N+a。

那么题目就变为了求N个Mi的最小公倍数。最后再减去a。


AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define LL __int64
using namespace std;

LL GCD(LL a,LL b)
{
    if(b == 0)
        return a;
    return GCD(b,a%b);
}

int main()
{
    int N,K,m;
    while(cin >> N >> K && (N||K))
    {
        LL ans = 1;
        for(int i = 0; i < N; ++i)
        {
            cin >> m;
            ans = (ans*m)/GCD(ans,m);
        }
        cout << ans - K << endl;
    }

    return 0;
}