#include "gondola.h"
#include <stdio.h>
#include <string.h>
#define N 100000
#define A 250000
#define MD 1000000009
int max(int a, int b) { return a > b ? a : b; }
int valid(int n, int aa[]) {
static char used[A + 1];
int i, j;
for (i = 0; i < n; i++) {
if (used[aa[i]])
return 0;
used[aa[i]] = 1;
}
for (i = 0; i < n; i++)
if (aa[i] <= n) {
for (j = 0; j < n; j++)
if (aa[(i + j) % n] <= n && aa[(i + j) % n] != (aa[i] - 1 + j) % n + 1)
return 0;
return 1;
}
return 1;
}
int replacement(int n, int aa[], int bb[]) {
static int ii[A + 1];
int i, a, a_, b, offset;
memset(ii, -1, (A + 1) * sizeof *ii);
offset = 0;
for (i = 0, a_ = -1; i < n; i++) {
ii[aa[i]] = i, a_ = max(a_, aa[i]);
if (aa[i] <= n)
offset = (aa[i] - 1 - i + n) % n;
}
for (a = n + 1, b = (ii[a_] + offset) % n + 1; a <= a_; a++)
if (a < a_ && ii[a] != -1)
bb[a - n - 1] = (ii[a] + offset) % n + 1;
else
bb[a - n - 1] = b, b = a;
return a_ - n;
}
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int *aa_;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa_[ii[j]] == aa_[i_])
j++;
else if (aa_[ii[j]] < aa_[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
long long power(long long a, int k) {
long long p = 1;
while (k) {
if (k & 1)
p = p * a % MD;
a = a * a % MD;
k >>= 1;
}
return p;
}
int countReplacement(int n, int aa[]) {
static int ii[N];
int i, offset_, ans;
offset_ = -1;
for (i = 0; i < n; i++)
if (aa[i] <= n) {
int offset = (aa[i] - 1 - i + n) % n;
if (offset_ != -1 && offset_ != offset)
return 0;
offset_ = offset;
}
for (i = 0; i < n; i++)
ii[i] = i;
aa_ = aa, sort(ii, 0, n);
for (i = 1; i < n; i++)
if (aa[ii[i]] == aa[ii[i - 1]])
return 0;
ans = 1;
for (i = n - 1; i >= 0; i--) {
if (aa[ii[i]] <= n)
break;
ans = ans * power(n - i, aa[ii[i]] - (i == 0 ? n : max(aa[ii[i - 1]], n)) - 1) % MD;
}
if (offset_ == -1)
ans = (long long) ans * n % MD;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
5 ms |
460 KB |
Output is correct |
7 |
Correct |
10 ms |
716 KB |
Output is correct |
8 |
Correct |
9 ms |
588 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
11 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
5 ms |
460 KB |
Output is correct |
7 |
Correct |
11 ms |
716 KB |
Output is correct |
8 |
Correct |
9 ms |
592 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
11 ms |
700 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
5 ms |
588 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
11 ms |
720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1232 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1232 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
10 ms |
1580 KB |
Output is correct |
12 |
Correct |
12 ms |
1576 KB |
Output is correct |
13 |
Correct |
14 ms |
1996 KB |
Output is correct |
14 |
Correct |
10 ms |
1484 KB |
Output is correct |
15 |
Correct |
23 ms |
3060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
208 KB |
Output is correct |
6 |
Correct |
0 ms |
216 KB |
Output is correct |
7 |
Correct |
0 ms |
216 KB |
Output is correct |
8 |
Correct |
0 ms |
216 KB |
Output is correct |
9 |
Correct |
25 ms |
844 KB |
Output is correct |
10 |
Correct |
18 ms |
764 KB |
Output is correct |
11 |
Correct |
7 ms |
460 KB |
Output is correct |
12 |
Correct |
8 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
22 ms |
856 KB |
Output is correct |
10 |
Correct |
18 ms |
644 KB |
Output is correct |
11 |
Correct |
7 ms |
460 KB |
Output is correct |
12 |
Correct |
8 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
27 ms |
916 KB |
Output is correct |
15 |
Correct |
31 ms |
972 KB |
Output is correct |
16 |
Correct |
6 ms |
332 KB |
Output is correct |
17 |
Correct |
21 ms |
716 KB |
Output is correct |
18 |
Correct |
11 ms |
460 KB |
Output is correct |