Links: http://codevs.cn/problem/1530/
Problem Solving Ideas:
The most critical pruning of the problem is that the factor is less than the square root, but it is not used as originally.
Reversing thinking is more about judging which is not prime, simpler and more efficient than judging which is prime.
All composite have a common feature, which is that they can be split into mass factors.
Then the quality factor that has appeared, sooner or later, will become a composite factor.
Then directly take the number to judge by the prime numbers in addition to the first time, you can directly judge whether it is composite.
Write this, the first three points on the past, as for the last 100000, is the beginning of the pruning.
In any case, the quality factor within a composite must be less than its square root.
So in advance to judge, try to the first few composite stop, you can greatly improve efficiency.
1#include <cstdio>2#include <iostream>3#include <cmath>4 using namespacestd;5 intn,ans,prime[100001],PH;//the ph indicates the number of prime numbers currently being searched. 6 7 intDivideDoublex) {//Judging the number of prime numbers to be tested8 intTemp= (int) x;9 for(intI=1; i<ph;i++){Ten if(prime[i]>temp)returnI-1; One } A } - - intMain () { theCin>>N; -prime[1]=2; -prime[2]=3; -Ph=2; + BOOLcheck; - inttemp,test; + while(ph<n) {//Recursive Atemp=prime[ph]; atph++; - while(1){ -temp++; -test=Divide (sqrt (temp)); - for(intI=1; i<=test;i++) { - if(Temp% prime[i]==0) {check=true; Break;} in}//I 'll do it again. - if(check) {check=false;Continue;} toprime[ph]=temp; + Break; - } the } *cout<<Prime[n]; $}
Codevs 2530 Large prime numbers