Codeforces Round 828 (Div. 3) 1744D. Divisibility by 2^n Solution
Problem Link : https://codeforces.com/problemset/problem/1744/D
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n ; cin >> n ;
- int cnt = 0 ;
- std::vector<int> v;
- for ( int i = 1 ; i <= n ; i++){
- int x ;
- cin >> x ;
- while (x % 2 == 0){
- x /= 2;
- cnt++ ;
- }
- int cur = i ;
- int temp = 0 ;
- while (cur % 2 == 0) {
- cur /= 2;
- temp++ ;
- }
- v.push_back(temp) ;
- }
- if (cnt >= n ){
- cout <<0<<"\n";
- }
- else {
- int ans = 0 ;
- sort(v.rbegin(),v.rend()) ;
- for ( auto i : v) {
- cnt += i ;
- ans++ ;
- if (cnt >= n){
- cout << ans <<"\n" ;
- break ;
- }
- }
- if ( cnt < n )
- cout <<"-1\n";
- }
- }
- return 0 ;
- }
No comments