HDU 1019 Least Common Multiple (least common multiple _ water problem)

Source: Internet
Author: User
Tags greatest common divisor

Problem DescriptionThe Least common multiple (LCM) of a set of positive integers are the smallest positive integer which is Divisible by all the numbers in the set. For example, the LCM of 5, 7 and 105.


Inputinput would consist of multiple problem instances. The first line of the input would contain a single integer indicating the number of problem instances. Each instance would consist of a single line of the form M N1 n2 n3 ... nm where m is the number of integers in the set and N1. NM is the integers. All integers would be positive and lie within the range of a 32-bit integer.

Outputfor each problem instance, output a single line containing the corresponding LCM. All results would lie in the range of a 32-bit integer.

Sample Input
23 5 7 156 4 10296 936 1287 792 1

Sample Output
10510296

int gcd (int a,int b)//greatest common divisor template
{
if (a<b) return gcd (B,a);
int R;
while (b) {r=a%b;a=b;b=r;}
return A;
}


int LCM (int a,int b) {return a/gcd (b) *b;} Least common multiple templates

This is titled Template title ...

#include <iostream> #include <cmath> #include <cstdio>using namespace std;int gcd (int a,int b) {if (a <B)    return gcd (b,a); int r; while (b) {r=a%b;a=b;b=r;} return A;} int LCM (int a,int b) {return a/gcd (b) *b;} int main () {int t,n,i,j,num,sum;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&sum); for (i=2;i<=n;i++) { scanf ("%d", &num); if (sum%num) {SUM=LCM (sum,num);}} printf ("%d\n", sum);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1019 Least Common Multiple (least common multiple _ water problem)

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.