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 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

来源:http://acm.hdu.edu.cn/showproblem.php?pid=3792

求区间素数,注意打表

#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=100005;
bool isPrime[MAXN];
int cnt[MAXN];
int main()
{
    int n;
    memset(isPrime,true,sizeof(isPrime));
    for(int i=2;i<MAXN;i++)
    {
        if(isPrime[i])
        {
            for(int j=i+i;j<MAXN;j+=i)
                isPrime[j]=false;
        }
    }
    cnt[5]=1;
    int pre=5;
    int num=1;
    for(int i=6;i<MAXN;i++)
    {
        if(isPrime[i])
        {
            if(i-pre==2)
            {
                num++;
            }
            pre=i;
        }
        cnt[i]=num;
    }
    while(scanf("%d",&n)!=EOF&&n>=0)
    {
        printf("%d\n",cnt[n]);
    }
    return 0;
}
时间: 2024-10-27 01:51:26

2011年浙大:Twin Prime Conjecture的相关文章

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

2011年浙大:Graduate Admission

题目描述: It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.    Each applicant will have to p

2014年麦克阿瑟基金奖,张益唐入围(62万美金用于个人支配)

在最底下 http://www.macfound.org/fellows/class/class-2014/ Meet the 2014 MacArthur Fellows Danielle Bassett University of Pennsylvania Philadelphia, PA Age: 32 Physicist applying mathematical approaches to the analysis and modeling of brain connectivity

HDU 3792 素数打表

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 inte

HDU_3792_(素数筛+树状数组)

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

资源向导之 &quot;Operating System&quot;

资源向导之"Operating System" ------------------------------------------------- MIT --- JOS  ------------------------------------------------ MIT关于JOS实验的主站 http://pdos.csail.mit.edu/6.828/2014/index.html MIT - Operating System Engineering(不同于6.008) ht

UVA 10395 素数筛

Twin Primes Twin primes are pairs of primes of the form (p; p + 2). The term \twin prime" was coined by PaulStckel (1892-1919). The rst few twin primes are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43).In this problem you are asked to nd out

2019浙大校赛--J--Extended Twin Composite Number(毒瘤水题)

毒瘤出题人,坑了我们好久,从基本的素数筛选,到埃氏筛法,到随机数快速素数判定,到费马小定理,好好的水题做成了数论题. 结果答案是 2*n=n+3*n,特判1,2. 以下为毒瘤题目: 题目大意: 输入一个数n, 输出两个合数(即非素数)a,b 实现 n+a=b 解题思路 3n=n+2n; 特判1.2 代码: 1 #include<iostream> 2 #include<stdio.h> 3 using namespace std; 4 typedef long long ll; 5

BZOJ2440 [中山市选2011]完全平方数

Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱. 这天是小X的生日,小 W 想送一个数给他作为生日礼物.当然他不能送一个小X讨厌的数.他列出了所有小X不讨厌的数,然后选取了第 K个数送给了小X.小X很开心地收下了. 然而现在小 W 却记不起送给小X的是哪个数了.你能帮他一下吗? Input 包含多组测试数据.文件第一行有一个整数 T,表示测试数据的组数.