QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#785369 | #9640. 格雷码 | zhouhuanyi | 100 ✓ | 216ms | 165556kb | C++17 | 2.0kb | 2024-11-26 17:40:26 | 2024-11-26 17:40:29 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#define N 25
#define M 33554432
using namespace std;
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
char obuf[1<<27],*O=obuf;
int n,b[N+1],c[N+1],cs[N+1];
bool used[M+1];
vector<short>solve(int sn)
{
if (sn==1) return {1,1};
else if (sn==2) return {1,2,1,2};
else if (sn==3) return {1,3,1,2,1,3,1,2};
else
{
int cnt=0;
vector<short>p=solve(sn-2);
vector<short>sp;
vector<short>st;
int d=(1<<(sn-1))%sn,lst=-1;
for (int i=1;i<=sn-2;++i) c[i]=0;
for (int i=1;i<=sn-2;++i) cs[i]=((1<<(sn-1))/sn+(i<=d))<<1;
for (int i=0;i<p.size();++i) c[p[i]]++;
for (int i=1;i<=sn-2;++i) b[i]=(c[i]<<1)-(cs[i]>>1);
if (b[p.back()]<2)
{
for (int i=1;i<p.size();++i)
if (b[p[i-1]]>=2)
{
for (int j=0;j<p.size();++j) sp.push_back(p[(i+j)%p.size()]);
p=sp;
break;
}
}
b[p.back()]--;
for (int i=p.size()-1;i>=0;--i)
{
if (b[p[i]]) b[p[i]]--,used[i]=1;
else used[i]=0;
}
for (int i=0;i+1<p.size();++i)
if (used[i])
{
cnt++;
for (int j=lst+1;j<=i-1;++j) st.push_back(p[j]);
if (cnt&1) st.push_back(sn);
else st.push_back(sn-1);
for (int j=i-1;j>=lst+1;--j) st.push_back(p[j]);
if (cnt&1) st.push_back(sn-1);
else st.push_back(sn);
for (int j=lst+1;j<=i;++j) st.push_back(p[j]);
lst=i;
}
for (int i=lst+1;i+1<p.size();++i) st.push_back(p[i]);
st.push_back(sn);
for (int i=p.size()-2;i>=lst+1;--i) st.push_back(p[i]);
st.push_back(sn-1);
for (int i=lst+1;i+1<p.size();++i) st.push_back(p[i]);
st.push_back(sn);
for (int i=p.size()-2;i>=0;--i) st.push_back(p[i]);
st.push_back(sn-1);
return st;
}
}
void print(int x)
{
if(x>9) print(x/10);
*O++=x%10+'0';
}
int main()
{
vector<short>p;
n=read(),p=solve(n);
for (int i=0;i<p.size();++i) print(p[i]),*O++=' ';
*O++='\n',fwrite(obuf,O-obuf,1,stdout);
return 0;
}
详细
Pretests
Final Tests
Test #1:
score: 4
Accepted
time: 1ms
memory: 5608kb
input:
1
output:
1 1
result:
ok Correct
Test #2:
score: 4
Accepted
time: 1ms
memory: 5560kb
input:
2
output:
1 2 1 2
result:
ok Correct
Test #3:
score: 4
Accepted
time: 1ms
memory: 5788kb
input:
3
output:
1 3 1 2 1 3 1 2
result:
ok Correct
Test #4:
score: 4
Accepted
time: 1ms
memory: 5632kb
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: 1ms
memory: 5812kb
input:
5
output:
3 1 2 5 2 1 3 4 3 1 2 1 4 5 3 5 4 1 4 5 2 5 4 5 2 1 3 1 2 1 3 4
result:
ok Correct
Test #6:
score: 4
Accepted
time: 1ms
memory: 5636kb
input:
6
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 2 4 5 6 2 1 6 1 5 1 4 5 6 3 6 5 4 5 6 1 6 5 2 5 6 1 6 5 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: 1ms
memory: 5652kb
input:
7
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 4 5 6 7 2 7 6 5 6 7 4 7 6 5 6 7 2 7 6 1 6 7 3 7 6 1 6 7 2 7 6 1 6 7 3 7 6 7 3 1 2 1 3 1 2 5 4 5 2 5 4 1 4 5 3 5 4 1 2 1 3 4 3 1 2 5 2 1 3 6
result:
ok Correct
Test #8:
score: 4
Accepted
time: 1ms
memory: 5648kb
input:
8
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 5 7 5 2 7 8 5 8 7 6 7 8 1 8 7 6 7 8 5 8 7 6 7 8 1 8 7 2 ...
result:
ok Correct
Test #9:
score: 4
Accepted
time: 0ms
memory: 5564kb
input:
9
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #10:
score: 4
Accepted
time: 1ms
memory: 5644kb
input:
10
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #11:
score: 4
Accepted
time: 1ms
memory: 5860kb
input:
11
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #12:
score: 4
Accepted
time: 0ms
memory: 5836kb
input:
12
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #13:
score: 4
Accepted
time: 1ms
memory: 5600kb
input:
13
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #14:
score: 4
Accepted
time: 0ms
memory: 5696kb
input:
14
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #15:
score: 4
Accepted
time: 1ms
memory: 5768kb
input:
15
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #16:
score: 4
Accepted
time: 0ms
memory: 5816kb
input:
16
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #17:
score: 4
Accepted
time: 2ms
memory: 5756kb
input:
17
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #18:
score: 4
Accepted
time: 3ms
memory: 7920kb
input:
18
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #19:
score: 4
Accepted
time: 6ms
memory: 6620kb
input:
19
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #20:
score: 4
Accepted
time: 3ms
memory: 11716kb
input:
20
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #21:
score: 4
Accepted
time: 22ms
memory: 18116kb
input:
21
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #22:
score: 4
Accepted
time: 37ms
memory: 26408kb
input:
22
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #23:
score: 4
Accepted
time: 62ms
memory: 46152kb
input:
23
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct
Test #24:
score: 4
Accepted
time: 100ms
memory: 85780kb
input:
24
output:
4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 8 1 3 4 5 4 3 1 2 3 2 6 2 3 2 1 3 4 7 4 3 1 2 3 2 6 2 3 2 1 3 4 5 4 3 1 2 3 7 3 8 3 2 4 5 6 8 6 5 4 7 4 5 6 2 1 6 7 6 1 8 1 6 1 5 8 5 7 5 1 4 5 6 7 6 5 4 8 4 5 6 3 6 5 4 5 6 8 6 5 4 5 6 7 6 5 4 5 6 1 7 8 6 5 8 10 8 5 6 8 7 1 6 5 4 5 6 7 6 5 4 5 6 8 6 5 4 5 6 3 6 5 4...
result:
ok Correct
Test #25:
score: 4
Accepted
time: 216ms
memory: 165556kb
input:
25
output:
3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 2 7 2 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 4 7 4 6 9 6 4 7 4 1 4 5 7 5 4 6 4 5 3 5 4 6 4 5 7 5 4 1 2 7 2 6 2 1 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 8 3 1 2 5 2 1 3 4 3 7 3 4 3 1 2 5 2 1 3 6 3 1 2 5 2 1 3 4 3 1 2 6 ...
result:
ok Correct