Advertisement
newb_ie

10616 - Divisible Group Sums

Jul 8th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 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 = 212;
  16. int n,k,mod;
  17. lli grid[maxN][22][12];
  18. vector<int> nums(maxN + 1);
  19. lli solve(int i,int rem,int count){
  20.     if(i > n or count == k){
  21.         return (rem == 0 and count == k);
  22.     }
  23.     if(grid[i][rem][count] != -1){
  24.         return grid[i][rem][count];
  25.     }
  26.     int x = (nums[i] + rem) % mod;
  27.     if(x < 0 ){
  28.         x += mod;
  29.     }
  30.     return grid[i][rem][count] = solve(i + 1,x,count + 1) + solve(i + 1,rem,count);
  31. }
  32. int main(){
  33.     ios::sync_with_stdio(false);
  34.     cin.tie(nullptr);
  35.     int T = 1;
  36.     int q;
  37.     while(cin >> n >> q and n != 0 and q != 0){
  38.         for(int i = 1;i <= n; i++){
  39.             cin >> nums[i];
  40.         }
  41.         cout << "SET " << T++ << ":\n";
  42.         for(int i = 1;i <= q; i++){
  43.             cin >> mod >> k;
  44.             memset(grid,-1,sizeof(grid));
  45.             cout << "QUERY " << i << ": " << solve(1,0,0) << "\n";
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement