版本比较
我不会构造
正文
增加 63 行删除 2 行
14 行未改动15161718191920212223242526272829302631323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868714 行未改动这两种都是 $k+1,k+4$ 作为开头结尾。
那么转化为 $5x+6y=n$ 有解情况。
根据裴蜀定理,$\gcd(5,6)|n$ 时有解。即 $n(n>10)$ 任意。根据裴蜀定理,$\gcd(5,6)|n$ 时有解。但是不一定是非负的。
我们又知道:Frobenius 硬币问题保证若 $\gcd(a,b)=1$,最大需要手动构造的是 $ab-a-b=19$。
这题不需要 exgcd,直接枚举 $x$ 即可。
复杂度 $O(n)$。
对于 $n=4$,构造 $[3,1,4,2]$。
对于 $n< 4$,可以枚举证明无解。
对于 $n=7,8,9$,手动构造对于 $n=7$,$[1,3,5,2,6,4,7]$。
对于 $n=8$,$[1,3,5,7,6,8,4,2]$。
对于 $n=9$,$[1,3,5,9,7,8,6,4,2]$。
然后对于 $n=13,14,19$ 还要构造,自己手动一下就行。也可以看代码。不是难点。
```cpp#include<bits/stdc++.h>#include<cassert>using namespace std;void work5(int k){ cout<<k+1<<' '<<k+3<<' '<<k+5<<' '<<k+2<<' '<<k+4<<' ';}void work6(int k){ cout<<k+1<<' '<<k+3<<' '<<k+5<<' '<<k+2<<' '<<k+6<<' '<<k+4<<' ';}signed main(){ ios::sync_with_stdio(0);cin.tie(0); // freopen(".in","r",stdin); // freopen(".out","w",stdout); int t; cin>>t; while(t--){ int n; cin>>n; if(n<4)cout<<"-1\n"; else if(n==4)cout<<"3 1 4 2\n"; else if(n==5)cout<<"1 3 5 2 4\n"; else if(n==6)cout<<"1 3 5 2 6 4\n"; else if(n==7)cout<<"1 3 5 2 6 4 7\n"; else if(n==8)cout<<"1 3 5 7 4 2 6 8\n"; else if(n==9)cout<<"1 3 5 9 7 4 2 6 8\n"; else if(n==10)cout<<"1 3 5 2 4 6 8 10 7 9\n"; else if(n==13)cout<<"13 9 7 11 8 4 1 3 5 2 6 10 12\n";//样例 else if(n==14)cout<<"1 3 5 7 9 11 13 10 14 12 8 6 4 2\n"; else if(n==19)cout<<"1 3 5 7 9 11 13 15 17 19 16 18 14 12 10 8 6 4 2\n"; else{ int p=0,x=0,y; for(;x*5<=n;x++){ if((n-x*5)%6==0){ y=(n-x*5)/6; break; } } // cout<<x<<' '<<y<<endl; assert(x>=0); assert(y>=0); while(x--)work5(p),p+=5; while(y--)work6(p),p+=6; cout<<'\n'; } } return 0;}```