ACM/ICPC algorithm Training "play table" thinking (anti-timeout)--Additional prime number screening method

Source: Internet
Author: User

What is "hit the table", say simple point is:

Sometimes, rather than running the same algorithm repeatedly, it is better to simply enumerate all possible answers to this set of data into a container that is large enough-such as an array (to hit the table) and then enter the data, and then directly traverse the container to retrieve whether the data has the test instructions required results.

    • A simple example that almost all programmers know = =: prime (POJ 1595)-prime cuts

The main idea is to give more than a group of N (1~1000) and C, so that you from the middle of the prime number of N to extend the C prime number, for example, given 7 1, the prime number of 5 (note that the topic of the pit Dad to let 1 as "prime") 1,2,3,5,7, then output 7 1:3 5 7 (note the output format

In particular, an even number of prime numbers output 2c-1, odd numbers output 2c.

  

So there are a few things that we need to analyze:

    • n The data range is small, but may be a large amount of data, it looks like only 1000 of the prime, but if the tens of thousands of data, with the outrageous method, each time the prime number is solved and then to the statistical median, and then expand, is obviously unwise, easy tle.
    • Should pay attention to the special sentence: C If greater than the actual number of prime range, then we should output all primes, this place is not handled well, easy WA.
    • Note the upper and lower bounds of the number of primes for the output extension, and also note that the maximum prime number (<=1000) is not exceeded, and the discuss area is saying that the test data may be greater than 1000 probably he did not handle the problem himself.

Second, three points is the detail problem, but the 1th is the algorithm optimization problem, the common Prime algorithm optimization is: The table Method and screening method .

    • Play Table method: before dealing with the problem, all possible prime numbers are marked separately by the array, and then the corresponding prime number is found when dealing with the problem, eliminating the time of the repeated counting primes.
    • Screening Method: all composite within the tag range (the basic idea is: There are two number:i,j(i,j>=2), then i*j must be composite, so when i You can skip the composite when traversing to these

Code attached:

    

1 //The prime number of the table (screening method)2 //memory:140 k,time:0 Ms3#include <iostream>4#include <cstdio>5#include <cmath>6 using namespacestd;7 8 #defineMAX 10059 Ten intPRIME[MAX],PL; One intNum[max]; A  - /*Screening Method Prime number of the table*/ - voidPrime_number (intmax_num) the { -     inti,j; -     intK = (int) sqrt (1.0*max_num);//Radical -Rounding -  +     /*Mark All composite*/ -      for(i=2; I <= k;i++) +     { A         if(!Num[i]) at              for(j =2*i; j<=max_num; j+=i) -NUM[J] =1; -     } -  -     /*Play Table*/ -PL =1;//Prime Counter in      for(i=1; I <= max_num;i++)//PS: This question "Prime" includes 1 -     { to         if(!Num[i]) +prime[pl++] =i; -     } the     return; * } $ Panax Notoginseng intMain () - { the     intn,c; +     inti; A  thePrime_number (MAX);//first hit the maximum prime number table. +  -      while(~SCANF ("%d%d",&n,&c)) $     { $         /*Find Prime Range*/ -          for(i =1; i < pl; i++) -         { the             if(N <Prime[i]) -                  Break;Wuyi         } the         intMAXP = i1; -  Wuprintf"%d%d:", n,c); -         if(maxp%2)//Odd number of About         { $             intMid = (maxp+1)/2; -             if(2*c-1>Maxp) -c =mid; -  A              for(i = mid-c+1; I <= mid+c-1; i++) +printf"%d", Prime[i]); the         } -         Else    //an even number of $         { the             intMID = maxp/2; the             if(2*c >Maxp) thec =mid; the  -              for(i = mid-c+1; I <= mid+c; i++) inprintf"%d", Prime[i]); the         } theprintf"\ n"); About     } the  the     return 0; the}
View Code

Loading ...

ACM/ICPC algorithm Training "play table" thinking (anti-timeout)--Additional prime number screening method

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.