附和同学口胡大赛题单:https://www.luogu.com.cn/training/1076930#problems
挑战在熄灯后口胡完。
CF1352G Special Permutation
我们有构造 这样构造。
也就是 可以构造 。
这种。
。
这两种都是 作为开头结尾。
那么转化为 有解情况。
根据裴蜀定理, 时有解。但是不一定是非负的。
我们又知道:Frobenius 硬币问题保证若 ,最大需要手动构造的是 。
这题不需要 exgcd,直接枚举 即可。
复杂度 。
对于 ,构造 。
对于 ,可以枚举证明无解。
对于 ,。
对于 ,。
对于 ,。
然后对于 还要构造,自己手动一下就行。也可以看代码。不是难点。
#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;
}