1661B - Getting Zero - CodeForces Solution


brute force graphs greedy shortest paths

Please click on ads to support us..

Python Code:

cases = int(input())
a = input()
b = list(map(int, a.split()))
for i in range (cases):
  if b[i] == 0:
    print(0, end=' ')
    continue;
  ans = 16
  for j in range(16):
    k = b[i] + j
    for p in range(16):
      if k%2 == 1:
        ans = min(ans, 15 - p + j)
        break
      k //= 2
  print(ans)

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pos "YES"
#define neg "NO"

using namespace std;

int main(){

    fast_io

    ll n;
    cin >> n;

    ll *a = new ll[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];

    ll msk = 0;
    for (int i = 0; i <= 14; i++)
        msk |= (1<<i);

    for (int i = 0; i < n; i++){
        ll ans = 15;

        for (ll ad = 0; ad <= 15; ad++){
            for (ll m = 0; m <= 15; m++){
                if ((((a[i]+ad)<<m)&msk) == 0)
                    ans = min(ans, ad+m);
            }
        }

        cout << ans << " ";
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word