We know that the Sieve method is generally open up a large array, and then the cycle can be 2,3,5, such as multiple culling, through yesterday's ACM training, I learned several variants, very interesting.
1): General Screening Method:
BOOL Prime[max];
memset (prime,true,sizeof (Prime));
for (k=4;k<max;k+=2)
prime[k]=false;
for (I=3;i<=sqrtmax;++i)
{
if (Prime[i])
{for
(int j=i*i;j<max;j+=2*i)
prime[j]=false;
}
}
J Starting from the I*i, it is clear that the number of smaller than i*i has been removed, but this method note that the size of I may lead to i*i out of bounds, this time to open a wide range or to be judged divided into i*i and i+i two cases of the first twice times the number of elimination, then can j+=2*i.
Attached: HDU1397
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
const int max=32770;
const int sqrtmax=181;
const int submax=10000;
BOOL Prime[max];
int Subprime[submax];
void Creatprime (void)
{
int i,k;
memset (prime,true,sizeof (Prime));
for (k=4;k<max;k+=2)
prime[k]=false;
for (I=3;i<=sqrtmax;++i)
{
if (Prime[i])
{for
(int j=i*i;j<max;j+=2*i)
prime[j]= false;
}
}
subprime[0]=0;
for (I=2;i<max;++i)
if (Prime[i])
subprime[++subprime[0]]=i;
}
int main (void)
{
int n,count;
Creatprime ();
cin>>n;
while (n!=0)
{
count=0;
if (Prime[n-2])
count++;
for (int i=1;i<=subprime[0]&&subprime[i]<=n/2;i++)
if (prime[subprime[i]]&&prime[ N-subprime[i]])
count++;
cout<<count<<endl;
cin>>n;
}
return 0;
}
2): Solve the sum of a number of factors: on the basis of the general sieve slightly deformed, prime[i] accumulation factor can be.
memset (prime,0,sizeof (Prime));
for (I=1;i<=max/2;++i)
{for
(j=i+i;j<max;j+=i)
prime[j]+=i;
}
Attached: HDU1215
#include <stdio.h>
#include <string.h>
#define MAX 500001
int Prime[max];
void Creatprime (void)
{
int i,j;
memset (prime,0,sizeof (Prime));
for (I=1;i<=max/2;++i)
{for
(j=i+i;j<max;j+=i)
prime[j]+=i;
}
}
int main ()
{
int n,ncase;
Creatprime ();
scanf ("%d", &ncase);
while (ncase--)
{
scanf ("%d", &n);
printf ("%d\n", Prime[n]);
}
return 0;
}
3): Solve a number of the maximum mass factor (not more than he itself in the quality table of the first few) such as 9 of the maximum factor 3 ranked second, 15 of the largest factor 5 ranked third:
memset (prime,0,sizeof (Prime));
for (I=2;i<=max;++i)
{
if (prime[i]==0)
{
count++;
for (j=i;j<max;j+=i)
prime[j]=count;
}
}
Attached: HDU2136
#include <stdio.h>
#include <string.h>
#define MAX 1000000
int Prime[max];
void Creatprime (void)
{
int i,j,count=0;
memset (prime,0,sizeof (Prime));
for (I=2;i<=max;++i)
{
if (prime[i]==0)
{
count++;
for (j=i;j<max;j+=i)
prime[j]=count;
}}} int main ()
{
int n;
Creatprime ();
while (scanf ("%d", &n)!=eof)
{
printf ("%d\n", Prime[n]);
}
return 0;
}
4): solves the maximum mass factor of a number (if it is prime, it is itself)
memset (prime,0,sizeof (Prime));
for (int i=2;i<max;++i)
{
if (prime[i]==0)
{for
(int j=i;j<max;j+=i)
prime[j]=i;
}
}
Yesterday's ACM training learned something, let us learn to think, but the key is to practice more thinking, continue to work hard.