zoukankan      html  css  js  c++  java
  • poj3518

    线性筛法可以过

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    
    const int  MAXSIZE=1500000;
    unsigned long previous[MAXSIZE+1];   /*size of array is MAXSIZE, then previous*/
    unsigned long next[MAXSIZE+1];         /*and next[MAXSIZE] can be used for */
    unsigned long prime, fact, i, mult;         /*number MAXSIZE*/
    unsigned long n;
    bool notprime[MAXSIZE+1];
    inline void REMOVE(unsigned long x)
    {
           next[previous[x]]=next[x];
           previous[next[x]]=previous[x];
           notprime[x] = true;
    }
    inline void INITIAL(unsigned long n)
    {
       unsigned long i=3;
       memset(notprime, 0, sizeof(notprime));
       notprime[0] = true;
       notprime[1] = true;
       while(i<=n)
       {
         previous[i]=i-2;
         if(i+2<=n)
         {
             next[i]=i+2;
             notprime[i + 1] = true;
             i+=2;
         }
         else
         {
              next[i]=0;
              break;
         }
       }
       previous[3]=2;
       next[2]=3;
       previous[2]=0;
    }
    int main(void)
    {
    	n = 1500000;
    	INITIAL(n);
        for(prime=3;prime*prime<=n;prime=next[prime])
            for(fact=prime;prime*fact<=n;fact=next[fact])
                for(mult=prime*fact;mult<=n;mult*=prime)
                       REMOVE(mult);
        int k;
        while (scanf("%d", &k) != EOF && k != 0)
        {
        	int l = k, r = k;
        	if (!notprime[k])
        	{
        		printf("0\n");
        		continue;
        	}
        	while (notprime[l])
        		l--;
        	while (notprime[r])
        	    r++;
        	printf("%d\n", r - l);
        }
        return 0;
    }
  • 相关阅读:
    perl 获取铜板街页码
    $response->decoded_content 和$response->content
    基于Netty5.0高级案例之请求响应同步通信
    [Err] 1091
    [Err] 23000
    [Err] 42000
    perl 爬取 find_by_tag_name
    perl 爬取html findvalues 方法
    perl 安装DBI和DBD
    js setTimeout 参数传递使用介绍
  • 原文地址:https://www.cnblogs.com/rainydays/p/1948634.html
Copyright © 2011-2022 走看看