mr_dot_convict

10306-e-coins-UVa-mr.convict

Jun 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 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 = 41, M = 305, infi = (int)1e9;
  36. int dp[M][M];
  37. vector <pii> coins;
  38. int n, S;
  39.  
  40. void solve() {
  41.    for (int x = 0; x < M; ++x) for (int y = 0; y < M; ++y)
  42.       dp[x][y] = infi;
  43.  
  44.    dp[0][0] = 0;
  45.  
  46.    for (int i = 1; i <= n; ++i)
  47.       for (int x = 0; x < M; ++x) for (int y = 0; y < M; ++y)
  48.       {
  49.          if (x >= coins[i].x && y >= coins[i].y)
  50.             dp[x][y] = min(dp[x][y], dp[x-coins[i].x][y-coins[i].y] + 1);
  51.       }
  52.  
  53.    int mn_val = infi;
  54.    for (int x = 0; x < M; ++x) for (int y = 0; y*y <= S*S - x*x; ++y)
  55.    {
  56.       if (x*x + y*y != S*S) continue;
  57.       mn_val = min(mn_val, dp[x][y]);
  58.    }
  59.    if (mn_val == infi) cout << "not possible\n";
  60.    else cout << mn_val << '\n';
  61. }
  62.  
  63. void read() {
  64.    cin >> n >> S;
  65.    coins.assign(n+1, pii());
  66.    for (int i = 1; i <= n; ++i)
  67.       cin >> coins[i].x >> coins[i].y;
  68. }
  69.  
  70. signed main() {
  71.    IOS; PREC;
  72.    int tc; cin >> tc;
  73.    while (tc--) read(), solve();
  74.    return EXIT_SUCCESS;
  75. }
Add Comment
Please, Sign In to add comment