1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <bits/stdc++.h> #define FOR(i, x, y) for (remove_const<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (remove_const<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) using namespace std; const int maxn = 1E5 + 10; int f[maxn], T, n, d; int ans[maxn];
inline bool check(int d, int k) { return d == -1 || (f[d] <= k && k <= 1LL * d * (d - 1) / 2); }
void fill(int x, int y, int k, int l, int r) { if (x > y) return; assert(check(y - x + 1, k)); int d = y - x; k -= d; while (d >= 0 && check(d, k)) { ans[l++] = x++; k -= --d; } if (d > 0) { int t = (x + y + 1) / 2; while (!check(y - t, k - f[t - x])) --t; assert(t > x); ans[l] = t; fill(x, t - 1, f[t - x], l + 1, l + t - x); fill(t + 1, y, k - f[t - x], l + t - x + 1, r); } }
int main() { FOR(i, 2, maxn) f[i] = i - 1 + f[i / 2] + f[(i - 1) / 2]; cin >> T; while (T--) { scanf("%d%d", &d, &n); if (!check(d, n)) puts("-1"); else { fill(1, d, n, 1, d); FOR(i, 1, d + 1) printf("%d%c", ans[i], i == d ? '\n' : ' '); } } }
|