A 00-indexed array aa of size nn is called good if for all valid indices ii (0≤i≤n−10≤i≤n−1), ai+iai+i is a perfect square††.
Given an integer nn. Find a permutation‡‡ pp of [0,1,2,…,n−1][0,1,2,…,n−1] that is good or determine that no such permutation exists.
†† An integer xx is said to be a perfect square if there exists an integer yy such that x=y2x=y2.
‡‡ An array bb is a permutation of an array aa if bb consists of the elements of aa in arbitrary order. For example, [4,2,3,4][4,2,3,4] is a permutation of [3,2,4,4][3,2,4,4] while [1,2,2][1,2,2] is not a permutation of [1,2,3][1,2,3].
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The only line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the length of the permutation pp.
It is guaranteed that the sum of nn over all test cases does not exceed 105105.
Output
For each test case, output nn distinct integers p0,p1,…,pn−1p0,p1,…,pn−1 (0≤pi≤n−10≤pi≤n−1) — the permutation pp — if the answer exists, and −1−1 otherwise.
Example
input
Copy
3 3 4 7
output
Copy
1 0 2 0 3 2 1 1 0 2 6 5 4 3
Note
In the first test case, we have n=3n=3. The array p=[1,0,2]p=[1,0,2] is good since 1+0=121+0=12, 0+1=120+1=12, and 2+2=222+2=22
In the second test case, we have n=4n=4. The array p=[0,3,2,1]p=[0,3,2,1] is good since 0+0=020+0=02, 3+1=223+1=22, 2+2=222+2=22, and 1+3=221+3=22.
1,构造题,比赛时应该冷静的发现数据之间的关系
2,从后往前看,eg。n==18 在8+17==25,所以从i=8开始,17 16 15 。。。。8 i从8到17,而a【i】从17到8,位置上加1,a【i】上减1,总体和仍未某个数的平方
- #include
- using namespace std;
- #define int long long
- #define vec vector
- #define pi 3.1415926
- #define rep(i,l,r) for(int i=l;i<=r;++i)
- const int maxj=2e5+100,mod=998244353,inf=0x7f7f7f7f7f7f7f;
- int a[maxj],vis[maxj];
- void solve(){
- int n;cin>>n;
- for(int i=0;i
//赋值 - a[i]=i;
- }
- vector
int,2>>ve; - int las=n-1;
- while(las>=0){
- int s=ceil(sqrt((double)las));
- s=s*s;
- int pre=s-las;//找配套的数
- ve.push_back({pre,las});
- las=pre-1;//从前边接着找合适的距离
- }
- for(auto [p,q]:ve){
- for(int i=p;i<=q;++i){
- a[i]=p+q-i;//合的数仍为平方数
- }
-
- }
- for(int i=0;i
- cout<' ';
- }cout<<'\n';
- }
- signed main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t;
- t=1;
- cin>>t;
- while(t--)solve();
- return 0;
- }