简单的素数打表,然后枚举。开始没注意n读到0结束,TLE了回。。下次再认真点。A过后讨论里面有个暴力打表过的,给跪了!
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cctype> 7 #include <complex.h> 8 #include <cmath> 9 using namespace std; 10 11 const int N = 10005; 12 13 bool is[N]; int prm[N]; 14 int getprm (int n) { 15 int i, j, k = 0; 16 int s, e = (int)(sqrt(0.0 + n) + 1); 17 memset (is, 1, sizeof (is)); 18 prm[k ++] = 2; is[0] = is[1] = 0; 19 for (i = 4; i < n; i += 2) is[i] = 0; 20 for (i = 3; i < e; i += 2) 21 if (is[i]){ 22 prm[k ++] = i; 23 for (s = i * 2, j = i * i; j < n; j += s) { 24 is[j] = 0; 25 } 26 } 27 for (; i < n; i += 2) { 28 if (is[i]) prm[k ++] = i; 29 } 30 return k; 31 } 32 33 int main () { 34 getprm(10000); 35 /*for (int i = 0 ; i < 100; ++ i) { 36 cout << i << ": "; 37 cout << prm[i] << endl; 38 }*/ 39 // 1229个素数 0-1w 40 ios::sync_with_stdio(false); 41 cin.tie(0); 42 int n; 43 while (~scanf ("%d", &n)) { 44 if (n == 0) break; 45 int sum, cnt = 0; 46 for (int i = 0 ; prm[i] <= n; ++ i) { 47 sum = 0; 48 for (int j = i; prm[j] <= n; ++ j) { 49 sum += prm[j]; 50 if (sum < n) { 51 continue; 52 } else if (sum == n) { 53 cnt ++; 54 break; 55 } else { 56 break; 57 } 58 } 59 } 60 printf ("%d ", cnt); 61 } 62 return 0; 63 }