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)


    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
     
    Source
    题意:在n以内的孪生素数的对数;

     思路:本以为素数打表+暴力能过,结果数据好像很多,求下前缀和打个表就行;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define mod 1000000007
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=1e6+10,inf=1e9+10;
    const int MAXN=100001;
    int prime[MAXN];
    bool vis[MAXN];
    int Prime(int n)
    {
        int cnt=0;
        memset(vis,0,sizeof(vis));
        for(int i=2;i<n;i++)
        {
            if(!vis[i])
            prime[cnt++]=i;
            for(int j=0;j<cnt&&i*prime[j]<n;j++)
            {
                vis[i*prime[j]]=1;
                if(i%prime[j]==0)
                break;
            }
        }
        return cnt;
    }
    int flag[N];
    int sum[N];
    int main()
    {
        int ji=Prime(MAXN);
        int x,y,z,i,t;
        for(i=1;i<ji;i++)
        {
            if(prime[i]-prime[i-1]==2)
            flag[prime[i]]=1;
        }
        for(i=1;i<=100000;i++)
        sum[i]=sum[i-1]+flag[i];
        while(~scanf("%d",&x))
        {
            if(x<0)break;
            printf("%d
    ",sum[x]);
        }
        return 0;
    }
  • 相关阅读:
    了解Web2.0必订阅之十大Blog[个人推荐]
    [J2ME Q&A]Target port denied to untrusted applications问题回应
    2005年Csdn十大最热门BLog作者排名第一?
    J2me流媒体技术实现讨论[1]
    液氮
    微分、差分和变分的概念
    Python mutable vs immutable (不可变对象 vs 可变对象)
    异戊烷
    免疫组化
    [导入]java escape unescape
  • 原文地址:https://www.cnblogs.com/jhz033/p/5578720.html
Copyright © 2011-2022 走看看