[Luogu 3383] [TEMPLATE] linear sieve prime number, luogu3383 sieve Prime Number

Source: Internet
Author: User

[Luogu 3383] [TEMPLATE] linear sieve prime number, luogu3383 sieve Prime Number
Description

For example, given a range of N, You need to query whether M is a prime number (each number is within the range of 1-N)

Input/Output Format

Input Format:

The first line contains two positive integers N and M, indicating the query range and the number of queries respectively.

Each row in the next M row contains an integer not less than 1 and not greater than N, that is, whether the approximate number is a prime number.

Output Format:

The output contains M rows. Each row is Yes or No, which is the result of each query in sequence.

Input and Output sample input sample #1:
100 52349197
Output sample #1:
YesYesNoNoYes
Description

Time-Space limit: 500 ms 128 M

Data scale:

For 30% of data: N <= 10000, M <= 10000

For 100% of data: N <= 10000000, M <= 100000

Example:

N = 100, indicating that the number of subsequent queries is not greater than 100 and greater than 1.

Therefore, 2, 3, and 97 are prime numbers, and 4 and 91 are non-prime numbers.

Therefore, Yes, Yes, No, No, and Yes are output in sequence.

Normal screening method:

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 using namespace std; 6 bool is_prime(int x){ 7     if(x==1) return false; 8     for(int i=2;i*i<=x;i++){ 9         if(x%i==0) return false;    10     }11     return true;12 }13 int n,m;14 int main(){15     scanf("%d%d",&n,&m);16     for(int i=1;i<=m;i++){17         int x;scanf("%d",&x);18         if(is_prime(x)) puts("Yes");19         else puts("No");20     }21     return 0;22 }

AI screening method:

 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cmath>  6 using namespace std; 7 int n,m,q;bool is_prime[10000005]; 8 void eratos(int n){ 9     memset(is_prime,true,sizeof(is_prime));10     is_prime[1]=false;11     for(int i=2;i<=sqrt(n);i++){12         if(is_prime[i]){13             int j=i*2;14             while(j<=n){15                 is_prime[j]=false;16                 j=j+i;17             }18         }19     }20 }21 int main(){22     scanf("%d%d",&n,&m);23     eratos(n);24     for(int i=1;i<=m;i++){25         scanf("%d",&q);26         if(is_prime[q]) printf("Yes\n");27         else printf("No\n");28     }29     return 0;30 }

European screening method (linear screening method ):

 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 using namespace std; 7 bool is_prime[10000005]; 8 int prime[5000005],cnt=0,n,m,a;  9 void getlist(int n){10     memset(is_prime,true,sizeof(is_prime));11     is_prime[1]=false;12     for(int i=2;i<=n/2;i++){13         if(is_prime[i])prime[++cnt]=i;14         for(int j=1;j<=cnt&&i*prime[j]<=n;j++){15             is_prime[i*prime[j]]=false;16             if(i%prime[j]==0) break;17         }18     }19 }20 int main(){21     scanf("%d%d",&n,&m);22     getlist(n);23     for(int i=1;i<=m;i++){24         scanf("%d",&a);25         if(is_prime[a]) printf("Yes\n");26         else printf("No\n");27     }28     return 0;29 }

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.