/**
* Quite simple, though a little bit tedious? Just note that the gondola seq can have
* extremely large values in [S10].
*
* Time Complexity: O(n * log(n)) (Full solution)
* Implementation 1 (Math, implementation)
*/
#include <bits/stdc++.h>
#include "gondola.h"
typedef long long ll;
typedef std::vector<int> vec;
const ll MOD = 1e9 + 9;
ll pow(ll a, int b) {
ll res = 1;
while (b > 0) {
if (b % 2 == 1)
res = res * a % MOD;
a = a * a % MOD, b /= 2;
}
return res;
}
vec cyc_shift(int n, int input_seq[]) {
int original = -1, original_pos = -1;
for (int k = 0; k < n && original == -1; k++) {
if (input_seq[k] <= n)
original = input_seq[k], original_pos = k;
}
vec shift(n);
int delta = (original != -1 ? original_pos + 1 - original : 0);
for (int k = 0; k < n; k++)
shift[k] = input_seq[(k + delta + n) % n];
return shift;
}
int valid(int n, int input_seq[]) {
vec shift = cyc_shift(n, input_seq);
bool ok = true;
std::set<int> appeared;
for (int k = 0; k < n && ok; k++) {
if (shift[k] <= n) {
ok &= (shift[k] == k + 1);
} else {
ok &= (appeared.find(shift[k]) == appeared.end());
appeared.emplace(shift[k]);
}
}
return ok;
}
int replacement(int n, int gondola_seq[], int replacement_seq[]) {
assert(valid(n, gondola_seq));
vec shift = cyc_shift(n, gondola_seq);
int repl_len = 0;
vec count(n + 1, 0);
memset(replacement_seq, -1, sizeof(int) * 250001);
for (int k = 0; k < n; k++) {
if (shift[k] > n) {
repl_len = std::max(repl_len, shift[k] - n);
replacement_seq[shift[k] - n - 1] = k + 1;
}
}
for (int k = repl_len - 1, last = -1; k >= 0; k--) {
if (replacement_seq[k] == -1) {
replacement_seq[k] = replacement_seq[last];
replacement_seq[last] = n + k + 1;
}
last = k;
}
return repl_len;
}
int countReplacement(int n, int input_seq[]) {
if (!valid(n, input_seq))
return 0;
vec shift = cyc_shift(n, input_seq);
vec top;
int max_val = 0;
for (int k = 0; k < n; k++) {
if (shift[k] > n)
top.push_back(shift[k]);
max_val = std::max(max_val, shift[k]);
}
std::sort(top.begin(), top.end());
ll comb = 1;
for (int i = 0, m = top.size(), prev = n + 1; i < m; i++)
comb *= pow(m - i, top[i] - prev), comb %= MOD, prev = top[i] + 1;
bool fixed_cyc = false;
for (int k = 0; k < n && !fixed_cyc; k++)
fixed_cyc |= (shift[k] == k + 1);
if (!fixed_cyc)
comb *= ll(n), comb %= MOD;
return int(comb);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
4 ms |
808 KB |
Output is correct |
7 |
Correct |
8 ms |
1392 KB |
Output is correct |
8 |
Correct |
6 ms |
1216 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
8 ms |
1412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
5 ms |
724 KB |
Output is correct |
7 |
Correct |
8 ms |
1492 KB |
Output is correct |
8 |
Correct |
7 ms |
1264 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
8 ms |
1344 KB |
Output is correct |
11 |
Correct |
0 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
4 ms |
740 KB |
Output is correct |
14 |
Correct |
0 ms |
312 KB |
Output is correct |
15 |
Correct |
10 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1204 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1208 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1208 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1204 KB |
Output is correct |
7 |
Correct |
1 ms |
1208 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1204 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
11 ms |
2612 KB |
Output is correct |
12 |
Correct |
9 ms |
2756 KB |
Output is correct |
13 |
Correct |
20 ms |
3424 KB |
Output is correct |
14 |
Correct |
9 ms |
2544 KB |
Output is correct |
15 |
Correct |
27 ms |
3320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
31 ms |
4028 KB |
Output is correct |
10 |
Correct |
23 ms |
3052 KB |
Output is correct |
11 |
Correct |
11 ms |
1676 KB |
Output is correct |
12 |
Correct |
12 ms |
1832 KB |
Output is correct |
13 |
Correct |
3 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
32 ms |
4028 KB |
Output is correct |
10 |
Correct |
23 ms |
3108 KB |
Output is correct |
11 |
Correct |
10 ms |
1620 KB |
Output is correct |
12 |
Correct |
12 ms |
1836 KB |
Output is correct |
13 |
Correct |
3 ms |
700 KB |
Output is correct |
14 |
Correct |
46 ms |
5760 KB |
Output is correct |
15 |
Correct |
51 ms |
6352 KB |
Output is correct |
16 |
Correct |
9 ms |
1336 KB |
Output is correct |
17 |
Correct |
38 ms |
4352 KB |
Output is correct |
18 |
Correct |
17 ms |
2548 KB |
Output is correct |