hdu 3792 二分

/*
* hdu3792.c
*
* Created on: 2011-9-19
* Author: bjfuwangzhu
*/

#include
<stdio.h>
#include
<string.h>
#define nmax 100200
int prime[nmax], flag[nmax];
int plen, nlen;
typedef
struct node {
int i, value;
} node;
node Node[nmax];
void init() {
memset(flag,
-1, sizeof(flag));
int i, j;
for (i = 2, plen = 0; i < nmax; i++) {
if (flag[i]) {
prime[plen
++] = i;
}
for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) {
flag[i
* prime[j]] = 0;
if (i % prime[j] == 0) {
break;
}
}
}
Node[
0].i = 1, Node[0].value = 0;
for (i = 1, nlen = 1; i < plen; i++) {
if (prime[i] - prime[i - 1] == 2) {
Node[nlen].i
= prime[i];
Node[nlen].value
= Node[nlen - 1].value + 1;
nlen
++;
}
}
}
void solve(int n) {
int left, right, mid;
left
= 0, right = nlen;
while (left <= right) {
mid
= (left + right) / 2;
if (Node[mid].i > n) {
right
= mid - 1;
}
else {
left
= mid + 1;
}
}
if (Node[left].i > n) {
left
--;
}
printf(
"%d\n", Node[left].value);
}
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data.in", "r", stdin);
#endif
init();
int n;
while (~scanf("%d", &n) && (n > 0)) {
solve(n);
}
return 0;
}

posted @ 2011-09-19 17:58  qingyezhu  阅读(163)  评论(0编辑  收藏  举报