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

浙大计算机研究生保研复试上机考试-2011年

题意:在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\n",sum[x]);
    }
    return 0;
}
时间: 2025-01-19 20:27:23

hdu 3792 Twin Prime Conjecture 前缀和+欧拉打表的相关文章

2011年浙大:Twin Prime Conjecture

Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2659    Accepted Submission(s): 906 Problem Description If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is

HDU 4002 Find the maximum(数论-欧拉函数)

Find the maximum Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than

hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 852 Accepted Submission(s): 259 Problem Descriptionan = X*an-1 + Y and Y mod (X-1) = 0.Your task is to calculate th

『素数(Prime)判定和线性欧拉筛法(The sieve of Euler)』

素数(Prime)及判定 定义 素数又称质数,一个大于1的自然数,除了1和它自身外,不能整除其他自然数的数叫做质数,否则称为合数. 1既不是素数也不是合数. 判定 如何判定一个数是否是素数呢?显然,我们可以枚举这个数的因数,如果存在除了它本身和1以外的因数,那么这个数就是素数. 在枚举时,有一个很简单的优化:一个合数\(n\)必有一个小于等于\(\sqrt{n}\)的因数. 证明如下: 假设一个合数\(n\)没有小于等于\(\sqrt{n}\)的因数. 由于\(n\)为合数,所以除了\(n\)与

欧拉函数与欧拉打表解决实际问题

1.欧拉函数的定义: 欧拉函数phi(x)等于不超过x且与x互素的整数的个数. 2.欧拉函数的求法:推导过程见随笔<欧拉函数与容斥原理>. 3.代码实现欧拉函数: 1 int euler_phi(int n) 2 { 3 int m=(int)sqrt(n+0.5);//取一半就行,简化计算 4 int ans=n; 5 for(int i=2;i<=m;i++) 6 if(n%i==0)//找素因子 7 { 8 ans=ans/i*(i-1);//公式的运用 9 while(n%i==

HDU 1286:找新朋友(欧拉函数)

http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 #define N 33000 6 int is_prime[N], prime[N], phi[N]; 7 8 void Euler() { 9 int n = 32

POJ 3126 Prime Path (bfs+欧拉线性素数筛)

Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. - It is a matter of security to change such things every now

(hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)

题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 125 Accepted Submission(s): 84   Problem Description Do you have spent some time to think and try to solve those unsolved problem afte

hdu 5152 A Strange Problem线段树+欧拉函数

*****************************************BC题解**********************************************************************1003 A Strange Problem 对于操作二,利用公式 当x >= Phi(C), A^x = A ^ (x%Phi(C) + Phi(C)) (mod C) 对于2333333这个模数来说,求18次欧拉函数后就变成了1,所以只需要保存19层第三次操作的加数