HDU 1431 Prime Number

Source: Internet
Author: User

HDU 1431 Prime Number

Someone asked me this question.

I personally feel that the violent search will be TLE O (n * sqrt (n )). N = 100000000; (2 ~ is used to determine the prime number ~ Sqrt (n) + 1 removal)


The enumeration is better. Enumeration 1 ~ 10000. Save each of them. The number of input records is known as left, right, and then combine them.


For example, 1, judge whether 11 is a prime number.

For example, 10 is used to determine whether 101 is a prime number and whether 1001 is a prime number.


The complexity is O (n ^ 2 ). At the beginning, bool pa [100000000] is ready to be identified with tags. Result MLE.


Then, calculate the total number, which can be 781 at most. Int pa [1000] can be mounted.

G ++ 15 ms


#include
 
  #include
  
   #include
   
    #include
    
     #include#include
     #include
      
       #include
       
        #include
        
         #include
         
          #include
          
           #include
           
            #define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,b) for(int i=a;i
            
             =b;i--)#define pb push_back#define mp make_pair#define ft first#define sd second#define sf scanf#define pf printf#define sz(v) ((int)(v).size())#define all(v) (v).begin(),(v).end()#define acfun std::ios::sync_with_stdio(false)#define SIZE 100000000 +1using namespace std;int pa[1000];int cot;bool prime(int n){ FOR(i,2,sqrt(n)+2) if(n%i==0)return 0; return 1;}void PA(){ cot=0; pa[cot++]=2; pa[cot++]=3; pa[cot++]=5; pa[cot++]=7; FOR(i,1,10000) { int num[5]; int len=0; int m=i; while(m) { int tmp=m%10; num[len++]=tmp; m/=10; } int ans=i; if(len>1) { FOR(r,1,len) ans=ans*10+num[r]; if(prime(ans)) pa[cot++]=ans; } ans=i; FOR(r,0,len) ans=ans*10+num[r]; if(prime(ans)) pa[cot++]=ans; }}int main(){ PA(); int a,b; while(~sf("%d%d",&a,&b)) { FOR(i,0,cot) if(pa[i]>=a&&pa[i]<=b)pf("%d\n",pa[i]); pf("\n"); }}
            
           
          
         
        
       
      
    
   
  
 




Related Article

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.