Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add this #1

Open
aryamanarora opened this issue Dec 24, 2018 · 0 comments
Open

Add this #1

aryamanarora opened this issue Dec 24, 2018 · 0 comments

Comments

@aryamanarora
Copy link
Owner

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
 
#define mp make_pair
#define pb push_back
#define f first
#define s second

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, k;
    cin >> n >> k;
    bitset<64> a(n);
    if (a.count() == k) {
        string s = a.to_string();
        next_permutation(s.begin(), s.end());
        cout << stoull(s, nullptr, 2) << endl;
        return 0;
    }
    while (a.count() != k) {
        if (a.count() > k) {
            int ct = 0, i;
            for (i = 0; i < 64; i++) {
                if (a[i]) {
                    ct++;
                    a[i] = false;
                    if (ct == 2) break;
                }
            }
            i++;
            while (a[i]) {
                a[i] = false;
                i++;
            }
            a[i] = true;
        }
        else {
            for (int i = 0; i < 64; i++) {
                if (!a[i]) {
                    a[i] = true;
                    break;
                }
            }
        }
    }
    cout << a.to_ullong() << endl; 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant