Catch its root (hdu2710 Max Factor Prime Greatest common divisor least common multiple ... )

Source: Internet
Author: User
Tags greatest common divisor

Prime judgment:

One, according to the definition of primes, the number in addition to 1 and itself no longer have other factors.

See the code.

1 intPrime ()2 {3      for(intI=2; i*i<=n; i++)4     {5         if(n%i==0)//not Prime6             return 1;//returns 17     }8     return 0;//is the prime number to return 09}

Second, hit the table, all the prime numbers are listed, there is an array.

See the code.

1 voidPrime ()2 {3      for(intI=2; i<20050; i++)//starting from 2, one after another.4     {5         if(hash[i]==0)//This judgment can reduce a lot of duplication and save a lot of time6         {7              for(intj=2; i*j<20050; J + +)//It's not a prime number if I multiply it .8             {9hash[i*j]=1;//not a prime number labeled 1Ten             } One         } A     } -}

As an example: hdu2710 Max Factor

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2710

Max Factor

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4168 Accepted Submission (s): 1366


Problem Descriptionto improve the organization of his farm, Farmer John labels each of his n (1 <= n <= 5,000) cows With a distinct serial number in the range 1..20,000. Unfortunately, he is unaware, the cows interpret some serial numbers as better than others. In particular, a cow whose serial number have the highest prime factor enjoys the highest social standing among all the oth ER cows.

(Recall that a prime number is just a number, the has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, was not).

Given a set of n (1 <= n <= 5,000) serial numbers in the range 1..20,000, determine the one, and the largest PRI Me factor.

input* Line 1: A single integer, N

* Lines 2..n+1:the serial numbers to being tested, one per line

output* line 1:the Integer with the largest prime factor. If there is more than one, the output of the one that is appears earliest in the input file.

Sample Input436384042 Sample Output38

The main topic: find the maximum factor of the given number, and then in the comparison of the size of these factors, find the largest, the final output of the original number.

See the code.

1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 inthash[20050];8 9 voidPrime ()Ten { One      for(intI=2; i<20050; i++) A     { -         if(hash[i]==0) -         { the              for(intj=1; i*j<20050; J + +) -             { -Hash[i*j]=i;//I represents the largest factor of the element -             } +         } -     } +     //return hash[n]; A } at  - intMain () - { -     intT; -memset (Hash,0,sizeof(hash)); - Sushu (); in      while(~SCANF ("%d",&T)) -     { to         intmax=0, x=1; +          while(t--) -         { the             intN; *scanf"%d",&n); $             if(hash[n]>Max)Panax Notoginseng             { -max=Hash[n]; thex=N; +             } A         } theprintf ("%d\n", x); +     } -     return 0; $}

Greatest common divisor (GCD)

See the code.

1 int gcd (int A,int  b)  2{  3     return A%B?GCD (b,a%b): b;   4

Least common multiple

To solve least common multiple, it is generally to use greatest common divisor. The greatest common divisor is calculated by dividing the sum of two numbers, and then the greatest common divisor is least common multiple.

ATTENTION BASIC!!!

Catch its root (hdu2710 Max Factor Prime Greatest common divisor least common multiple ... )

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.