ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [10728] XOR삼형제 1
    BOJ 2022. 2. 21. 09:40

    https://www.acmicpc.net/problem/10728

     

    10728번: XOR삼형제 1

    (위에서 아래로, 오른쪽에서 왼쪽으로 읽어주세요.)

    www.acmicpc.net

    n이 20이면 완전탐색이 가능하다.

    next_permutation()으로 모든 경우를 만들어주고, N^3으로 조건을 만족하는지를 판별한다.


    #include <bits/stdc++.h>
    using namespace std;
    #ifdef ONLINE_JUDGE
    constexpr bool local = false;
    #else
    constexpr bool local = true;
    #endif
    using ll = long long;
    using pi = pair<ll, ll>;
    int n;
    bool f(const vector<int>& v) {
        int i, j, k;
        for (i = 0; i < n; i++) {
            if (!v[i]) continue;
            for (j = 0; j < n; j++) {
                if (!v[j]) continue;
                for (k = 0; k < n; k++) {
                    if (!v[k]) continue;
                    if (((i + 1) ^ (j + 1) ^ (k + 1)) == 0) return false;
                }
            }
        }
        return true;
    }
    int main(void) {
        if (!local) ios_base::sync_with_stdio(0), cin.tie(0);
        int t;
        cin >> t;
        while (t--) {
            cin >> n;
            int i, j;
            for (i = n; i >= 1; i--) {
                vector<int> v(n, 0);
                for (j = 0; j < i; j++) v[j] = 1;
                sort(v.begin(), v.end());
                do {
                    bool ret = f(v);
                    if (ret == false) continue;
                    cout << i << '\n';
                    for (j = 0; j < n; j++)
                        if (v[j] == 1) cout << j + 1 << ' ';
                    cout << '\n';
                    goto findAns;

                } while (next_permutation(v.begin(), v.end()) == true);
            }
        findAns:;
        }
        return 0;
    }

     

    'BOJ' 카테고리의 다른 글

    [2445] 별 찍기 - 8  (0) 2022.02.22
    [20291] 파일 정리  (0) 2022.02.22
    [4991] 로봇 청소기  (0) 2022.02.21
    [13911] 집 구하기  (0) 2022.02.20
    [3020] 개똥벌레  (0) 2022.02.20

    댓글