fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6.  
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. #include <ext/pb_ds/tree_policy.hpp>
  9. using namespace __gnu_pbds;
  10.  
  11. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
  12. typedef pair<int, int>pr;
  13. #define all(i) i.begin() , i.end()
  14. #define ft first
  15. #define sn second
  16. #define pb push_back
  17. #define totalone(mask) __builtin_popcount(mask)
  18. #define chkbit(mask,bit) (mask&(1LL << bit))
  19.  
  20. // debug section start
  21. void __print(int x) {cerr << x;}
  22. void __print(long x) {cerr << x;}
  23. void __print(long long x) {cerr << x;}
  24. void __print(unsigned x) {cerr << x;}
  25. void __print(unsigned long x) {cerr << x;}
  26. void __print(unsigned long long x) {cerr << x;}
  27. void __print(float x) {cerr << x;}
  28. void __print(double x) {cerr << x;}
  29. void __print(long double x) {cerr << x;}
  30. void __print(char x) {cerr << '\'' << x << '\'';}
  31. void __print(const char *x) {cerr << '\"' << x << '\"';}
  32. void __print(const string &x) {cerr << '\"' << x << '\"';}
  33. void __print(bool x) {cerr << (x ? "true" : "false");}
  34.  
  35. template<typename T, typename V>
  36. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  37. template<typename T>
  38. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  39. void _print() {cerr << "]\n";}
  40. template <typename T, typename... V>
  41. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  42. #ifndef ONLINE_JUDGE
  43. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  44. #else
  45. #define debug(x...)
  46. #endif
  47. // debug section closed
  48.  
  49. #define en "\n"
  50. #define sum(n) ((1LL*(n)*(n+1))/ 2LL)
  51. #define sqr(n) (1LL*(n)*(n))
  52. #define vag(a,b) ((a + b - 1)/b)
  53.  
  54. #define MAXN 10005
  55. #define inf 1e6+5
  56. const int mod = 1e9 + 7;
  57.  
  58. bool prime[MAXN];
  59. vector<ll>v;
  60.  
  61. void sieve()
  62. {
  63. v.pb(2);
  64. for (ll i = 3; i * i < MAXN; i += 2)
  65. {
  66. if (prime[i] == false) {
  67. for (ll j = i * i; j < MAXN; j += 2 * i)
  68. prime[j] = true;
  69. }
  70. }
  71. for (ll i = 3; i < MAXN; i += 2) if (prime[i] == false) v.pb(i);
  72. }
  73.  
  74. ll mem[1300][MAXN];
  75. ll n;
  76.  
  77. ll f(ll i, ll sm)
  78. {
  79. if (i == v.size())
  80. {
  81. if (sm != 0) return INT_MIN;
  82. return 0;
  83. }
  84. if (mem[i][sm] != -1) return mem[i][sm];
  85.  
  86. ll an = INT_MIN;
  87. an = max(an, f(i + 1, sm));
  88. if (sm - v[i] >= 0)
  89. an = max(an, 1 + f(i + 1, sm - v[i]));
  90.  
  91. return mem[i][sm] = an;
  92. }
  93. void solve()
  94. {
  95. cin >> n;
  96.  
  97. ll an = f(0, n);
  98. an = max(an, 0LL);
  99. cout << an << en;
  100. }
  101. int main()
  102. {
  103. IOS;
  104. int t;
  105. t = 1;
  106. memset(mem, -1, sizeof(mem));
  107.  
  108. cin >> t;
  109. sieve();
  110.  
  111. int c = 0;
  112. while ( t-- )
  113. {
  114. // cout << "Case " << ++c << ":\n";
  115. solve();
  116. }
  117. return 0;
  118. }
Success #stdin #stdout 0.06s 105244KB
stdin
Standard input is empty
stdout
0