QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#445126 | #8529. Balance of Permutation | ucup-team3215# | TL | 7068ms | 761412kb | C++20 | 3.1kb | 2024-06-15 23:43:05 | 2024-06-15 23:43:06 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC target "abm,bmi,bmi2"
using namespace std;
constexpr int N = 1e6;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
u64 splitmix(u64 x) {
x += 0x9e3779b97f4a7c15;
(x ^= x >> 30) *= 0xbf58476d1ce4e5b9;
(x ^= x >> 27) *= 0x94d049bb133111eb;
return x ^ (x >> 31);
}
template <typename V>
struct umap {
vector<u64> keys;
vector<V> vals;
vector<u32> nx;
vector<u32> head;
int cap{};
int geti(u64 k) {
reserve();
k = splitmix(k);
for (int i = head[k & cap]; ~i; i = nx[i]) if (keys[i] == k) return i;
nx.push_back(head[k & cap]);
head[k & cap] = keys.size();
keys.push_back(k);
vals.push_back({});
return vals.size() - 1;
}
void reserve() {
if (keys.size() * 4 < cap) return;
cap = max(cap * 2 + 1, 7);
head.assign(cap + 1, -1);
nx.clear();
for (int i = 0; i < keys.size(); ++i) {
nx.push_back(head[keys[i] & cap]);
head[keys[i] & cap] = i;
}
}
};
int n, b;
umap<u64> mem_;
u64 cnt_(int l, int c, int b, u32 m) {
if (m >> l & 1) ++c, m -= 1 << l;
if (l == n || b < 0) return !b;
int loc = mem_.geti(l * 8192 + c * 256ull + b << 32 | m);
if (mem_.vals[loc]) return ~mem_.vals[loc];
int mx = (2 * n - 1 - c - 2 * l) * c / 2;
for (int i = n - c, t = m; --i > l; ) {
if (int d = __builtin_ctz(t); d < i) mx += i - d, t &= t - 1;
else break;
}
if (b < 0 || b > mx) return mem_.vals[loc] = -1, 0;
u64 ans{};
if (c) ans += cnt_(l + 1, c - 1, b - c + 1, m) * c;
for (u32 t = m; t; t &= t - 1) ans += cnt_(l + 1, c, b - c, m - (t & -t));
mem_.vals[loc] = ~ans;
return ans;
}
umap<u128> mem;
u128 cnt(int l, int c, int b, u32 m) {
if (n - l < 21) return cnt_(l, c, b, m);
if (m >> l & 1) ++c, m -= 1 << l;
if (l == n || b < 0) return !b;
int loc = mem.geti(l * 8192 + c * 256ull + b << 32 | m);
if (mem.vals[loc]) return ~mem.vals[loc];
int mx = (2 * n - 1 - c - 2 * l) * c / 2;
for (int i = n - c, t = m; --i > l; ) {
if (int d = __builtin_ctz(t); d < i) mx += i - d, t &= t - 1;
else break;
}
if (b < 0 || b > mx) return mem.vals[loc] = -1, 0;
u128 ans{};
if (c) ans += cnt(l + 1, c - 1, b - c + 1, m) * c;
for (u32 t = m; t; t &= t - 1) ans += cnt(l + 1, c, b - c, m - (t & -t));
mem.vals[loc] = ~ans;
return ans;
}
u128 cnt(int l, int b, u32 m) {
int c = 0;
for (int i = 0; i < l; ++i) if (m & 1 << i) {
++c;
b -= l - i;
m ^= 1 << i;
}
auto ans = cnt(l, c, b, m);
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> b; b /= 2;
u128 k{};
for (char c; cin >> c && isdigit(c); ) k = k * 10 + c - '0';
u32 ans[30], l = 0, m = (1 << n) - 1;
while (l < n)
for (int i = 0; i < n; ++i) if (m & 1 << i) if (auto t = cnt(l + 1, b - max((int)l - i, {}), m - (1 << i)); t >= k) {
b -= max((int)l - i, {});
ans[l++] = i + 1;
m -= 1 << i;
break;
} else k -= t;
for (int i = 0; i < n; ++i) cout << ans[i] << ' ';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3608kb
input:
6 6 6
output:
1 2 6 3 4 5
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 7068ms
memory: 692532kb
input:
30 300 3030303030303030303030
output:
1 2 3 4 9 23 20 28 24 16 21 17 27 29 8 26 25 30 19 18 22 12 7 13 6 10 5 15 14 11
result:
ok 30 numbers
Test #3:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
1 0 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
2 0 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3716kb
input:
2 2 1
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
5 8 3
output:
1 5 4 2 3
result:
ok 5 number(s): "1 5 4 2 3"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
7 20 100
output:
3 6 7 4 1 5 2
result:
ok 7 numbers
Test #8:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
7 2 6
output:
2 1 3 4 5 6 7
result:
ok 7 numbers
Test #9:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
7 24 1
output:
4 5 6 7 1 2 3
result:
ok 7 numbers
Test #10:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
7 22 360
output:
7 6 4 3 5 2 1
result:
ok 7 numbers
Test #11:
score: 0
Accepted
time: 0ms
memory: 3708kb
input:
7 20 358
output:
5 7 2 4 6 3 1
result:
ok 7 numbers
Test #12:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
10 48 10001
output:
7 5 8 9 6 10 3 4 1 2
result:
ok 10 numbers
Test #13:
score: 0
Accepted
time: 1ms
memory: 3672kb
input:
10 42 10101
output:
3 9 6 8 10 5 7 2 1 4
result:
ok 10 numbers
Test #14:
score: 0
Accepted
time: 2ms
memory: 4856kb
input:
25 300 1
output:
7 14 15 16 17 18 19 20 21 22 23 24 25 1 2 3 4 5 6 8 9 10 11 12 13
result:
ok 25 numbers
Test #15:
score: 0
Accepted
time: 20ms
memory: 13704kb
input:
25 300 283788388040048639877
output:
25 24 23 22 21 20 19 18 17 16 11 12 13 14 15 10 9 8 7 5 6 4 2 1 3
result:
ok 25 numbers
Test #16:
score: 0
Accepted
time: 297ms
memory: 96352kb
input:
26 302 105773752969551707419545
output:
19 22 25 13 17 18 23 20 10 26 16 6 5 11 14 12 24 4 3 21 1 15 7 8 2 9
result:
ok 26 numbers
Test #17:
score: 0
Accepted
time: 1066ms
memory: 205108kb
input:
27 308 8781128321749037280676555
output:
16 18 17 21 25 6 20 24 22 15 27 5 7 8 2 9 26 13 1 3 14 10 23 19 4 11 12
result:
ok 27 numbers
Test #18:
score: 0
Accepted
time: 2927ms
memory: 395464kb
input:
28 304 806517199954337651602356955
output:
12 17 5 16 23 26 25 15 20 2 19 7 22 24 6 13 11 10 28 8 1 21 18 14 27 3 4 9
result:
ok 28 numbers
Test #19:
score: 0
Accepted
time: 6156ms
memory: 761412kb
input:
29 322 40281026669581503094652149519
output:
16 21 10 25 17 29 9 28 2 8 26 27 22 4 3 5 18 14 19 1 23 20 15 11 13 7 6 12 24
result:
ok 29 numbers
Test #20:
score: 0
Accepted
time: 4992ms
memory: 611308kb
input:
30 400 46479902466857426153849991132
output:
25 19 30 29 9 20 26 21 14 27 28 10 22 11 24 2 7 4 18 17 5 13 12 6 8 1 15 23 16 3
result:
ok 30 numbers
Test #21:
score: 0
Accepted
time: 10ms
memory: 18356kb
input:
30 450 1140008168482799670544355
output:
26 16 17 18 19 20 21 22 23 24 25 27 28 29 30 1 2 3 5 9 4 8 14 10 6 11 12 15 7 13
result:
ok 30 numbers
Test #22:
score: -100
Time Limit Exceeded
input:
30 150 480087379811286955791425915