Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1898 Accepted Submission(s): 592
#include <stdio.h> #include <string.h> const int MAX = 100000 + 10; bool prime[MAX]; int r[MAX]; void IsPrime() { memset(prime,0,sizeof(prime)); prime[0] = prime[1] = 1; for(int i = 2;i < MAX;i++) for(int j = 2;i*j < MAX;j++) prime[j*i] = 1; } void sove() { IsPrime(); r[0] = r[1] = r[2] = 0; for(int i = 3;i < MAX;i++) { if(!prime[i-2]&&!prime[i]) r[i] = r[i-1] + 1; else r[i] = r[i-1]; } } int main() { int n; sove(); while(scanf("%d",&n),n>-1) { printf("%d\n",r[n]); } return 0; }
#include <iostream> #define lowbit(t)(-t&t) using namespace std ; const int MAX = 100001; int prime[MAX],tree[MAX],num[MAX]; void update(int x,int val) { for(int i = x;i < MAX;i += lowbit(i)) tree[i] += val; } int GetSum(int x) { int sum=0; for(int i = x;i > 0;i -= lowbit(i)) sum += tree[i]; return sum; } int main() { int cnt=0; for(int i=2;i<MAX;i++) if(!prime[i]) { num[cnt++]=i; for(int j=i;i<318&&j*i<MAX;j++) prime[i*j]=1; } for(int i=1;i<cnt;i++) if(num[i]-num[i-1]==2) update(num[i],1); int n; while(cin>>n,n>=0) { if(!n) cout<<"0"<<endl; else cout<<GetSum(n)<<endl; } return 0; }