zoukankan      html  css  js  c++  java
  • 筛素数——HDU 3792 Twin Prime Conjecture

    Twin Prime Conjecture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2653    Accepted Submission(s): 900


    Problem Description
    If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2".
    Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
     
    Input
    Your program must read test cases from standard input.
    The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
     
    Output
    For each test case, your program must output to standard output. Print in one line the number of twin primes which are no greater than N.
     
    Sample Input
    1 5 20 -2
     
    Sample Output
    0 1 4
     
    题意:输入一个数N(N<105),问这个数以内有多少对孪生素数(两个素数相差2)。
    题解:筛出所需的素数,然后用一个数组来存到第i个素数时有多少对孪生素数。最后找到不大于N的最大素数,只要输出到这个素数有多少对孪生素数。
    /*HDU 3792*/
    /*筛素数*/
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int maxn=1e5+100;
    bool isprime[maxn];
    int prime[maxn];
    int cnt=0;
    int a[maxn];
    void getprime()
    {
        memset(isprime,1,sizeof(isprime));
        memset(prime,0,sizeof(prime));
        isprime[0]=isprime[1]=0;
        for(int i=2;i<maxn;i++)
        {
            if(isprime[i])
                for(int j=2*i;j<maxn;j+=i)
                    isprime[j]=false;
        }
    
        for(int i=0;i<maxn;i++)
        {
            if(isprime[i])
                prime[cnt++]=i;
        }
        memset(a,0,sizeof(a));
        int t=0;
        for(int i=1;i<cnt;i++)
        {
            if((prime[i]-prime[i-1])==2)
            {
                a[i]=++t;/*当找到一对孪生素数时,在后一个+1*/
                //printf("%d %d
    ",prime[i],prime[i-1]);/**/
            }
            else
                a[i]=t;
        }
    }
    
    int find(int n,int l,int r)/*开始想用二分查找,可是不知道错哪了*/
    {
    
        int mid=(l+r)/2;
        if(prime[mid]>n)
            return find(n,l,mid);
        else if(prime[mid]==n)
            return a[mid];
        else 
            return find(n,mid+1,r);
    }
    int main()
    {
        int n;
        getprime();
        while(scanf("%d",&n)!=EOF)
        {
            if(n<0)    break;    
            int ans=0;
            int i;
            for(i=0;i<cnt-1;i++)
                if(prime[i]<=n&&prime[i+1]>n)
                    break;
            ans=a[i];
            if(n==1)    ans=0;
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    begin lydsy 2731
    关于js中this关键字的补充
    js中this关键字测试集锦
    js文件中函数前加分号和感叹号是什么意思?
    好用的wget命令从下载添加环境变量到各参数详解
    一个解析json串并组装echarts的option的函数解析
    oschina代码仓库远程push,pull免密实操总结
    yii 核心类classes.php详解(持续更新中...)
    yii2.0归档安装方法
    配置windows 系统PHP系统环境变量
  • 原文地址:https://www.cnblogs.com/yepiaoling/p/5365707.html
Copyright © 2011-2022 走看看