设为首页 加入收藏

TOP

POJ 2689 Prime Distance (素数+两次筛选)
2014-11-23 19:01:53 来源: 作者: 【 】 浏览:5
Tags:POJ 2689 Prime Distance 素数 筛选


题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数。


AC代码:

#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
#include    
using namespace std;  
  
typedef long long LL;  
const int N=1<<16;  
const int M=1000005;  
const int mod=1000007;  
const int INF=0x3f3f3f3f;  
const double PI=acos(-1.0);  
  
int pri[N],k;  
void xh_phi()  
{  
    int i,j;  
    memset(pri,0,sizeof(pri));  
    k=0;  
    for(i=2;i<=N;i++)  
    {  
        if(!pri[i])  
        {  
            pri[++k]=i;  
            for(j=i;j<=N;j+=i)  
                pri[j]=1;  
        }  
    }  
}  
  
int prime[M],t;  
bool nopri[M];  
void getprime(int L,int R)  
{  
    int i,j;  
    memset(nopri,false,sizeof(nopri));  
    if(L<2)  
        L=2;  
    for(i=1;i<=k&&(LL)pri[i]*pri[i]<=R;i++)  
    {  
        int s=L/pri[i]+(L%pri[i]>0);  
        if(s==1)    s=2;  
        for(j=s;(LL)j*pri[i]<=R;j++)  
            if((LL)j*pri[i]>=L)  
                nopri[j*pri[i]-L]=true;  
    }  
    prime[0]=0;  
    t=0;  
    for(i=0;i<=R-L;i++)  
        if(!nopri[i])  
        {  
            prime[++t]=i+L;  
        }  
}  
  
int main()  
{  
    int n,m,i,j;  
    xh_phi();  
    while(~scanf("%d%d",&n,&m))  
    {  
        getprime(n,m);  
        int Mi=INF,Ma=0;  
        int x1,x2,y1,y2,f=0;  
        if(t<2)  
        {  
            printf("There are no adjacent primes.\n");  
            continue;  
        }  
        for(i=1;iMa)  
            {  
                Ma=p;x2=prime[i];y2=prime[i+1];  
            }  
            if(p
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef long long LL;
const int N=1<<16;
const int M=1000005;
const int mod=1000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

int pri[N],k;
void xh_phi()
{
    int i,j;
    memset(pri,0,sizeof(pri));
    k=0;
    for(i=2;i<=N;i++)
    {
        if(!pri[i])
        {
            pri[++k]=i;
            for(j=i;j<=N;j+=i)
                pri[j]=1;
        }
    }
}

int prime[M],t;
bool nopri[M];
void getprime(int L,int R)
{
    int i,j;
    memset(nopri,false,sizeof(nopri));
    if(L<2)
        L=2;
    for(i=1;i<=k&&(LL)pri[i]*pri[i]<=R;i++)
    {
        int s=L/pri[i]+(L%pri[i]>0);
        if(s==1)    s=2;
        for(j=s;(LL)j*pri[i]<=R;j++)
            if((LL)j*pri[i]>=L)
                nopri[j*pri[i]-L]=true;
    }
    prime[0]=0;
    t=0;
    for(i=0;i<=R-L;i++)
        if(!nopri[i])
        {
            prime[++t]=i+L;
        }
}

int main()
{
    int n,m,i,j;
    xh_phi();
    while(~scanf("%d%d",&n,&m))
    {
        getprime(n,m);
        int Mi=INF,Ma=0;
        int x1,x2,y1,y2,f=0;
        if(t<2)
        {
            printf("There are no adjacent primes.\n");
            continue;
        }
        for(i=1;iMa)
            {
                Ma=p;x2=prime[i];y2=prime[i+1];
            }
            if(p 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ2282:The Counting Problem(.. 下一篇poj 1325 Machine Schedule 二分..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python 教程 - W3Sch (2025-12-26 12:00:51)
·Python基础教程,Pyt (2025-12-26 12:00:48)
·神仙级python入门教 (2025-12-26 12:00:46)
·“我用Java 8”已成 (2025-12-26 11:19:54)
·下载 IntelliJ IDEA (2025-12-26 11:19:52)