给出数字n,构造一个符合的数组
很容易想到,
n1时,只有1符合。
n2时,有1 2;2 1符合。
n==3时,有1 2 3;1 3 2;2 3 1;3 2 1;
发现必须分为 1 和 2——n 的两块数字,有某种递归的感觉,答案与2次方有关
于是做出代码
:
#include<iostream>
#include<algorithm>
using namespace std;
#define ffp(x,y,z) for(ll (x) = (y);(x)<=(z);(x++))
#define ffs(x,y,z) for(ll (x) = (y);(x)>=(z);(x--))
#define ll long long int
#define q_ (qd())
long long int qd() {long long w = 1, c, ret;while ((c = getchar()) > '9' || c < '0')w = (c == '-' ? -1 : 1); ret = c - '0';while ((c = getchar()) >= '0' && c <= '9')ret = ret * 10 + c - '0';return ret * w;
}
ll qs(ll a, ll b)
{ll bei = a;a = 1;while (b){if (b & 1) { a = a * bei % MOD; }bei = bei * bei % MOD;b >>= 1;}return a;
}
bool change[200200];
ll n = 0;
void dfs(int now)
{if (now == n + 1) { return; }if (change[now]){dfs(now + 1);cout << now << ' ';}else{cout << now << ' ';dfs(now + 1);}
}int main()
{int t = q_;while (t--){n = q_;ll k = q_;ll temp = n-1;int cnt = n+1;ll ttemp = 1;while (temp){ttemp *= 2;temp--;if (ttemp >= k) { cnt = n - 1; break; }}if (ttemp >= k) { cnt = n - 1; }if (cnt > n) { cout << -1 << endl; continue; }//递归寻找数组k -= 1;temp = k; ttemp = 0;while (temp){temp >>= 1;ttemp++;}for (int i = n-1 ; i >=n-ttemp; i--){if (k & 1) { change[i] = 1; }k >>= 1;}dfs(1);cout << endl;fill(change, change + 1 + n, 0);}return 0;
}