HDU1788 Chinese remainder theorem again 最小公倍数

来源:互联网 发布:淘宝流量和访客怎么刷 编辑:程序博客网 时间:2024/05/22 07:06

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1788



分析:由n%m=m-a,==>n%m+a=m,即,n+a≡0(mod m),其实(n+a)就是m的倍数,那么问题就变为了找出∑mi的最小公倍数,再减去a了。


实现代码如下:

#include <iostream>#include <cstdio>using namespace std;typedef long long LL;LL gcd(LL a,LL b){    return !b?a:gcd(b,a%b);}int main(){    int n,k,t;    while(cin>>n>>k)    {        if(n==0&&k==0) break;        LL ans=1;        for(int i=0;i<n;i++)        {            cin>>t;            ans=t/gcd(ans,t)*ans;        }        cout<<ans-k<<endl;    }    return 0;}


0 0