Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <map>
  6. #include <set>
  7. #include <unordered_map>
  8. #include <unordered_set>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12.  
  13. using namespace std;
  14.  
  15. #define ll long long
  16. #define ld long double
  17. #define mp make_pair
  18. #define pii pair<int, int>
  19. #define ft first
  20. #define sd second
  21.  
  22.  
  23. int n;
  24. vector<ll> a;
  25.  
  26. vector<ll> dp;
  27. vector<ll> makedp;
  28.  
  29. pair<ll, ll> countans(ll b) {
  30. if (b == 0)
  31. return mp(0, 0);
  32. int i = upper_bound(a.begin(), a.end(), b) - a.begin() - 1;
  33. ll c = b / a[i];
  34. pair<ll, ll> ans = mp((c - 1) * a[i] + makedp[i], c - 1 + dp[i]);
  35. auto otherans = countans(b - c * a[i]);
  36. if (otherans.second + c > ans.second) {
  37. ans = otherans;
  38. ans.second += c;
  39. ans.first += (c) * a[i];
  40. }
  41. return ans;
  42. }
  43.  
  44. int32_t main() {
  45. cin.tie(NULL);
  46. cout.tie(NULL);
  47. ios_base::sync_with_stdio(false);
  48.  
  49. cin >> n;
  50. a.resize(n);
  51. for (int i = 0; i < n; i++)
  52. cin >> a[i];
  53.  
  54. dp.resize(n); // ans if b is equal to a[i] - 1;
  55. makedp.resize(n);
  56.  
  57. dp[0] = 0;
  58. for (int i = 1; i < n; i++) {
  59. auto t = countans(a[i] - 1);
  60. dp[i] = t.second;
  61. makedp[i] = t.first;
  62. }
  63. int q;
  64. cin >> q;
  65. for (; q > 0; q--) {
  66. ll b;
  67. cin >> b;
  68. auto ans = countans(b);
  69. cout << ans.ft << ' ' << ans.sd << '\n';
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement