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;
}
原文地址:https://www.cnblogs.com/jhz033/p/5578720.html