HDU 1019 (GCD and LMC)

Source: Internet
Author: User
Tags gcd greatest common divisor

Test instructions gives you a bunch of numbers and asks you to find them LMC (least common multiple). The first two are least common multiple equals they multiply and then one of their gcd (greatest common divisor), that three number of the largest common multiple how to ask? Obviously can't do as before, now give a theorem: If the number of least common multiple known n, then add a number of M, then this n+1 number of least common multiple equals the first n number of least common multiple and the newly added number m least common multiple. The problem that we need to solve now is only GCD left. How does the GCD beg?

GCD recursion theorem: For any nonnegative integer A and any positive integer B,GCD (A, b) = gcd (b, a mod b). GCD (b,a mod b) ==b can be easily found when a mod is b==0. So all I have to do is fix it.

#include <stdio.h>
#include <cstring>
#include <iostream>
using namespace Std;
int gcd (int a, int b)
{
Return (b==0) A:GCD (b,a%b);
}
int LCM (int a,int b)
{
Return A/GCD (A, b) *b;
}
int a[1000010];
int main ()
{
int t,n;
cin>>t;
while (t--)
{
cin>>n;
int lcm=1;
cin>>a[0];
LCM=A[0];
for (int i=1;i<n;i++)
{
cin>>a[i];
LCM=LCM (Lcm,a[i]);
}
cout<<lcm<<endl;;

}

return 0;
}

HDU 1019 (GCD and LMC)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.