HackerRank# Red John is Back
简单动归+素数判定,没用筛法也能过
代码:
1 #include <cmath> 2 #include <cstdio> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 10 #define MAX_N 64 11 #define MAX_M 1000000 12 13 int T, N; 14 int cnt[MAX_N]; 15 int p[MAX_M]; 16 17 int is_prime(int n) { 18 for (int i = 2; i * i <= n; i++) 19 if (n % i == 0) 20 return 0; 21 return 1; 22 } 23 24 int main() { 25 /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 26 p[1] = 0; 27 for (int i = 2; i < MAX_M; i++) 28 p[i] = p[i - 1] + is_prime(i); 29 cin >> T; 30 while (T--) { 31 cin >> N; 32 memset(cnt, 0, sizeof(cnt)); 33 cnt[0] = 1; 34 for (int i = 1; i <= N; i++) 35 cnt[i] = cnt[i - 1] + (i - 4 >= 0 ? cnt[i - 4] : 0); 36 cout << p[cnt[N]] << endl; 37 } 38 return 0; 39 }