QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#375675#8529. Balance of Permutationucup-team1209#TL 7347ms644884kbC++202.4kb2024-04-03 14:47:042024-04-03 14:47:05

Judging History

你现在查看的是最新测评结果

  • [2024-04-03 14:47:05]
  • 评测
  • 测评结果:TL
  • 用时:7347ms
  • 内存:644884kb
  • [2024-04-03 14:47:04]
  • 提交

answer

#include<bits/stdc++.h>
using std::cin;
using std::cout;
using ll = long long;
using i128 = __int128;
const int N = 35;
int n, b;
i128 k;

i128 dp[N * 2][N][N][N * N / 2];
i128 calc(std::vector<std::pair<int, int>> v, int b) {
	if(b < 0) return 0;
	int n = v.size();
	int c0 = 0;
	int c1 = 0;
	dp[0][0][0][0] = 1;
	for(int i = 0;i < n;++i) {
		auto [c, col] = v[i];
		int s = 0;
		if(i) s = v[i].first - v[i - 1].first;
		assert(s >= 0);
		for(int x = 0;x <= c0;++x) {
			for(int y = 0;y <= c1;++y) {
				const int add = s * (x + y);
				if(col) {
					for(int z = 0;z + add <= b;++z) {
						dp[i + 1][x][y + 1][z + s * (x + y)] += dp[i][x][y][z];
					}
					if(x > 0) {
						for(int z = 0;z + add <= b;++z) {
							dp[i + 1][x - 1][y][z + s * (x + y)] += dp[i][x][y][z] * x;
						}
					}
				} else {
					for(int z = 0;z + add <= b;++z) {
						dp[i + 1][x + 1][y][z + s * (x + y)] += dp[i][x][y][z];
					}
					if(y > 0) {
						for(int z = 0;z + add <= b;++z) {
							dp[i + 1][x][y - 1][z + s * (x + y)] += dp[i][x][y][z] * y;
						}
					}
				}
			}
		}
		++ (col ? c1 : c0);
	}
	i128 res = dp[n][0][0][b];
	for(int i = 0;i <= n;++i) {
		for(int x = 0;x <= c0;++x) {
			for(int y = 0;y <= c1;++y) {
				memset(dp[i][x][y], 0, (b + 1) * sizeof(i128));
			}
		}
	}
	return res;
}
int ans[N];

void getk(int i, int mask, int sum, i128 k) {
	if(i == n) {
		for(int x = 0;x < n;++x) {
			cout << ans[x] + 1 << " \n"[i == n - 1];
		}
		return ;
	}
	for(int x = 0;x < n;++x) {
		if(mask >> x & 1) {
			ans[i] = x;
			int _sum = sum + std::abs(x - i);
			std::vector<std::pair<int, int>> v;
			for(int z = i + 1;z < n;++z) {
				v.emplace_back(z, 0);
			}
			for(int z = 0;z < n;++z) if((mask >> z & 1) && z != x) {
				v.emplace_back(z, 1);
			}
			sort(v.begin(), v.end());

			i128 count = calc(v, b - _sum);
			if(k > count) {
				k -= count;
			} else {
				ans[i] = x;
				getk(i + 1, mask ^ (1 << x), _sum, k);
				return ;
			}
		}
	}
}

int main() {
	std::ios::sync_with_stdio(false), cin.tie(0);
//	printf("%d\n", sizeof(dp) >> 20);
	std::string s;
	cin >> n >> b >> s;
	for(char c : s) k = k * 10 + (c & 15);
	/*
	std::vector<std::pair<int, int>> v;
	for(int i = 0;i < n;++i) {
		v.emplace_back(i, 0);
		v.emplace_back(i, 1);
	}
	cout << (ll) calc(v, b) << '\n';
	*/
	getk(0, (1 << n) - 1, 0, k);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 42640kb

input:

6 6 6

output:

1 2 6 3 4 5 

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 2165ms
memory: 579604kb

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: 3628kb

input:

1 0 1

output:

1 

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 1ms
memory: 7640kb

input:

2 0 1

output:

1 2 

result:

ok 2 number(s): "1 2"

Test #5:

score: 0
Accepted
time: 1ms
memory: 7904kb

input:

2 2 1

output:

2 1 

result:

ok 2 number(s): "2 1"

Test #6:

score: 0
Accepted
time: 0ms
memory: 32492kb

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: 55192kb

input:

7 20 100

output:

3 6 7 4 1 5 2 

result:

ok 7 numbers

Test #8:

score: 0
Accepted
time: 0ms
memory: 50796kb

input:

7 2 6

output:

2 1 3 4 5 6 7 

result:

ok 7 numbers

Test #9:

score: 0
Accepted
time: 4ms
memory: 54932kb

input:

7 24 1

output:

4 5 6 7 1 2 3 

result:

ok 7 numbers

Test #10:

score: 0
Accepted
time: 0ms
memory: 52880kb

input:

7 22 360

output:

7 6 4 3 5 2 1 

result:

ok 7 numbers

Test #11:

score: 0
Accepted
time: 0ms
memory: 55148kb

input:

7 20 358

output:

5 7 2 4 6 3 1 

result:

ok 7 numbers

Test #12:

score: 0
Accepted
time: 7ms
memory: 99920kb

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: 0ms
memory: 96096kb

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: 1493ms
memory: 426196kb

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: 2445ms
memory: 435956kb

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: 2451ms
memory: 465700kb

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: 2680ms
memory: 489244kb

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: 2754ms
memory: 522944kb

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: 3786ms
memory: 563644kb

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: 6729ms
memory: 613788kb

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: 6731ms
memory: 612764kb

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: 849ms
memory: 535268kb

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: 894ms
memory: 535228kb

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: 7347ms
memory: 626472kb

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: 6791ms
memory: 644884kb

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: 6930ms
memory: 638260kb

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: 4ms
memory: 562532kb

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: 6224ms
memory: 639272kb

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: -100
Time Limit Exceeded

input:

30 450 1710012252724199424000000

output:


result: