QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#304287#8004. Bit Componentucup-team133#AC ✓7ms5264kbC++234.7kb2024-01-13 17:15:542024-01-13 17:15:55

Judging History

你现在查看的是最新测评结果

  • [2024-01-13 17:15:55]
  • 评测
  • 测评结果:AC
  • 用时:7ms
  • 内存:5264kb
  • [2024-01-13 17:15:54]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

vector<string> perm_to_field(int n, const vector<int>& p) {
    int w = topbit(n) + 1;
    vector<string> v;
    for (int i = 0; i < n; i++) {
        string s = "";
        for (int j = 0; j < w; j++) s += char('0' + (p[i] >> j & 1));
        ranges::reverse(s);
        v.emplace_back(s);
    }
    return v;
}

int component(int n, const vector<int>& p) {
    int w = topbit(n) + 1;
    auto v = perm_to_field(n, p);
    vector seen(n, vector<bool>(w, false));
    int res = 0;
    auto dfs = [&](auto self, int x, int y) -> void {
        seen[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (clamp(nx, 0, n - 1) != nx or clamp(ny, 0, w - 1) != ny) continue;
            if (v[nx][ny] != '1') continue;
            if (seen[nx][ny]) continue;
            self(self, nx, ny);
        }
    };

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < w; j++) {
            if (v[i][j] != '1') continue;
            if (seen[i][j]) continue;
            dfs(dfs, i, j);
            res++;
        }
    }

    return res;
}

vector<int> gray_code(int n) {
    vector<int> res;
    for (int i = 0; i < n; i++) res.emplace_back(i ^ (i >> 1));
    return res;
}

vector<int> solve(int n) {
    if (n == 1) return {1};
    if (n == 2) return {};
    if (n == 3) return {2, 3, 1};
    int top = topbit(n);
    if (n < (1 << top) + (1 << (top - 1)) + 1) return {};
    if (n == (1 << (top + 1)) - 1) {
        auto ord = gray_code(1 << (top + 1));
        return vector<int>(ord.begin() + 1, ord.end());
    }
    vector<int> ans;
    auto org = gray_code(1 << top);
    int f = (1 << (top - 1)) + (1 << (top - 2)), g = f + 1;
    for (int i = 1; i < int(org.size()); i++) {
        ans.emplace_back(org[i]);
        if ((org[i] >> (top - 1) & 1)) {
            int val = org[i] + (1 << top);
            if (val <= n and val != f * 2 and val != f * 2 + 1) ans.emplace_back(org[i] + (1 << top));
        }
        if (org[i] == f) {
            assert(org[i + 1] == g);
            ans.emplace_back((1 << top) + (1 << (top - 1)));
            auto org2 = gray_code(1 << (top - 1));
            ans.emplace_back(1 << top);
            ranges::reverse(org2);
            org2.pop_back();
            for (int& val : org2) ans.emplace_back(val + (1 << top));
            ans.emplace_back((1 << top) + (1 << (top - 1)) + 1);
        }
    }
    return ans;
}

bool check(int n, const vector<int>& p) {
    vector<bool> used(n + 1, false);
    if (int(p.size()) != n) return false;
    for (const int& val : p) {
        if (val < 1 or n < val) return false;
        if (used[val]) return false;
        used[val] = true;
    }
    return component(n, p) == 1;
}

