#include "gondola.h"
#include <numeric>
#include <algorithm>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
using llong = long long;
#define BUGO(x) cerr << #x << " = " << x << '\n';
int valid(int n, int a[])
{
if (set<int>(a, a + n).size() != n) return false;
set<int> ones;
for (int i = 0; i < n; ++i)
if (a[i] <= n) {
int j = i - (a[i] - 1);
if (j < 0) j += n;
ones.insert(j);
}
return ones.size() <= 1;
}
//----------------------
int replacement(int n, int a[], int ans[])
{
int anslen = 0;
vector<int> must(n, -1);
for (int i = 0; i < n; ++i)
if (a[i] <= n) {
for (int j = i; j < i + n; ++j) {
must[j % n] = (a[i] + j - i) % n;
if (!must[j % n]) must[j % n] = n;
}
break;
}
if (must[0] == -1)
iota(must.begin(), must.end(), 1);
vector<int> inds; inds.reserve(n);
for (int i = 0; i < n; ++i)
if (a[i] > n)
inds.push_back(i);
auto comp = [&a](int l, int r) {
return a[l] < a[r];
};
sort(inds.begin(), inds.end(), comp);
int gondol = n + 1;
for (int i: inds)
while (must[i] < a[i]) {
ans[anslen++] = must[i];
must[i] = gondol++;
}
return anslen;
}
//----------------------
const llong mod = 1000'000'009LL;
llong binpow(llong a, llong p) {
if (p == 0) return 1;
if (p % 2) return (a * binpow(a, p - 1)) % mod;
return binpow((a * a) % mod, p / 2);
}
int countReplacement(int n, int a[])
{
if (!valid(n, a)) return 0;
vector<int> b; b.reserve(n);
for (int i = 0; i < n; ++i)
if (a[i] > n)
b.push_back(a[i]);
b.push_back(n);
sort(b.begin(), b.end());
llong ans = 1;
for (int i = 0; i + 1 < b.size(); ++i)
{
ans *= binpow((llong)b.size() - 1LL - i, b[i + 1] - b[i] - 1LL);
ans %= mod;
}
if (*min_element(a, a + n) > n)
ans = (ans * n) % mod;
return ans;
}
Compilation message
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:13:35: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
13 | if (set<int>(a, a + n).size() != n) return false;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:74:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i + 1 < b.size(); ++i)
| ~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
2424 KB |
Output is correct |
7 |
Correct |
33 ms |
4216 KB |
Output is correct |
8 |
Correct |
25 ms |
4352 KB |
Output is correct |
9 |
Correct |
6 ms |
1664 KB |
Output is correct |
10 |
Correct |
34 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
2432 KB |
Output is correct |
7 |
Correct |
33 ms |
4216 KB |
Output is correct |
8 |
Correct |
24 ms |
4352 KB |
Output is correct |
9 |
Correct |
6 ms |
1664 KB |
Output is correct |
10 |
Correct |
24 ms |
5008 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
17 ms |
2304 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
60 ms |
5352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
13 ms |
1408 KB |
Output is correct |
12 |
Correct |
15 ms |
1532 KB |
Output is correct |
13 |
Correct |
19 ms |
1400 KB |
Output is correct |
14 |
Correct |
12 ms |
1408 KB |
Output is correct |
15 |
Correct |
25 ms |
2420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
44 ms |
4472 KB |
Output is correct |
10 |
Correct |
34 ms |
3848 KB |
Output is correct |
11 |
Correct |
12 ms |
1528 KB |
Output is correct |
12 |
Correct |
16 ms |
1920 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
45 ms |
4472 KB |
Output is correct |
10 |
Correct |
37 ms |
3840 KB |
Output is correct |
11 |
Correct |
12 ms |
1536 KB |
Output is correct |
12 |
Correct |
15 ms |
1920 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
14 |
Correct |
55 ms |
5368 KB |
Output is correct |
15 |
Correct |
64 ms |
6008 KB |
Output is correct |
16 |
Correct |
10 ms |
1376 KB |
Output is correct |
17 |
Correct |
44 ms |
4172 KB |
Output is correct |
18 |
Correct |
21 ms |
2432 KB |
Output is correct |