Advertisement
lalalalalalalaalalla

Untitled

Feb 12th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <map>
  8. #include <unordered_map>
  9. #include <cmath>
  10. #include <time.h>
  11. #include <random>
  12. #include <string>
  13. #include <cassert>
  14. #include <vector>
  15. #include <ostream>
  16. #include <istream>
  17. #include <stack>
  18. #include <deque>
  19. #include <queue>
  20. #include <functional>
  21.  
  22. using namespace std;
  23.  
  24. #define int long long
  25. #define pb push_back
  26. #define all(a) (a).begin(), (a).end()
  27. #define pii pair<int, int>
  28. #define ld long double
  29.  
  30. ostream& operator << (ostream &a, const vector<int> &b) {
  31. for (auto k : b) cout << k << " ";
  32. return a;
  33. }
  34.  
  35. #ifdef LOCAL
  36. #define dbg(x) cout << #x << " : " << (x) << "\n";
  37. const int INF = 1e18;
  38. const int mlog = 20;
  39. // const int mod = 2600000069;
  40. // const int p = 10;
  41. // const ld PI = 3.1415926535;
  42. #else
  43. #define dbg(x)
  44. const int INF = 1e18;
  45. const int mlog = 61;
  46. // const int mod = 2600000069;
  47. // const int p = 179;
  48. // const ld PI = 3.1415926535;
  49. #endif
  50.  
  51. //#pragma GCC optimize("Ofast,no-stack-protector")
  52. //#pragma GCC target("sse,sse2,sse3,sse3,sse4")
  53. //#pragma GCC optimize("unroll-loops")
  54. //#pragma GCC optimize("fast-math")
  55. //#pragma GCC target("avx2")
  56. //#pragma GCC optimize("section-anchors")
  57. //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
  58. //#pragma GCC optimize("vpt")s
  59. //#pragma GCC optimize("rename-registers")
  60. //#pragma GCC optimize("move-loop-invariants")
  61. //#pragma GCC optimize("unswitch-loops")
  62. //#pragma GCC optimize("function-sections")
  63. //#pragma GCC optimize("data-sections")
  64.  
  65.  
  66. void solve() {
  67. int n, m;
  68. cin >> n >> m;
  69. vector<int> a(m);
  70. vector<int> cnt(mlog, 0);
  71. for (int i = 0; i < m; i++) {
  72. cin >> a[i];
  73. cnt[log2(a[i])]++;
  74. }
  75. // dbg(cnt);
  76. for (int i = 0; i < mlog; i++) {
  77. if (((n >> i) & 1)) {
  78. if (cnt[i]) {
  79. n -= (1LL << i);
  80. cnt[i]--;
  81. continue;
  82. }
  83. vector<pii> take;
  84. int need = (1LL << i);
  85. for (int j = i - 1; j >= 0; j--) {
  86. if (!cnt[j]) continue;
  87. if (need <= cnt[j] * (1LL << j)) {
  88. take.pb({j, need / (1LL << j)});
  89. need -= ((need / (1LL << j)) * (1LL << j));
  90. } else {
  91. take.pb({j, cnt[j]});
  92. need -= (1LL << j) * cnt[j];
  93. }
  94. }
  95. if (need == 0) {
  96. for (auto k : take) {
  97. cnt[k.first] -= k.second;
  98. }
  99. n -= (1LL << i);
  100. }
  101. }
  102. }
  103. int ans = 0;
  104. int lst = -1;
  105. for (int i = mlog - 1; i >= 0; i--) {
  106. if ((n >> i) & 1) {
  107. if (lst == -1) {
  108. for (int j = i + 1; j < mlog; j++) {
  109. if (cnt[j]) {
  110. lst = j;
  111. break;
  112. }
  113. }
  114. }
  115. if (lst == -1) {
  116. cout << "-1\n";
  117. return;
  118. }
  119. cnt[lst]--;
  120. cnt[i] += (1LL << lst) / (1LL << i) - 1;
  121. ans += lst - i;
  122. // lst = i;
  123. }
  124. if (cnt[i]) lst = i;
  125. }
  126. cout << ans << "\n";
  127. }
  128.  
  129. signed main() {
  130. ios_base::sync_with_stdio(0);
  131. cin.tie(0);
  132. cout.tie(0);
  133. int q = 1;
  134. cin >> q;
  135. while (q--) solve();
  136. }
  137. /*
  138. 10 3
  139. 1 32 1
  140. -> 2
  141. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement