QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#858887#9640. 格雷码hos_lyric#100 ✓318ms167384kbC++144.0kb2025-01-17 08:37:312025-01-17 08:37:31

Judging History

This is the latest submission verdict.

  • [2025-01-17 08:37:31]
  • Judged
  • Verdict: 100
  • Time: 318ms
  • Memory: 167384kb
  • [2025-01-17 08:37:31]
  • Submitted

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


/*
  a b c d e f
  ->
  (00a 01a' 11a) (11b 01b' 00b) (00c 01c' 11c) (11d 01d' 00d) (00e 01e' 11e) (11f 01f' 00f) (10f' 10e' 10d' 10c' 10b' 10a')
  
  fix this
  assume |a| = |b| = 1
  hypercube {00,01,11,10} * {a,b}
  (00a 10a), (10b 11b 11a 01a 01b 00b)
*/

int main() {
  int N;
  for (; ~scanf("%d", &N); ) {
    int n;
    vector<int> es;
    vector<int> cs;
    if (N == 1) {
      n = 1;
      es = {0, 0};
      cs = {1};
    } else if (N % 2 == 0) {
      n = 2;
      es = {0, 1, 0, 1};
      cs = {1, 1};
    } else {
      n = 3;
      es = {0, 1, 0, 2, 0, 1, 0, 2};
      cs = {2, 1, 1};
    }
    for (; n < N; n += 2) {
      vector<int> ccs(n + 2);
      {
        const int quo = (1 << (n + 1)) / (n + 2);
        const int rem = (1 << (n + 1)) % (n + 2);
        for (int e = 0; e < n + 2; ++e) ccs[e] = quo + ((e < rem) ? 1 : 0);
        if (ccs[n] != ccs[n + 1]) reverse(ccs.begin(), ccs.end());
// cerr<<n<<" -> "<<n+2<<": "<<cs<<" -> "<<ccs<<endl;
        assert(ccs[n] == ccs[n + 1]);
      }
      for (int e = 0; e < n; ++e) cs[e] *= 4;
      for (const int i : {0, 1, 2}) assert(--cs[es[i]] >= ccs[es[i]]);
      int last = 2;
      int side = 0;
      vector<int> ees;
      ees.reserve(1 << (n + 2));
      for (int i = 3; i <= 1 << n; ++i) if (i == 1 << n || cs[es[i]] > ccs[es[i]]) {
// cerr<<"["<<last<<", "<<i<<")"<<endl;
        if (i < 1 << n) --cs[es[i]];
        // 00, 01, 11
        ees.push_back(es[last]);
        for (int j = last + 1; j < i; ++j) ees.push_back(es[j]);
        ees.push_back(n + side);
        for (int j = i; --j >= last + 1; ) ees.push_back(es[j]);
        ees.push_back(n + (side ^ 1));
        for (int j = last + 1; j < i; ++j) ees.push_back(es[j]);
        last = i;
        side ^= 1;
      }
      ees.push_back(es[0]);
      ees.push_back(n + 1);
      ees.push_back(es[0]);
      for (int j = 1 << n; --j >= 2; ) ees.push_back(es[j]);
      ees.push_back(n);
      ees.push_back(es[1]);
      ees.push_back(n + 1);
      ees.push_back(es[1]);
      ees.push_back(n);
// cerr<<"ees = "<<ees<<endl;
      assert((int)ees.size() == 1 << (n + 2));
      vector<int> cnt(n + 2, 0);
      for (int i = 0; i < 1 << (n + 2); ++i) ++cnt[ees[i]];
      for (int e = 0; e < n + 2; ++e) assert(cnt[e] == 2 * ccs[e]);
      cs.swap(ccs);
      es.swap(ees);
    }
    
    for (int i = 0; i < 1 << N; ++i) {
      if (i) putchar(' ');
      const int e = es[i] + 1;
      if (e >= 10) putchar('0' + (e / 10));
      putchar('0' + (e % 10));
    }
    puts("");
#ifdef LOCAL
vector<int>xs((1<<N)+1,0);
for(int i=0;i<1<<N;++i)xs[i+1]=xs[i]^1<<es[i];
assert(!xs[1<<N]);
vector<int>freq(1<<N,0);
for(int i=0;i<1<<N;++i)assert(++freq[xs[i]]==1);
#endif
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

1

output:

1 1

result:

ok Correct

Test #2:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

2

output:

1 2 1 2

result:

ok Correct

Test #3:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

3

output:

1 2 1 3 1 2 1 3

result:

ok Correct

Test #4:

score: 4
Accepted
time: 1ms
memory: 3840kb

input:

4

output:

1 3 4 2 4 3 1 4 1 2 1 3 2 4 2 3

result:

ok Correct

Test #5:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

5

output:

1 4 5 3 5 4 1 2 4 2 5 2 1 3 5 3 4 3 1 5 1 3 1 2 1 3 1 4 2 5 2 4

result:

ok Correct

Test #6:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

6

output:

4 5 6 2 6 5 4 5 6 3 6 5 1 5 6 4 1 6 1 5 1 2 1 5 1 6 1 3 2 4 2 3 6 3 2 4 2 5 2 4 2 3 1 6 1 3 2 4 2 3 1 2 1 4 1 3 4 2 4 5 3 6 3 5

result:

ok Correct

Test #7:

score: 4
Accepted
time: 0ms
memory: 3712kb

input:

7

output:

5 6 7 3 7 6 5 6 7 4 7 6 1 6 7 2 7 6 4 6 7 2 7 6 5 6 7 2 7 6 1 6 7 3 5 7 5 6 5 3 4 3 6 3 4 7 4 3 1 5 7 5 6 5 1 3 6 3 7 3 1 2 1 3 1 4 2 5 2 4 7 4 2 5 2 4 1 3 1 2 6 2 1 3 1 4 2 5 2 4 1 7 1 4 2 5 2 4 1 3 1 2 1 3 1 5 1 3 4 3 5 3 1 2 5 2 4 2 1 4 5 3 5 6 4 7 4 6

result:

ok Correct

Test #8:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

8

output:

6 7 8 2 8 7 6 7 8 5 8 7 4 7 8 5 8 7 6 7 8 3 8 7 6 7 8 5 8 7 1 5 6 7 6 5 8 5 6 4 8 7 1 6 7 6 8 6 1 5 8 5 7 5 1 7 8 2 8 7 1 5 7 5 8 5 1 6 8 6 7 6 1 7 8 3 8 7 2 7 8 4 8 7 2 7 8 3 6 8 6 7 6 3 7 8 2 4 8 4 7 4 2 5 7 5 8 5 2 4 8 4 7 4 2 3 7 3 8 3 1 6 1 3 2 4 2 3 1 2 1 4 1 3 4 2 4 5 3 6 3 5 8 5 3 6 3 5 4 2 ...

result:

ok Correct

Test #9:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

9

output:

7 8 9 3 9 8 7 8 9 6 9 8 5 8 9 6 9 8 7 8 9 4 9 8 7 8 9 6 9 8 1 8 9 6 9 8 7 8 9 2 9 8 7 8 9 6 9 8 4 8 9 6 9 8 7 8 9 2 9 8 7 8 9 6 9 8 5 6 7 8 7 6 9 6 7 2 7 6 9 6 7 8 7 6 1 6 7 8 7 6 9 6 7 3 9 8 5 7 8 7 9 7 5 6 9 6 8 6 5 8 9 3 9 8 4 8 9 3 6 9 6 8 6 3 8 9 4 7 9 7 8 7 4 8 9 3 9 8 1 8 9 5 7 9 7 8 7 5 6 5 ...

result:

ok Correct

Test #10:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

10

output:

8 9 10 2 10 9 8 9 10 7 10 9 6 9 10 7 10 9 8 9 10 5 10 9 8 9 10 7 10 9 4 9 10 7 10 9 8 9 10 5 10 9 8 9 10 7 10 9 6 9 10 7 10 9 8 9 10 3 10 9 8 9 10 7 10 9 6 9 10 7 10 9 8 9 10 5 10 9 8 9 10 7 10 9 1 9 10 5 10 9 6 9 10 7 10 9 6 9 10 5 10 9 8 9 10 5 10 9 6 9 10 4 10 9 8 9 10 7 10 9 1 9 10 6 10 9 7 9 10...

result:

ok Correct

Test #11:

score: 4
Accepted
time: 1ms
memory: 3840kb

input:

11

output:

9 10 11 3 11 10 9 10 11 8 11 10 7 10 11 8 11 10 9 10 11 6 11 10 9 10 11 8 11 10 5 10 11 8 11 10 9 10 11 6 11 10 9 10 11 8 11 10 7 10 11 8 11 10 9 10 11 4 11 10 9 10 11 8 11 10 7 10 11 8 11 10 9 10 11 6 11 10 9 10 11 8 11 10 1 10 11 8 11 10 9 10 11 6 11 10 9 10 11 8 11 10 7 10 11 8 11 10 9 10 11 2 11...

result:

ok Correct

Test #12:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

12

output:

10 11 12 2 12 11 10 11 12 9 12 11 8 11 12 9 12 11 10 11 12 7 12 11 10 11 12 9 12 11 6 11 12 9 12 11 10 11 12 7 12 11 10 11 12 9 12 11 8 11 12 9 12 11 10 11 12 5 12 11 10 11 12 9 12 11 8 11 12 9 12 11 10 11 12 7 12 11 10 11 12 9 12 11 4 11 12 9 12 11 10 11 12 7 12 11 10 11 12 9 12 11 8 11 12 9 12 11 ...

result:

ok Correct

Test #13:

score: 4
Accepted
time: 0ms
memory: 3840kb

input:

13

output:

11 12 13 3 13 12 11 12 13 10 13 12 9 12 13 10 13 12 11 12 13 8 13 12 11 12 13 10 13 12 7 12 13 10 13 12 11 12 13 8 13 12 11 12 13 10 13 12 9 12 13 10 13 12 11 12 13 6 13 12 11 12 13 10 13 12 9 12 13 10 13 12 11 12 13 8 13 12 11 12 13 10 13 12 5 12 13 10 13 12 11 12 13 8 13 12 11 12 13 10 13 12 9 12 ...

result:

ok Correct

Test #14:

score: 4
Accepted
time: 0ms
memory: 3968kb

input:

14

output:

12 13 14 2 14 13 12 13 14 11 14 13 10 13 14 11 14 13 12 13 14 9 14 13 12 13 14 11 14 13 8 13 14 11 14 13 12 13 14 9 14 13 12 13 14 11 14 13 10 13 14 11 14 13 12 13 14 7 14 13 12 13 14 11 14 13 10 13 14 11 14 13 12 13 14 9 14 13 12 13 14 11 14 13 6 13 14 11 14 13 12 13 14 9 14 13 12 13 14 11 14 13 10...

result:

ok Correct

Test #15:

score: 4
Accepted
time: 1ms
memory: 3968kb

input:

15

output:

13 14 15 3 15 14 13 14 15 12 15 14 11 14 15 12 15 14 13 14 15 10 15 14 13 14 15 12 15 14 9 14 15 12 15 14 13 14 15 10 15 14 13 14 15 12 15 14 11 14 15 12 15 14 13 14 15 8 15 14 13 14 15 12 15 14 11 14 15 12 15 14 13 14 15 10 15 14 13 14 15 12 15 14 7 14 15 12 15 14 13 14 15 10 15 14 13 14 15 12 15 1...

result:

ok Correct

Test #16:

score: 4
Accepted
time: 1ms
memory: 3996kb

input:

16

output:

14 15 16 2 16 15 14 15 16 13 16 15 12 15 16 13 16 15 14 15 16 11 16 15 14 15 16 13 16 15 10 15 16 13 16 15 14 15 16 11 16 15 14 15 16 13 16 15 12 15 16 13 16 15 14 15 16 9 16 15 14 15 16 13 16 15 12 15 16 13 16 15 14 15 16 11 16 15 14 15 16 13 16 15 8 15 16 13 16 15 14 15 16 11 16 15 14 15 16 13 16 ...

result:

ok Correct

Test #17:

score: 4
Accepted
time: 1ms
memory: 4224kb

input:

17

output:

15 16 17 3 17 16 15 16 17 14 17 16 13 16 17 14 17 16 15 16 17 12 17 16 15 16 17 14 17 16 11 16 17 14 17 16 15 16 17 12 17 16 15 16 17 14 17 16 13 16 17 14 17 16 15 16 17 10 17 16 15 16 17 14 17 16 13 16 17 14 17 16 15 16 17 12 17 16 15 16 17 14 17 16 9 16 17 14 17 16 15 16 17 12 17 16 15 16 17 14 17...

result:

ok Correct

Test #18:

score: 4
Accepted
time: 1ms
memory: 4736kb

input:

18

output:

16 17 18 2 18 17 16 17 18 15 18 17 14 17 18 15 18 17 16 17 18 13 18 17 16 17 18 15 18 17 12 17 18 15 18 17 16 17 18 13 18 17 16 17 18 15 18 17 14 17 18 15 18 17 16 17 18 11 18 17 16 17 18 15 18 17 14 17 18 15 18 17 16 17 18 13 18 17 16 17 18 15 18 17 10 17 18 15 18 17 16 17 18 13 18 17 16 17 18 15 1...

result:

ok Correct

Test #19:

score: 4
Accepted
time: 5ms
memory: 6136kb

input:

19

output:

17 18 19 3 19 18 17 18 19 16 19 18 15 18 19 16 19 18 17 18 19 14 19 18 17 18 19 16 19 18 13 18 19 16 19 18 17 18 19 14 19 18 17 18 19 16 19 18 15 18 19 16 19 18 17 18 19 12 19 18 17 18 19 16 19 18 15 18 19 16 19 18 17 18 19 14 19 18 17 18 19 16 19 18 11 18 19 16 19 18 17 18 19 14 19 18 17 18 19 16 1...

result:

ok Correct

Test #20:

score: 4
Accepted
time: 11ms
memory: 8740kb

input:

20

output:

18 19 20 2 20 19 18 19 20 17 20 19 16 19 20 17 20 19 18 19 20 15 20 19 18 19 20 17 20 19 14 19 20 17 20 19 18 19 20 15 20 19 18 19 20 17 20 19 16 19 20 17 20 19 18 19 20 13 20 19 18 19 20 17 20 19 16 19 20 17 20 19 18 19 20 15 20 19 18 19 20 17 20 19 12 19 20 17 20 19 18 19 20 15 20 19 18 19 20 17 2...

result:

ok Correct

Test #21:

score: 4
Accepted
time: 20ms
memory: 13820kb

input:

21

output:

19 20 21 3 21 20 19 20 21 18 21 20 17 20 21 18 21 20 19 20 21 16 21 20 19 20 21 18 21 20 15 20 21 18 21 20 19 20 21 16 21 20 19 20 21 18 21 20 17 20 21 18 21 20 19 20 21 14 21 20 19 20 21 18 21 20 17 20 21 18 21 20 19 20 21 16 21 20 19 20 21 18 21 20 13 20 21 18 21 20 19 20 21 16 21 20 19 20 21 18 2...

result:

ok Correct

Test #22:

score: 4
Accepted
time: 40ms
memory: 24040kb

input:

22

output:

20 21 22 2 22 21 20 21 22 19 22 21 18 21 22 19 22 21 20 21 22 17 22 21 20 21 22 19 22 21 16 21 22 19 22 21 20 21 22 17 22 21 20 21 22 19 22 21 18 21 22 19 22 21 20 21 22 15 22 21 20 21 22 19 22 21 18 21 22 19 22 21 20 21 22 17 22 21 20 21 22 19 22 21 14 21 22 19 22 21 20 21 22 17 22 21 20 21 22 19 2...

result:

ok Correct

Test #23:

score: 4
Accepted
time: 75ms
memory: 44536kb

input:

23

output:

21 22 23 3 23 22 21 22 23 20 23 22 19 22 23 20 23 22 21 22 23 18 23 22 21 22 23 20 23 22 17 22 23 20 23 22 21 22 23 18 23 22 21 22 23 20 23 22 19 22 23 20 23 22 21 22 23 16 23 22 21 22 23 20 23 22 19 22 23 20 23 22 21 22 23 18 23 22 21 22 23 20 23 22 15 22 23 20 23 22 21 22 23 18 23 22 21 22 23 20 2...

result:

ok Correct

Test #24:

score: 4
Accepted
time: 154ms
memory: 85544kb

input:

24

output:

22 23 24 2 24 23 22 23 24 21 24 23 20 23 24 21 24 23 22 23 24 19 24 23 22 23 24 21 24 23 18 23 24 21 24 23 22 23 24 19 24 23 22 23 24 21 24 23 20 23 24 21 24 23 22 23 24 17 24 23 22 23 24 21 24 23 20 23 24 21 24 23 22 23 24 19 24 23 22 23 24 21 24 23 16 23 24 21 24 23 22 23 24 19 24 23 22 23 24 21 2...

result:

ok Correct

Test #25:

score: 4
Accepted
time: 318ms
memory: 167384kb

input:

25

output:

23 24 25 3 25 24 23 24 25 22 25 24 21 24 25 22 25 24 23 24 25 20 25 24 23 24 25 22 25 24 19 24 25 22 25 24 23 24 25 20 25 24 23 24 25 22 25 24 21 24 25 22 25 24 23 24 25 18 25 24 23 24 25 22 25 24 21 24 25 22 25 24 23 24 25 20 25 24 23 24 25 22 25 24 17 24 25 22 25 24 23 24 25 20 25 24 23 24 25 22 2...

result:

ok Correct