Advertisement
newb_ie

UVA 1213

Jul 8th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. /*
  2. ======================
  3. [     ___T_          ]
  4. [    | - + |         ]
  5. [    |__v__|=> HI:-) ]
  6. [   .=[::+]=.        ]
  7. [ ]=' [___] '=[      ]
  8. [     /  |           ]
  9. [    _\  |_          ]
  10. ======================
  11.  */
  12. #include<bits/stdc++.h>
  13. using namespace std;
  14. using lli = int64_t;
  15. const int maxN = 1200;
  16. lli isVisited[maxN / 64 + 200];
  17. vector<int> primes;
  18. void bitwise_seive_gen(int limit){
  19.     limit += 100;
  20.     for(lli i = 3; i <= sqrt(limit) ; i += 2){
  21.         if(!(isVisited[i/64]&(1LL<<(i%64)))) {
  22.             for(long long j = i * i; j <= limit; j += 2 * i) {
  23.                 isVisited[j/64] |= (1LL<<(j%64));
  24.             }
  25.         }
  26.     }
  27.     primes.push_back(2);
  28.     for(lli i = 3; i <= limit; i += 2){
  29.         if(!(isVisited[i / 64] & (1LL << (i % 64)))) {
  30.             primes.push_back(i);
  31.         }
  32.     }
  33. }
  34. lli grid[maxN - 940][maxN][15];
  35. int n,k;
  36. lli solve(int i,int j,int count){
  37.     if(i == (int) primes.size() or j >= n or count >= k){
  38.         return (j == n and count == k);
  39.     }
  40.     if(grid[i][j][count] != -1){
  41.         return grid[i][j][count];
  42.     }
  43.     return grid[i][j][count] = solve(i + 1,j + primes[i],count + 1) + solve(i + 1,j,count);
  44. }
  45. int main(){
  46.     ios::sync_with_stdio(false);
  47.     cin.tie(nullptr);
  48.     bitwise_seive_gen(maxN);
  49.     while(cin >> n >> k and n != 0 and k != 0){
  50.         memset(grid,-1,sizeof(grid));
  51.         cout << solve(0,0,0) << "\n";
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement