본문 바로가기

카테고리 없음

codeforce Cirno's Perfect Bitmasks Classroom

#include <bits/stdc++.h>

    using namespace std;
    int main() {
        ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

        int t;
        cin >> t;
        while (t--) {
            long long x;
            cin >> x;
            long long ans = 0;
            for (int i = 0; i < 33; i++) {
                 if ((x & (1LL << i))) {
                        ans += (1LL << i);
                        break;
                    }
                }

            if ((x ^ ans) > 0) {
                cout << ans << '\n';
                    continue;
                }

                for (int i = 0; i < 33; i++) {
                	if ((x & (1LL << i)) == 0) {
                    ans += (1LL << i);
                    break;
                }
            }
            cout << ans << '\n';

문제 

양의 정수 x가 주어지면 x and y > 0 , x xor y > 0 에 조건에 맞는 최소 y를 출력한다.

풀이 방법

X와 겹치는 쇠소 비트를 찾아 정답에 더해주고, 아래 조건을 만족한다면 그대로 출력, 아니면 x가 가지지 않는 최소 비트를 하나 더 찾아서 출력한다.