Permutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1190 Accepted Submission(s): 756
Special Judge
Problem Description
A permutation p1,p2,...,pn of 1,2,...,n is called a lucky permutation if and only if pi≡0(mod|pi−pi−2|) for i=3...n.
Now you need to construct a lucky permutation with a given n.
Input
The first line is the number of test cases.
For each test case, one single line contains a positive integer n(3≤n≤105).
Output
For each test case, output a single line with n numbers p1,p2,...,pn.
It is guaranteed that there exists at least one solution. And if there are different solutions, print any one of them.
Sample Input
1
6
Sample Output
1 3 2 6 4 5
同余定理
a≡0(mod b)即等于a%b==0;
如果要保证 pi≡0(mod|pi−pi−2|) ,那么保证 pi - pi-2 == 1即可。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define closeio std::ios::sync_with_stdio(false)
int a[100005];
int main()
{
int t,n,i;
cin>>t;
while(t--)
{
cin>>n;
int s=1;
for(i=1;i<=n;i+=2)
{
a[i]=s;
s++;
}
for(i=2;i<=n;i+=2)
{
a[i]=s;
s++;
}
for(i=1;i<=n;i++)
{
cout<<a[i];
if(i!=n)
cout<<" ";
}
cout<<endl;
}
return 0;
}