Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴代码#include<cstdio> #include<cstring> #include<cstdlib> using namespace std; #define N 1299800 bool f[N]; int p[N]; int tot = 0; void get_prime(){ memset(f,0,sizeof(f)); f[1] = 1; for (int i = 2; i <= N; i ++){ if (!f[i]) { p[++tot] = i; for (int j = i; j <= N / i; j++) f[i * j] = 1; } } return; } int n; int main(){ get_prime(); while (scanf("%d",&n),n){ if (!f[n] || n == 1) puts("0"); else{ int l = 0, r = tot; int ans; while (l <= r){ int mid = (l + r) >> 1; if (p[mid] < n) ans = mid,l = mid + 1; else r = mid - 1; } printf("%d\n",p[ans + 1] - p[ans]); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator