hdu 1788 Chinese remainder theorem again 多个数的最小公倍数

来源:互联网 发布:网络视频直播软件 编辑:程序博客网 时间:2024/05/18 02:49

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

说明: 题目很水 提到一个定理,求多个数的最小公倍数转化为求两个数的最小公倍数方法。 然后就是  a=b(mod m1)  a=b(mod m2)  等价于a=b(mod [m1,m2] )

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



原创粉丝点击