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 descriptionif We define DN AS:DN = PN+1-PN, where pi is the i-th prime. It's easy-to-see-D1 = 1 and Dn=even for n>1. Twin Prime conjecture states that "there is infinite consecutive primes differing by 2".
Now given any positive integer N (< 10^5), you is supposed to count the number of twin primes which is no greater tha n N.
Inputyour 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.
Outputfor Each test case, the your program must the output to standard output. Print in one line the number of twin primes which is no greater than N.
Sample input1520-2
Sample Output014Test Instructions: Enter N to find the number of twin primes less than N. Prime sieve + tree-like array
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespacestd;#defineN 100005intS[n];intPRIME[N],CNTP;BOOLIsnot[n];voidGetprime () { for(intI=2; i<=n;i++) { if(isnot[i]==0) {PRIME[CNTP++]=i; for(intj=2; i*j<=n;j++) Isnot[i*j]=1; } }}intLowbit (intx) { returnx& (-x);}voidAddintXintnum) { while(x<=N) {s[x]+=num; X+=lowbit (x); }}intSumintx) { intres=0; while(x>0) {res+=S[x]; X-=lowbit (x); } returnRes;}intMain () {CNTP=1; Getprime (); for(intI=2; i<cntp;i++) if(prime[i]-prime[i-1]==2) Add (Prime[i],1); intN; while(SCANF ("%d", &n)!=eof&&n>0) {printf ("%d\n", SUM (n)); } return 0;}
Hdu_3792_ (Prime sieve + tree-like array)