QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#783353 | #9640. 格雷码 | zjy0001 | 96 | 499ms | 103316kb | C++17 | 1.2kb | 2024-11-26 08:55:38 | 2024-11-26 08:55:38 |
Judging History
answer
#include<bits/stdc++.h>
#define LL long long
#define LLL __int128
#define uint unsigned
#define ldb long double
#define uLL unsigned long long
using namespace std;
inline vector<int>solve(int n){
if(n==1)return {0,0};
if(n==2)return {0,1,0,1};
if(n==3)return {1,0,2,0,1,0,2,0};
vector<int>b(solve(n-2));
int u=(1<<(n-1))/n,v=(1<<(n-1))%n;
if(v>=2)v-=2;
vector<int>cnt(n-2);
for(int i:b)++cnt[i];
--cnt[b.back()];
for(int i=0;i<n-2;++i)cnt[i]=cnt[i]*2-u-(i<v);
vector<int>c(1<<(n-2)),z;
for(int i=0;i<(1<<(n-2))-1;++i)if(cnt[b[i]])--cnt[b[i]],c[i]=1;
c.back()=1;
for(int l=0,r,t=0;l<(1<<(n-2));l=r+1){
for(r=l;!c[r];++r);
for(int i=l;i<r;++i)z.emplace_back(b[i]);
z.emplace_back(n-t-1);
for(int i=r-1;i>=l;--i)z.emplace_back(b[i]);
t=1-t,z.emplace_back(n-t-1);
for(int i=l;i<=r;++i)z.emplace_back(b[i]);
}
z.back()=n-1;
for(int i=(1<<(n-2))-1;i--;)z.emplace_back(b[i]);
z.emplace_back(n-2);
return z;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n;cin>>n;
for(int i:solve(n))cout<<i+1<<' ';
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 4
Accepted
time: 0ms
memory: 3848kb
input:
1
output:
1 1
result:
ok Correct
Test #2:
score: 4
Accepted
time: 0ms
memory: 3628kb
input:
2
output:
1 2 1 2
result:
ok Correct
Test #3:
score: 4
Accepted
time: 0ms
memory: 3828kb
input:
3
output:
2 1 3 1 2 1 3 1
result:
ok Correct
Test #4:
score: 4
Accepted
time: 0ms
memory: 3596kb
input:
4
output:
4 3 1 2 3 2 4 2 1 4 3 4 1 2 1 3
result:
ok Correct
Test #5:
score: 4
Accepted
time: 0ms
memory: 3620kb
input:
5
output:
5 4 2 4 5 1 5 4 3 4 5 1 2 1 3 5 3 1 2 4 2 1 3 5 3 1 2 1 3 1 2 4
result:
ok Correct
Test #6:
score: 4
Accepted
time: 0ms
memory: 3632kb
input:
6
output:
6 5 4 5 6 3 6 5 1 5 6 2 3 6 3 5 3 2 5 6 4 6 5 2 5 6 1 6 5 4 3 4 5 4 3 6 3 4 1 2 1 6 1 2 5 2 1 6 1 2 1 4 3 4 1 2 4 2 3 2 1 3 4 5
result:
ok Correct
Test #7:
score: 4
Accepted
time: 0ms
memory: 3600kb
input:
7
output:
7 6 5 6 7 4 7 6 2 4 6 4 7 4 5 7 6 1 6 7 5 4 7 4 6 4 3 4 5 6 5 4 7 4 5 1 7 6 2 6 7 1 7 6 3 5 6 5 7 5 3 7 6 1 6 7 2 4 2 7 2 4 6 4 2 1 3 5 3 6 3 5 3 7 3 5 3 1 2 1 3 1 2 7 2 1 3 1 2 6 2 1 3 1 2 7 2 1 3 1 2 1 3 5 3 1 2 4 2 1 3 5 3 1 2 1 5 4 3 4 5 1 5 4 2 4 5 6
result:
ok Correct
Test #8:
score: 4
Accepted
time: 0ms
memory: 3592kb
input:
8
output:
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 7 8 5 8 7 6 7 8 2 8 7 3 7 8 6 8 7 3 7 8 5 8 7 3 7 8 2 8 7 5 7 8 6 8 7 4 7 8 6 5 8 5 7 5 2 5 7 5 8 5 6 8 7 1 6 5 7 5 6 8 6 5 4 3 8 3 7 3 4 5 4 3 6 3 4 7 4 3 6 3 4 5 8 5 4 3 6 3 4 1 8 7 2 7 8 1 6 1 2 5 2 1 6 1 2 1 4 3 4 1 2 4 2 3 2 1 3 4 8 4 3 1 2 ...
result:
ok Correct
Test #9:
score: 4
Accepted
time: 0ms
memory: 3564kb
input:
9
output:
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 2 8 9 4 9 8 6 8 9 4 9 8 7 8 9 4 9 8 5 8 9 7 9 8 6 8 9 1 9 8 6 8 9 7 9 8 5 8 9 4 9 8 7 8 9 4 6 9 6 8 6 4 8 9 3 9 8 4 8 9 5 6 9 6 8 6 5 4 8 4 9 4 7 4 9 4 8 4 5 8 9 1 7 6 9 6 7 8 7 6 2 6 7 8 7 6 9 6 7 1 7 6 9 6 7 8 7 6 3 8 9 5 6 9 6 8 6 5 7 5 8 5 7 9 ...
result:
ok Correct
Test #10:
score: 4
Accepted
time: 0ms
memory: 3856kb
input:
10
output:
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 7 10 9 8 9 10 5 10 9 8 9 10 7 10 9 6 7 9 7 10 7 8 10 9 2 9 10 8 7 10 7 9 7 3 7 8 9 8 7 10 7 8 6 8 7 10 7 8...
result:
ok Correct
Test #11:
score: 4
Accepted
time: 1ms
memory: 3656kb
input:
11
output:
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 2 10 11 8 11 10 9 10 11 4 11 10 9 10 11 8 11 10 6 10 11 8 11 10 9 10 11 4 11 10 9 10 1...
result:
ok Correct
Test #12:
score: 4
Accepted
time: 1ms
memory: 3608kb
input:
12
output:
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 10 11 12 5 ...
result:
ok Correct
Test #13:
score: 4
Accepted
time: 1ms
memory: 3688kb
input:
13
output:
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 13 10 13 12...
result:
ok Correct
Test #14:
score: 4
Accepted
time: 1ms
memory: 3796kb
input:
14
output:
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 13 14 11 1...
result:
ok Correct
Test #15:
score: 4
Accepted
time: 2ms
memory: 3752kb
input:
15
output:
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 14 11 14 15 ...
result:
ok Correct
Test #16:
score: 4
Accepted
time: 3ms
memory: 3700kb
input:
16
output:
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 15 12 15 16...
result:
ok Correct
Test #17:
score: 4
Accepted
time: 0ms
memory: 4064kb
input:
17
output:
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 16 13 16 1...
result:
ok Correct
Test #18:
score: 4
Accepted
time: 10ms
memory: 4772kb
input:
18
output:
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 18 17 14 17 ...
result:
ok Correct
Test #19:
score: 4
Accepted
time: 19ms
memory: 6648kb
input:
19
output:
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 19 18 15 18 ...
result:
ok Correct
Test #20:
score: 4
Accepted
time: 31ms
memory: 9712kb
input:
20
output:
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 20 19 16 19 ...
result:
ok Correct
Test #21:
score: 4
Accepted
time: 58ms
memory: 18580kb
input:
21
output:
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 21 20 17 20 ...
result:
ok Correct
Test #22:
score: 4
Accepted
time: 126ms
memory: 29136kb
input:
22
output:
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 22 21 18 21 ...
result:
ok Correct
Test #23:
score: 4
Accepted
time: 248ms
memory: 61532kb
input:
23
output:
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 23 22 19 22 ...
result:
ok Correct
Test #24:
score: 4
Accepted
time: 499ms
memory: 103316kb
input:
24
output:
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 24 23 20 23 ...
result:
ok Correct
Test #25:
score: 0
Time Limit Exceeded
input:
25
output:
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 25 24 21 24 ...