Maximum factor of NYOJ-520

Source: Internet
Author: User

This question basically involves two knowledge points. One prime number embedding method is used to evaluate the prime number, and the other is the most common factor. However, to determine the position of the maximum prime number in the prime number table, the idea of binary is used, otherwise it will time out. The following is the implementation of the specific code;

1 # include <stdio. h> 2 # include <stdlib. h> 3 # define size 1000020 4 int prime [size]; // to save the prime number, starting from subscript 1 to save the first 5 Int temp [size]; // This is the prime number 6 int maxprimefactor (int n) obtained by the prime number embedding method. // function 7 {8 int ans; 9 for (INT I = 2; I * I <= N; I ++) 10 {11 while (N % I = 0) 12 {13 ans = I; // if the remainder can be obtained, then the current I is a prime factor 14 N/= I; // reduce N by 15} 16} 17 if (n> 1) // It indicates that N is not divisible by any prime number. The maximum prime factor is 18 ans = N; 19 Return ans; 20} 21 int main () 22. {23 // freopen ("1.txt"," r ", stdin); // use redirection 24 // freopen (" 2.txt", "W", stdout); 25 int N; 26 temp [1] = 1; 27 for (INT I = 0; I <size; I ++) // initialize the entire prime number table 28 temp [I] = I; 29 for (INT I = 2; I * I <size; I ++) 30 {31 if (temp [I]! =-1) 32 {33 for (Int J = I * I; j <size; j + = I) 34 temp [J] =-1; // if it is not a prime number, mark it as-135} 36} 37 int Index = 1; 38 for (INT I = 2; I <size; I ++) 39 {40 if (temp [I]! =-1) 41 prime [index ++] = temp [I]; // copy the prime number in the prime number table to the prime array 42} 43 while (scanf ("% d ", & N )! = EOF) 44 {45 if (n = 1) // special number, especially for 46 {47 printf ("0 \ n"); 48 continue; 49} 50 int left = 1, Right = index-1, mid = (left + right)/2; // determine the position of the maximum prime number in the prime table using the bipartite method, if a direct for loop times out, 51 int maxprime = maxprimefactor (n); 52 If (maxprime = prime [right]) // The subscript does not start from scratch, so when there are only two elements, if you want to determine the position of the next one, the current binary does not work, this special case is separately written 53 {54 printf ("% d \ n", right ); 55 continue; 56} 57 while (maxprime! = Prime [Mid]) 58 {59 If (maxprime> prime [Mid]) 60 left = mid; 61 else62 right = mid; 63 mid = (left + right)/2; 64} 65 printf ("% d \ n", mid); // print subscript 66} 67 return 0; 68}

 

Maximum factor of NYOJ-520

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.