QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#851361 | #8228. 排序 | hhoppitree | 100 ✓ | 3541ms | 249884kb | C++23 | 2.0kb | 2025-01-10 18:10:22 | 2025-01-10 18:10:23 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
int n, m, val[15], ct[15], vis[1048576];
pair<int, int> f[1048576];
int pw[15][15];
vector<int> M[15][35];
int solve(int z, int x, int y, int s = 0) {
if (z > m) return 0;
if (z != 1 && ~M[z][x][s]) return M[z][x][s];
int S = 0;
for (int j = 0; j < val[z]; ++j) ct[j] = 0;
for (int j = n - 1; ~j; --j) {
if ((y >> j) & 1) {
if (j == x) S = ct[j % val[z]];
++ct[j % val[z]];
}
}
int tx = -1, ty = 0, ts = 0;
for (int j = 0; j < n; ++j) {
if (ct[j % val[z]]) {
ty |= (1 << j), ts += pw[z][j % val[z]], --ct[j % val[z]];
if (!~tx && x % val[z] == j % val[z] && !ct[j % val[z]]) tx = j;
}
}
if (z != 1) M[z][x][s] = S + solve(z + 1, tx, ty, ts);
return S + solve(z + 1, tx, ty, ts);
}
pair<int, int> calc(int x, int s) {
if (x == ((1 << n) - 1)) return {0, 1};
if (vis[s]++) return f[s];
int mx = 0, sum = 0, usd = 0;
for (int i = n - 1; ~i; --i) {
if (((x >> i) & 1) || (usd >> (i % val[1])) & 1) continue;
usd |= (1 << (i % val[1]));
pair<int, int> ans = calc(x | (1 << i), s + pw[1][i % val[1]]);
ans.first += solve(1, i, x | (1 << i));
if (ans.first > mx) tie(mx, sum) = ans;
else if (ans.first == mx) ((sum += ans.second) >= P) && (sum -= P);
}
return f[s] = {mx, sum};
}
signed main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) scanf("%d", &val[i]);
for (int j = 1; j <= m; ++j) {
for (int i = 0; i < val[j]; ++i) pw[j][i] = 1;
for (int i = 0; i < n; ++i) ++pw[j][i % val[j]];
for (int i = 1; i < val[j]; ++i) pw[j][i] = pw[j][i - 1] * pw[j][i];
for (int i = 0; i < n; ++i) M[j + 1][i].resize(pw[j][val[j] - 1], -1);
for (int i = val[j] - 1; ~i; --i) pw[j][i] = (!i ? 1 : pw[j][i - 1]);
}
printf("%d %d\n", calc(0, 0).first, calc(0, 0).second);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 18
Accepted
Test #1:
score: 18
Accepted
time: 0ms
memory: 3924kb
input:
2 2 2 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #2:
score: 18
Accepted
time: 0ms
memory: 3944kb
input:
5 4 5 4 2 1
output:
6 4
result:
ok 2 number(s): "6 4"
Test #3:
score: 18
Accepted
time: 1ms
memory: 3948kb
input:
8 4 6 3 2 1
output:
15 4
result:
ok 2 number(s): "15 4"
Test #4:
score: 18
Accepted
time: 1ms
memory: 3964kb
input:
8 6 8 7 5 4 2 1
output:
14 2
result:
ok 2 number(s): "14 2"
Test #5:
score: 18
Accepted
time: 0ms
memory: 3860kb
input:
8 3 8 7 1
output:
22 7
result:
ok 2 number(s): "22 7"
Test #6:
score: 18
Accepted
time: 0ms
memory: 3980kb
input:
8 1 1
output:
28 1
result:
ok 2 number(s): "28 1"
Subtask #2:
score: 27
Accepted
Dependency #1:
100%
Accepted
Test #7:
score: 27
Accepted
time: 3ms
memory: 4012kb
input:
16 2 6 1
output:
77 15
result:
ok 2 number(s): "77 15"
Test #8:
score: 27
Accepted
time: 27ms
memory: 6188kb
input:
16 8 10 9 8 7 6 5 4 1
output:
57 5
result:
ok 2 number(s): "57 5"
Test #9:
score: 27
Accepted
time: 28ms
memory: 6144kb
input:
16 10 10 9 8 7 6 5 4 3 2 1
output:
57 3
result:
ok 2 number(s): "57 3"
Test #10:
score: 27
Accepted
time: 27ms
memory: 5952kb
input:
16 7 10 9 8 6 5 4 1
output:
49 1
result:
ok 2 number(s): "49 1"
Test #11:
score: 27
Accepted
time: 7ms
memory: 4380kb
input:
16 4 7 6 2 1
output:
52 9
result:
ok 2 number(s): "52 9"
Subtask #3:
score: 21
Accepted
Dependency #2:
100%
Accepted
Test #12:
score: 21
Accepted
time: 3ms
memory: 4296kb
input:
22 3 5 3 1
output:
100 1
result:
ok 2 number(s): "100 1"
Test #13:
score: 21
Accepted
time: 0ms
memory: 3848kb
input:
22 1 1
output:
231 1
result:
ok 2 number(s): "231 1"
Test #14:
score: 21
Accepted
time: 280ms
memory: 16876kb
input:
22 4 10 8 3 1
output:
97 4
result:
ok 2 number(s): "97 4"
Test #15:
score: 21
Accepted
time: 276ms
memory: 18408kb
input:
22 5 10 7 6 3 1
output:
92 70
result:
ok 2 number(s): "92 70"
Test #16:
score: 21
Accepted
time: 285ms
memory: 25352kb
input:
22 6 10 9 8 7 3 1
output:
97 1
result:
ok 2 number(s): "97 1"
Test #17:
score: 21
Accepted
time: 291ms
memory: 27148kb
input:
22 10 10 9 8 7 6 5 4 3 2 1
output:
109 1
result:
ok 2 number(s): "109 1"
Subtask #4:
score: 10
Accepted
Test #18:
score: 10
Accepted
time: 8ms
memory: 6292kb
input:
14 2 10 1
output:
61 210
result:
ok 2 number(s): "61 210"
Test #19:
score: 10
Accepted
time: 0ms
memory: 3868kb
input:
18 2 2 1
output:
117 1
result:
ok 2 number(s): "117 1"
Test #20:
score: 10
Accepted
time: 1340ms
memory: 71288kb
input:
30 2 9 1
output:
264 84
result:
ok 2 number(s): "264 84"
Test #21:
score: 10
Accepted
time: 1047ms
memory: 55940kb
input:
29 2 9 1
output:
253 36
result:
ok 2 number(s): "253 36"
Test #22:
score: 10
Accepted
time: 168ms
memory: 14236kb
input:
25 2 8 1
output:
195 8
result:
ok 2 number(s): "195 8"
Subtask #5:
score: 24
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #23:
score: 24
Accepted
time: 3519ms
memory: 200716kb
input:
30 4 10 9 5 1
output:
188 40
result:
ok 2 number(s): "188 40"
Test #24:
score: 24
Accepted
time: 3541ms
memory: 249884kb
input:
30 9 10 9 8 7 6 5 4 3 1
output:
184 6
result:
ok 2 number(s): "184 6"
Test #25:
score: 24
Accepted
time: 3520ms
memory: 241908kb
input:
30 8 10 9 8 7 4 3 2 1
output:
154 1
result:
ok 2 number(s): "154 1"
Test #26:
score: 24
Accepted
time: 3470ms
memory: 189488kb
input:
30 8 10 8 7 6 5 4 3 1
output:
155 1
result:
ok 2 number(s): "155 1"
Test #27:
score: 24
Accepted
time: 3471ms
memory: 174324kb
input:
30 6 10 8 6 4 3 1
output:
150 4
result:
ok 2 number(s): "150 4"
Test #28:
score: 24
Accepted
time: 3524ms
memory: 249600kb
input:
30 10 10 9 8 7 6 5 4 3 2 1
output:
184 6
result:
ok 2 number(s): "184 6"
Test #29:
score: 24
Accepted
time: 2555ms
memory: 162340kb
input:
29 6 10 9 7 5 3 1
output:
129 200
result:
ok 2 number(s): "129 200"
Extra Test:
score: 0
Extra Test Passed