C. Build Permutation(构造,思维)

原题链接

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<bits/stdc++.h>
using namespace std;
#define int long long
#define vec vector<int>
#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<n;++i){//赋值
        a[i]=i;
    }
    vector<array<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<n;++i){
        cout<<a[i]<<' ';
    }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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值