void test() {
    for (int i = 1; i < 50; i++) {
        auto ans = solve(i);
        if (not ans.empty()) {
            if (not check(i, ans)) {
                debug(i);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;

    auto ans = solve(n);
    if (ans.empty()) {
        cout << "NO\n";
        return 0;
    }
    cout << "YES\n";
    for (int i = 0; i < n; i++) cout << ans[i] << (i + 1 == n ? "\n" : " ");
    return 0;
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3612kb

input:

1

output:

YES
1

result:

ok answer is 1

Test #2:

score: 0
Accepted
time: 0ms
memory: 3784kb

input:

2

output:

NO

result:

ok answer is 0

Test #3:

score: 0
Accepted
time: 0ms
memory: 3824kb

input:

3

output:

YES
2 3 1

result:

ok answer is 1

Test #4:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

4

output:

NO

result:

ok answer is 0

Test #5:

score: 0
Accepted
time: 0ms
memory: 3544kb

input:

5

output:

NO

result:

ok answer is 0

Test #6:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

6

output:

NO

result:

ok answer is 0

Test #7:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

7

output:

YES
1 3 2 6 7 5 4

result:

ok answer is 1

Test #8:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

8

output:

NO

result:

ok answer is 0

Test #9:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

9

output:

NO

result:

ok answer is 0

Test #10:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

10

output:

NO

result:

ok answer is 0

Test #11:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

11

output:

NO

result:

ok answer is 0

Test #12:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

12

output:

NO

result:

ok answer is 0

Test #13:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

13

output:

YES
1 3 2 6 12 8 10 11 9 13 7 5 4

result:

ok answer is 1

Test #14:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

14

output:

YES
1 3 2 6 14 12 8 10 11 9 13 7 5 4

result:

ok answer is 1

Test #15:

score: 0
Accepted
time: 0ms
memory: 3828kb

input:

15

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8

result:

ok answer is 1

Test #16:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

16

output:

NO

result:

ok answer is 0

Test #17:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

17

output:

NO

result:

ok answer is 0

Test #18:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

23

output:

NO

result:

ok answer is 0

Test #19:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

24

output:

NO

result:

ok answer is 0

Test #20:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

25

output:

YES
1 3 2 6 7 5 4 12 24 16 20 21 23 22 18 19 17 25 13 15 14 10 11 9 8

result:

ok answer is 1

Test #21:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

26

output:

YES
1 3 2 6 7 5 4 12 24 16 20 21 23 22 18 19 17 25 13 15 14 10 26 11 9 8

result:

ok answer is 1

Test #22:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

27

output:

YES
1 3 2 6 7 5 4 12 24 16 20 21 23 22 18 19 17 25 13 15 14 10 26 11 27 9 8

result:

ok answer is 1

Test #23:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

40

output:

NO

result:

ok answer is 0

Test #24:

score: 0
Accepted
time: 0ms
memory: 3504kb

input:

53

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 48 32 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 49 25 27 26 30 31 29 28 20 52 21 53 23 22 18 50 19 51 17 16

result:

ok answer is 1

Test #25:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

93

output:

NO

result:

ok answer is 0

Test #26:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

105

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 96 64 80 81 83 82 86 87 85 84 92 93 95 94 90 91 89 88 72 73 75 74 78 79 77 76 68 69 71 70 66 67 65 97 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 104 41 105 43 42 46 47 45 44 36 100 37 101 39 103 38 102 34...

result:

ok answer is 1

Test #27:

score: 0
Accepted
time: 0ms
memory: 3764kb

input:

132

output:

NO

result:

ok answer is 0

Test #28:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

221

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 192 128 160 161 163 162 166 167 165 164 172 173 175 174 170 171 169 168 184 185 187 186 190 191 189 188 180 181 1...

result:

ok answer is 1

Test #29:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

373

output:

NO

result:

ok answer is 0

Test #30:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

473

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #31:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

513

output:

NO

result:

ok answer is 0

Test #32:

score: 0
Accepted
time: 0ms
memory: 3572kb

input:

934

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #33:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

1356

output:

NO

result:

ok answer is 0

Test #34:

score: 0
Accepted
time: 0ms
memory: 3800kb

input:

1651

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #35:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

2263

output:

NO

result:

ok answer is 0

Test #36:

score: 0
Accepted
time: 1ms
memory: 3600kb

input:

3330

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #37:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

4375

output:

NO

result:

ok answer is 0

Test #38:

score: 0
Accepted
time: 1ms
memory: 3592kb

input:

7989

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #39:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

10925

output:

NO

result:

ok answer is 0

Test #40:

score: 0
Accepted
time: 1ms
memory: 3744kb

input:

14097

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #41:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

16893

output:

NO

result:

ok answer is 0

Test #42:

score: 0
Accepted
time: 2ms
memory: 3684kb

input:

28913

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #43:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

40092

output:

NO

result:

ok answer is 0

Test #44:

score: 0
Accepted
time: 3ms
memory: 3904kb

input:

54980

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #45:

score: 0
Accepted
time: 0ms
memory: 3548kb

input:

88104

output:

NO

result:

ok answer is 0

Test #46:

score: 0
Accepted
time: 6ms
memory: 4096kb

input:

106284

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #47:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

152797

output:

NO

result:

ok answer is 0

Test #48:

score: 0
Accepted
time: 7ms
memory: 5264kb

input:

200000

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #49:

score: 0
Accepted
time: 1ms
memory: 3684kb

input:

3073

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #50:

score: 0
Accepted
time: 1ms
memory: 3692kb

input:

16383

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #51:

score: 0
Accepted
time: 2ms
memory: 3916kb

input:

32767

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #52:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

399

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 97 99 98 102 103 101 100 108 109 111 110 106 107 105 104 120 121 123 122 126 127 125 124 116 117 119 118 114 115 ...

result:

ok answer is 1

Test #53:

score: 0
Accepted
time: 0ms
memory: 3508kb

input:

5757

output:

NO

result:

ok answer is 0

Test #54:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

179

output:

NO

result:

ok answer is 0

Test #55:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

228

output:

YES
1 3 2 6 7 5 4 12 13 15 14 10 11 9 8 24 25 27 26 30 31 29 28 20 21 23 22 18 19 17 16 48 49 51 50 54 55 53 52 60 61 63 62 58 59 57 56 40 41 43 42 46 47 45 44 36 37 39 38 34 35 33 32 96 224 192 128 160 161 163 162 166 167 165 164 172 173 175 174 170 171 169 168 184 185 187 186 190 191 189 188 180 1...

result:

ok answer is 1

Extra Test:

score: 0
Extra Test Passed