QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#782232 | #9640. 格雷码 | N_z_ | 100 ✓ | 292ms | 232680kb | C++23 | 7.0kb | 2024-11-25 19:26:20 | 2024-11-25 19:26:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
struct time_helper{
#ifdef LOCAL
clock_t time_last;time_helper(){time_last=clock();}void test(){auto time_now=clock();std::cerr<<"time:"<<1.*(time_now-time_last)/CLOCKS_PER_SEC<<";all_time:"<<1.*time_now/CLOCKS_PER_SEC<<std::endl;time_last=time_now;}~time_helper(){test();}
#else
void test(){}
#endif
}time_helper;
#ifdef LOCAL
#include"dbg.h"
#else
#define dbg(...) (__VA_ARGS__)
#endif
namespace Fread{const int SIZE=1<<16;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return *S++;}}namespace Fwrite{const int SIZE=1<<16;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
int print_precision=10;bool print_T_endl=1;char print_between=' ';
template<typename T>struct is_char{static constexpr bool value=(std::is_same<T,char>::value||std::is_same<T,signed char>::value||std::is_same<T,unsigned char>::value);};template<typename T>struct is_integral_ex{static constexpr bool value=(std::is_integral<T>::value||std::is_same<T,__int128>::value)&&!is_char<T>::value;};template<typename T>struct is_floating_point_ex{static constexpr bool value=std::is_floating_point<T>::value||std::is_same<T,__float128>::value;};namespace Fastio{struct Reader;struct Writer;template<size_t id>struct read_tuple{template<typename...T>static void read(Reader&stream,std::tuple<T...>&x){read_tuple<id-1>::read(stream,x);stream>>get<id-1>(x);}};template<>struct read_tuple<0>{template<typename...T>static void read([[maybe_unused]]Reader&stream,[[maybe_unused]]std::tuple<T...>&x){}};template<size_t id>struct print_tuple{template<typename...T>static void print(Writer&stream,const std::tuple<T...>&x){print_tuple<id-1>::print(stream,x);putchar(print_between);stream<<get<id-1>(x);}};template<>struct print_tuple<1>{template<typename...T>static void print(Writer&stream,const std::tuple<T...>&x){stream<<get<0>(x);}};template<>struct print_tuple<0>{template<typename...T>static void print([[maybe_unused]]Writer&stream,[[maybe_unused]]const std::tuple<T...>&x){}};
struct Reader{template<typename T>typename std::enable_if_t<std::is_class<T>::value,Reader&>operator>>(T&x){for(auto &y:x)*this>>y;return *this;}template<typename...T>Reader&operator>>(std::tuple<T...>&x){read_tuple<sizeof...(T)>::read(*this,x);return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1;while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;return *this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1,s=0;x=0;T t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Reader&>operator>>(T&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}template<typename T1,typename T2>Reader&operator>>(std::pair<T1,T2>&x){*this>>x.first>>x.second;return *this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';
struct Writer{typedef __int128 mxdouble;template<typename T>typename std::enable_if_t<std::is_class<T>::value,Writer&>operator<<(const T&x){for(auto q:x){*this<<q;if(!is_class<decltype(q)>::value)*this<<print_between;}if(!is_class<typename T::value_type>::value&&print_T_endl)*this<<'\n';return *this;}template<typename...T>Writer&operator<<(const std::tuple<T...>&x){print_tuple<sizeof...(T)>::print(*this,x);if(print_T_endl)*this<<'\n';return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Writer&>operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Writer&>operator<<(T x){if(x<0)putchar('-'),x=-x;x+=pow(10,-print_precision)/2;mxdouble _=x;x-=(T)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<print_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<print_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Writer&>operator<<(const T&c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}template<typename T1,typename T2>Writer&operator<<(const std::pair<T1,T2>&x){*this<<x.first<<print_between<<x.second;if(print_T_endl)*this<<'\n';return *this;}Writer&operator<<(const std::string&str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
template<class Fun>class y_combinator_result{Fun fun_;public:template<class T>explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}template<class ...Args>decltype(auto) operator()(Args &&...args){return fun_(std::ref(*this), std::forward<Args>(args)...);}};template<class Fun>decltype(auto) y_combinator(Fun &&fun){return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));}
void init();void solve(int tc);
main()
{
init();int t=1;
// cin>>t;
for(int tc=1;tc<=t;tc++)solve(tc);
}
void init()
{
}
vector<int>work(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>sres=work(n-2);
int u=(1<<n-1)/n,v=(1<<n-1)%n;
if(v>=2)v-=2;
vector<int>cnt(n-2);
for(auto q:sres)cnt[q]++;
cnt[sres.back()]--;
for(int x=0;x<n-2;x++)
cnt[x]=cnt[x]*2-u-(x<v);
vector<int>ful(1<<n-2),res;
for(int x=0;x<(1<<n-2)-1;x++)
if(cnt[sres[x]])cnt[sres[x]]--,ful[x]=1;
ful.back()=1;
for(int l=0,r,now=0;l<1<<n-2;l=r+1)
{
r=l;
while(!ful[r])r++;
for(int x=l;x<r;x++)res.emplace_back(sres[x]);
res.emplace_back(n-1-now);
for(int x=r-1;x>=l;x--)res.emplace_back(sres[x]);
now=!now,res.emplace_back(n-1-now);
for(int x=l;x<r;x++)res.emplace_back(sres[x]);
res.emplace_back(sres[r]);
}
res.back()=n-1;
for(int x=(1<<n-2)-2;x>=0;x--)
res.emplace_back(sres[x]);
res.emplace_back(n-2);
return res;
}
void solve([[maybe_unused]]int tc)
{
int n;
cin>>n;
for(auto q:work(n))cout<<q+1<<' ';cout<<endl;
}
详细
Pretests
Final Tests
Test #1:
score: 4
Accepted
time: 0ms
memory: 3548kb
input:
1
output:
1 1
result:
ok Correct
Test #2:
score: 4
Accepted
time: 0ms
memory: 3560kb
input:
2
output:
1 2 1 2
result:
ok Correct
Test #3:
score: 4
Accepted
time: 0ms
memory: 3616kb
input:
3
output:
2 1 3 1 2 1 3 1
result:
ok Correct
Test #4:
score: 4
Accepted
time: 0ms
memory: 3880kb
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: 3612kb
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: 3560kb
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: 3788kb
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: 3624kb
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: 3576kb
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: 3504kb
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: 0ms
memory: 3676kb
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: 0ms
memory: 3668kb
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: 0ms
memory: 3708kb
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: 4020kb
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: 0ms
memory: 3740kb
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: 1ms
memory: 3808kb
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: 3968kb
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: 0ms
memory: 4584kb
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: 3ms
memory: 6520kb
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: 13ms
memory: 10952kb
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: 17ms
memory: 17916kb
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: 42ms
memory: 29580kb
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: 59ms
memory: 60568kb
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: 125ms
memory: 102608kb
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: 4
Accepted
time: 292ms
memory: 232680kb
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 ...
result:
ok Correct