HDU Least Common Multiple

来源:互联网 发布:异性缘好的男生 知乎 编辑:程序博客网 时间:2024/05/19 10:10

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019
本题应该是唯一的一道一次AC的题目了,使用的数论结论可以参考数论的结论,GCD和LCM以及拓展定理。

#include<stdio.h>#include<string.h>//#define LOCALusing namespace std;int gcd(int a,int b){  //b看做除数    return a%b==0?b:gcd(b,a%b);}int lcm(int a,int b){    return a/gcd(a,b)*b; //防止溢出}int main(){    #ifdef LOCAL    freopen("input.txt","r",stdin);    #endif // LOCAL    int T=0;    scanf("%d",&T);    while(T--){        int N=0;        int result=0;        int pre=1,next=0;        scanf("%d",&N);        for(int i=0;i<N;i++){            scanf("%d",&next);            pre=lcm(pre,next);        }        result=pre;        printf("%d\n",result);    }    return 0;}
0 0
原创粉丝点击