-
1303-D, *1900코드포스 2021. 12. 26. 14:33
Problem - D - Codeforces
codeforces.com
풀이
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include <bits/stdc++.h>#define endl "\n"#define ooop(i, n) for(int i = 0; i < n; i++)#define loop(i, n) for(int i = 1; i <= n; i++)#define all(v) (v).begin(), (v).end()using namespace std;typedef long long ll;typedef pair<int, int> pi;typedef pair<ll, ll> pl;map<ll, int> exponent;void make_exponent(map<ll, int>& exponent){ll i = 1;ooop(bit, 30){exponent[i] = bit;i *= 2;}}int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int t;cin >> t;make_exponent(exponent);while(t--){ll n, m;cin >> n >> m;vector<int> cnt(30);ll a, sum = 0;loop(i, m){cin >> a;sum += a;cnt[exponent[a]]++;}if(sum < n) cout << -1 << endl;else{int divide_cnt = 0;ll acc = 0;ooop(bit, 60){ll cur = (1LL<<bit);acc += cnt[bit]*cur;if((cur&n) == 0) continue;if(acc >= cur) acc -= cur;else{int p = bit;while(cnt[p] == 0) p++;divide_cnt += p-bit;cnt[p]--;for(int b = p-1; b >= bit+1; b--) cnt[b]++;acc += cur;}}cout << divide_cnt << endl;}}return 0;}cs※ 1 << 60 은 int 라서 원하는 결과 안나옴!! (1LL << 60 ) 사용해야함!
'코드포스' 카테고리의 다른 글
1623-B (0) 2021.12.29 1622-B (0) 2021.12.29 1303-C, *1600 (0) 2021.12.26 1303-B, *1400 (0) 2021.12.26 1615-C (0) 2021.12.25