mr_dot_convict

10616-Divisble_Group_Sum_UVa-mr.convict

Jun 22nd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. using namespace std;
  10. #pragma GCC      optimize ("Ofast")
  11. #pragma GCC      optimize ("unroll-loops")
  12. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  13.  
  14. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  15. #define PREC     cout.precision (10); cout << fixed
  16. #define bg(x)    " [ " << #x << " : " << (x) << " ] "
  17. #define x        first
  18. #define y        second
  19. using ll = long long;
  20. using ld = long double;
  21. using pii = pair<int,int>;
  22.  
  23. #define debug(args...) { \
  24.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  25.    stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); \
  26. }
  27. void err(istream_iterator<string> it) { it->empty();
  28.    cerr << " (Line : " << __LINE__ << ")" << '\n';
  29. }
  30. template<typename T, typename... Args>
  31. void err(istream_iterator<string> it, T a, Args... args) {
  32.     cerr << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  33.     err(++it, args...);
  34. }
  35. const int N = 200, M = 10, D = 20;
  36. int dp[N+1][M+1][D+1];
  37. vector <int> ar;
  38. int n, m, d, q;
  39.  
  40. void dp_solve() {
  41.    //base case
  42.    for (int i = 0; i <= n; ++i) {
  43.       for (int j = 0; j <= m; ++j) {
  44.          for (int k = 0; k < d; ++k) {
  45.             if (k > 0) dp[i][0][k] = 0; // cannot chose any item and get k > 0 // NOT POSSIBLE
  46.             if (k > 0) dp[0][j][k] = 0; // not allowed any item to chose from to get k > 0 // NOT POSSIBLE
  47.             if (j > i) dp[i][j][k] = 0; // chosing more items than given items // NOT POSSIBLE
  48.             dp[i][0][0] = 1; // Chosing none to get k == 0 from i items always possible -- chose none (1 way)
  49.          }
  50.       }
  51.    }
  52.  
  53.    //recurrence
  54.    for (int i = 1; i <= n; ++i)
  55.       for (int j = 1; j <= m; ++j)
  56.          for (int k = 0; k < d; ++k)
  57.             dp[i][j][k] = dp[i-1][j][k] + dp[i-1][j-1][((k - ar[i]%d + d)%d)];
  58.  
  59.    // dp(i,j,k) := chose j items from i to get remainder of sum to be k
  60.    // either don't chose ith item so dp(i-1,j,k) i.e. solve with upto i-1 items
  61.    // or chose ith items so dp(i-1,j-1,remaining remainder) i.e. solve with upto i-1
  62.    // // // by chosing j-1 items from i-1 and get remainder that is left after taking ith item
  63. }
  64. void read(int tc) {
  65.    ar.assign(n+1, 0);
  66.    for (int i = 1; i <= n; ++i) cin >> ar[i];
  67.    cout << "SET " << tc << ":\n";
  68.    for (int iq = 1; iq <= q; ++iq) {
  69.       cout << "QUERY " << iq << ": ";
  70.       cin >> d >> m;
  71.       dp_solve();
  72.       cout << dp[n][m][0] << '\n';
  73.    }
  74. }
  75.  
  76. signed main() {
  77.    IOS; PREC;
  78.    int tc = 0;
  79.    while (cin >> n >> q, n || q) read(++tc);
  80.    return EXIT_SUCCESS;
  81. }
Add Comment
Please, Sign In to add comment