1271E - Common Number - CodeForces Solution


binary search combinatorics dp math *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

typedef long long i;
using namespace std;
int main() {
    i n,k;
    cin >> n >> k;
    if (k == n){
        cout << 1;
        return 0;
    } else if (k == n-1){
        cout << 2;
        return 0;
    } else if (k == 1){
        cout << n;
        return 0;
    } else if (k == 2){
        if (n%2 == 0){
            cout << n-2;
            return 0;
        } else{
            cout << n-1;
            return 0;
        }
    }
    vector<i> state = {n, 2, 1}; //max#, even, last
    if (n%2 == 1){
        state[2] = 0;
        state[0] = n+1;
    }
    ///cout << "state " << state[0] << ": " << state[1] << " " << state[2] << endl;
    while (state[0] > 1){
        i new0, new1, new2;
        new1 = (state[1]+1)*2;
        new0 = state[0]/2;
        if (new0%2 == 0){
            new2 = 1 + state[2] + (state[1]/2);
        }
        else{
            new0 -= 1;
            new2 = 2 + state[2] + (state[1]);
        }
        state[0] = new0;
        state[1] = new1;
        state[2] = new2;
        ///cout << "state " << state[0] << ": " << state[1] << " " << state[2] << endl;
        if (k <= state[2]){
            cout << state[0]; return 0;
        } else if (k <= state[1]/2) {
            cout << state[0] - 1; return 0;
        } else if (k <= state[1]){
            if (state[0] > 4){
                cout << state[0] - 2; return 0;
            }
        }
    }
}


Comments

Submit
0 Comments
More Questions

766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament