"NOIP simulation Question" [T1] prime number generator (linear sieve + promiscuity)

Source: Internet
Author: User
Tags generator printf

Prime number generator

"Title description"
Tmall is designing a cipher system. First he wants to generate some prime numbers. Your task is to help him generate all prime numbers within a given range.
"Input Data"
There are multiple sets of data.
The first line of input data is an integer T (t<=10) that represents the number of groups of test data.
Next T line, each line has two integers m,n, which indicates that the range that is required to generate prime numbers is [M,n] (1<=m<=n<=10^9,n-m<=1000000)
"Output Data"
For each set of test data, output all the prime numbers in [M,n] p, one line at a.
Separate test data with a blank line.
"Sample Input"
2
1 10
3 5
"Sample Output"
2

3

5

7


3

5
"Data Range"
For 30% of data,m<n<=1000;
For 50% of data, m<n<=1000000 and n-m<=1000;
For 100% of data, m<n<=1000000000 and n-m<=1000000;

"Solving" " linear sieve + promiscuity"

"because it's a group of data, and it's going to 10^9, the linear sieve is definitely too much." "

"Since the composite of the 10^9 can be sifted with the sqrt (n) factor, the pretreatment 10^ (4.5) of the mass tables, the linear sieve"

"in M, N in the 10^4.5, you can directly output the prime number within the quality tables, using dichotomy to find the first position of the >=m, start the output can be"

"When M, N is not in the 10^4.5, using the quality tables to sift out composite, marking, because of n-m<=1000000, so the time can be saved into the vis[i-m] so that you can use the array to save. "

#include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #define N 40000 using
namespace Std;
int prime[40010],n,m,t;
BOOL p[40010],vis[1000010];
	 	inline void Shai () {for (int i=2;i<=n;++i) {if (!p[i]) prime[++prime[0]]=i;
	 	 	for (int j=1;j<=prime[0];++j) {if (i*prime[j]>n) break;
	 	 	P[i*prime[j]]=1; if (! (
		  I%PRIME[J])) break;
	}}}-inline int find (int l,int r,int x) {int mid,sum=0;
	 	while (l<=r) {mid= (l+r) >>1;
	 	 if (prime[mid]>=x) sum=mid,r=mid-1;
	 else l=mid+1;
} return sum;
	} int main () {freopen ("prime.in", "R", stdin);
	Freopen ("Prime.out", "w", stdout);
	int i,j;
	Shai ();
	scanf ("%d", &t);
	 	while (t--) {scanf ("%d%d", &m,&n);
			if (n<=n) {int l;
	 	 	L=find (1,PRIME[0],M);
	 	 	  for (int i=l;i<=prime[0];++i) if (prime[i]>n) break;
	 	 	else printf ("%d\n", Prime[i]);
	 	 	printf ("\ n");
		  Continue
		} int l=m+ (m&1);
		if (l==2) l+=2; for (i=l;i<=n;i+=2) Vis[i-m]=1;
		int d=sqrt (n) +1;
		  for (I=2;i<=prime[0];++i) if (prime[i]>d) break;
		 	else {j= (m-1)/prime[i]+1; if (! (
		 	j&1)) J + +;
		 	if (j==1) j+=2;
		 	int r=n/prime[i];
		 for (int k=j;k<=r;++k) vis[k*prime[i]-m]=1;
		} for (int i=m;i<=n;++i) if (!vis[i-m]) printf ("%d\n", I);
		memset (vis,0,sizeof (VIS));
	 printf ("\ n"); }
}


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.