1656D - K-good - CodeForces Solution


constructive algorithms math number theory

Please click on ads to support us..

Python Code:

import sys
import math
from collections import defaultdict
 
T = int(input())
for _ in range(T):
    n = int(input())
    k=1
    while n%2==0:
        n=n//2
        k*=2
    if n==1:
        print('-1')
    else:
        print(min(n,2*k))
        

C++ Code:

#include <bits/stdc++.h> 

using namespace std;

typedef long long ll;
const ll MOD = 998244353;
const ll mod = 1e9+7;
const ll INF = 2e18;


void solve() {
    ll n; cin >> n;
    ll po2=1;
    while(n%2==0){
        n/=2;
        po2*=2;
    }
    if(n==1) cout << -1 << endl;
    else if(n>=(2*po2+1)) cout << po2*2 << endl;
    else cout << n << endl;

}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll t; cin >> t;
    for(ll i = 0; i < t; i++) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices