poj 2689 线性时间筛选素数

#include 
#include 
#include "stdio.h"
#include "memory.h"
#include 
using namespace std;


#define N 50010


bool used[1000010];
int p[N];
int cnt = 0;


void Init()
{
    memset(used,false,sizeof(used));
    for (int i = 2; i < N; i++)
    {
        if (!used[i])
        {
            p[cnt] = i;
            cnt++;
        }
        for (int j = 0; j < cnt && i * p[j] < N; j++)
        {
            used[i * p[j]] = true;
            if (i % p[j] == 0)
                break;
        }
    }
}


int res[500000];
int num = 0;
void GetPrime(int L,int R)
{
    memset(used,false,sizeof(used));
    for (int i = 0; i < cnt && p[i] <= sqrt(R * 1.0) + 0.00001; i++)
    {
        int s = L / p[i] + ((L % p[i]) > 0);
        if (s == 1) s = 2;
        for (int j = s; (long long)j * p[i] <= R; j++)
       {
            if ((long long)j * p[i] - L >= 0)
                 used[j * p[i] - L] = true;
            else
                {
            printf("");
            }
       }
    }


    num = 0;
    for (size_t i = L; i <= R; i++)
    {
        if (used[i - L] == false)
        {
            res[num] = i;
            num++;
        }
    }
}


int main()
{
    Init();
 /*   for (int i = 0; i < cnt; i++)
        printf("%d ",p[i]);
    printf("\n");
    cout< maxDis)
                {
                    s2 = i;
                    maxDis = res[i + 1] - res[i];
                }
            }


            printf("%d,%d are closest, %d,%d are most distant.\n",
                res[s1],res[s1 + 1],res[s2],res[s2 + 1]);
        }
 /*       for (int i = 0; i < num; i++)
            printf("%d ",res[i]);
        printf("\n");*/
    }
    return 0;
}


你可能感兴趣的:(poj 2689 线性时间筛选素数)