QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#443025 | #8529. Balance of Permutation | ucup-team087# | AC ✓ | 87ms | 6196kb | C++14 | 5.3kb | 2024-06-15 14:07:23 | 2024-06-15 14:07:26 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
#ifndef LIBRA_OTHER_INT128_H_
#define LIBRA_OTHER_INT128_H_
#include <stdio.h>
#include <iostream>
constexpr unsigned __int128 toUInt128(const char *s) {
unsigned __int128 x = 0;
for (; *s; ++s) x = x * 10 + (*s - '0');
return x;
}
constexpr __int128 toInt128(const char *s) {
if (*s == '-') return -toInt128(s + 1);
__int128 x = 0;
for (; *s; ++s) x = x * 10 + (*s - '0');
return x;
}
unsigned __int128 inUInt128() {
static char buf[41];
scanf("%s", buf);
return toUInt128(buf);
}
__int128 inInt128() {
static char buf[41];
scanf("%s", buf);
return toInt128(buf);
}
void out(unsigned __int128 x) {
static char buf[41];
int len = 0;
do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
for (int i = len; --i >= 0; ) putchar(buf[i]);
}
void out(__int128 x) {
if (x < 0) {
putchar('-');
out(-static_cast<unsigned __int128>(x));
} else {
out(static_cast<unsigned __int128>(x));
}
}
std::ostream &operator<<(std::ostream &os, unsigned __int128 x) {
static char buf[41];
int len = 0;
do { buf[len++] = '0' + static_cast<int>(x % 10); } while (x /= 10);
for (int i = len; --i >= 0; ) os << buf[i];
return os;
}
std::ostream &operator<<(std::ostream &os, __int128 x) {
if (x < 0) {
os << '-' << -static_cast<unsigned __int128>(x);
} else {
os << static_cast<unsigned __int128>(x);
}
return os;
}
#endif // LIBRA_OTHER_INT128_H_
using INT = __int128;
int N;
vector<int> P;
INT dp[2][40][2010];
INT solve(int B) {
if (B < 0) return false;
vector<int> xs(N, 1), ys(N, 1);
for (int i = 0; i < N; ++i) if (~P[i]) {
xs[i] = ys[P[i]] = 0;
}
for (int b = 0; b <= B; ++b) {
dp[0][0][b] = 0;
}
dp[0][0][0] = 1;
int last = 0;
int x = 0, y = 0;
for (int i = 0; i < N; ++i) if (xs[i] || ys[i]) {
const int z = min(x, y);
for (int m = 0; m <= z; ++m) for (int b = 0; b <= B; ++b) {
dp[1][m][b] = 0;
}
for (int m = 0; m <= z; ++m) for (int b = 0; b <= B; ++b) {
const int bb = b + ((x-m) + (y-m)) * (i - last);
if (bb <= B) {
dp[1][m][bb] += dp[0][m][b];
}
}
// for(int m=0;m<=z;++m)for(int b=0;b<=B;++b)if(dp[1][m][b])cerr<<i<<" "<<m<<" "<<b<<": "<<dp[1][m][b]<<endl;
for (int m = 0; m <= z + 2; ++m) for (int b = 0; b <= B; ++b) {
dp[0][m][b] = 0;
}
if (xs[i]) {
if (ys[i]) {
for (int m = 0; m <= z; ++m) for (int b = 0; b <= B; ++b) if (dp[1][m][b]) {
dp[0][m][b] += dp[1][m][b];
dp[0][m + 1][b] += dp[1][m][b] * (x-m);
dp[0][m + 1][b] += dp[1][m][b] * (y-m);
dp[0][m + 2][b] += dp[1][m][b] * (x-m) * (y-m);
dp[0][m + 1][b] += dp[1][m][b];
}
} else {
for (int m = 0; m <= z; ++m) for (int b = 0; b <= B; ++b) if (dp[1][m][b]) {
dp[0][m][b] += dp[1][m][b];
dp[0][m + 1][b] += dp[1][m][b] * (y-m);
}
}
} else {
if (ys[i]) {
for (int m = 0; m <= z; ++m) for (int b = 0; b <= B; ++b) if (dp[1][m][b]) {
dp[0][m][b] += dp[1][m][b];
dp[0][m + 1][b] += dp[1][m][b] * (x-m);
}
} else {
assert(false);
}
}
last = i;
x += xs[i];
y += ys[i];
}
assert(x == y);
// cerr<<COLOR("33")<<"[solve] "<<P<<" "<<B<<" = "<<dp[0][x][B]<<COLOR()<<endl;
return dp[0][x][B];
}
int main() {
int B;
INT K;
for (; ~scanf("%d%d", &N, &B); ) {
K = inInt128();
P.assign(N, -1);
solve(B);
int b = B;
INT k = K;
for (int i = 0; i < N; ++i) {
const set<int> used(P.begin(), P.begin() + i);
for (int a = 0; a < N; ++a) if (!used.count(a)) {
P[i] = a;
const INT res = solve(b - abs(a - i));
if (k > res) {
k -= res;
} else {
goto found;
}
}
assert(false);
found:{}
b -= abs(P[i] - i);
}
for (int i = 0; i < N; ++i) {
if (i) printf(" ");
printf("%d", P[i] + 1);
}
puts("");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3840kb
input:
6 6 6
output:
1 2 6 3 4 5
result:
ok 6 numbers
Test #2:
score: 0
Accepted
time: 27ms
memory: 5960kb
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: 0ms
memory: 3780kb
input:
1 0 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 1ms
memory: 4092kb
input:
2 0 1
output:
1 2
result:
ok 2 number(s): "1 2"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
2 2 1
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3908kb
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: 1ms
memory: 4168kb
input:
7 20 100
output:
3 6 7 4 1 5 2
result:
ok 7 numbers
Test #8:
score: 0
Accepted
time: 1ms
memory: 4116kb
input:
7 2 6
output:
2 1 3 4 5 6 7
result:
ok 7 numbers
Test #9:
score: 0
Accepted
time: 1ms
memory: 6120kb
input:
7 24 1
output:
4 5 6 7 1 2 3
result:
ok 7 numbers
Test #10:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
7 22 360
output:
7 6 4 3 5 2 1
result:
ok 7 numbers
Test #11:
score: 0
Accepted
time: 0ms
memory: 3892kb
input:
7 20 358
output:
5 7 2 4 6 3 1
result:
ok 7 numbers
Test #12:
score: 0
Accepted
time: 1ms
memory: 3964kb
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: 3896kb
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: 20ms
memory: 4308kb
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: 30ms
memory: 4508kb
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: 29ms
memory: 4248kb
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: 30ms
memory: 5952kb
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: 30ms
memory: 4332kb
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: 39ms
memory: 4624kb
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: 67ms
memory: 6196kb
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: 69ms
memory: 4456kb
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: 0
Accepted
time: 10ms
memory: 6152kb
input:
30 150 480087379811286955791425915
output:
7 4 8 5 16 3 1 12 13 11 9 10 15 25 18 17 20 30 28 2 6 14 23 21 24 26 27 22 19 29
result:
ok 30 numbers
Test #23:
score: 0
Accepted
time: 6ms
memory: 4424kb
input:
30 150 480087379811286955791439470
output:
7 4 8 5 16 3 1 12 13 11 9 10 15 25 18 17 20 30 28 2 19 6 22 24 21 23 26 14 29 27
result:
ok 30 numbers
Test #24:
score: 0
Accepted
time: 73ms
memory: 5984kb
input:
30 440 41509275104334759322587324
output:
22 23 20 24 18 30 19 26 21 28 4 29 17 25 27 16 3 1 2 5 8 13 10 15 7 12 9 14 11 6
result:
ok 30 numbers
Test #25:
score: 0
Accepted
time: 70ms
memory: 4736kb
input:
30 450 1140008168482800727111311
output:
26 16 17 18 19 20 21 22 23 24 25 27 28 29 30 1 2 5 7 14 4 15 8 11 3 13 10 9 6 12
result:
ok 30 numbers
Test #26:
score: 0
Accepted
time: 63ms
memory: 4464kb
input:
30 400 52289890275214604423031772929
output:
26 27 29 21 28 16 18 11 2 25 24 23 6 30 20 13 17 10 15 4 9 12 8 22 19 1 5 7 3 14
result:
ok 30 numbers
Test #27:
score: 0
Accepted
time: 1ms
memory: 4020kb
input:
30 0 1
output:
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
result:
ok 30 numbers
Test #28:
score: 0
Accepted
time: 64ms
memory: 4452kb
input:
30 450 1
output:
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
result:
ok 30 numbers
Test #29:
score: 0
Accepted
time: 87ms
memory: 4464kb
input:
30 450 1710012252724199424000000
output:
30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
result:
ok 30 numbers
Test #30:
score: 0
Accepted
time: 85ms
memory: 5908kb
input:
30 450 1692383260428073656742269
output:
30 27 26 28 18 29 21 19 25 17 20 16 24 22 23 7 13 4 6 3 5 12 1 15 14 9 11 8 2 10
result:
ok 30 numbers
Test #31:
score: 0
Accepted
time: 48ms
memory: 5920kb
input:
30 302 5918364042599361729860937331200
output:
30 29 28 27 26 25 14 8 9 10 11 12 13 7 15 16 17 18 19 20 21 22 23 24 6 5 4 3 2 1
result:
ok 30 numbers
Test #32:
score: 0
Accepted
time: 22ms
memory: 4320kb
input:
30 254 2256781660157136563723839089600
output:
25 2 3 12 7 16 19 8 22 6 11 17 27 26 10 24 15 21 20 18 28 9 30 23 14 13 5 29 4 1
result:
ok 30 numbers
Test #33:
score: 0
Accepted
time: 76ms
memory: 4708kb
input:
30 448 3131906441000512625049600
output:
23 20 28 18 26 30 19 29 27 22 17 24 21 25 2 13 16 15 14 12 11 10 9 8 7 6 5 4 3 1
result:
ok 30 numbers
Test #34:
score: 0
Accepted
time: 1ms
memory: 4320kb
input:
30 2 20
output:
1 2 3 4 5 6 7 8 9 11 10 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
result:
ok 30 numbers
Test #35:
score: 0
Accepted
time: 1ms
memory: 4012kb
input:
30 2 29
output:
2 1 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
result:
ok 30 numbers
Extra Test:
score: 0
Extra Test Passed