#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "/home/ioi/codes/ioi14_d2/debug.h"
#else
#define debug(...) void(37)
#endif
template<typename T>
vector<T> inverse_fuck(T* a, int N) {
vector<T> res(N);
for (int i = 0; i < N; ++i) {
res[i] = a[i];
}
return res;
}
const int md = int(1e9) + 9;
struct Mint {
int val = 0;
Mint() : val(0) { }
template<typename T>
Mint(T x) {
if (-md <= x && x < md) {
val = x;
} else {
val = x % md;
}
if (val < 0) {
val += md;
}
}
int& operator()() { return val; }
Mint& operator+=(Mint x) {
if ((val += x.val) > md) {
val -= md;
}
return *this;
}
Mint& operator-=(Mint x) {
if ((val -= x.val) < 0) {
val += md;
}
return *this;
}
Mint& operator*=(Mint x) {
val = int(1LL * val * x.val % md);
return *this;
}
};
Mint operator+(Mint x, Mint y) {
return x += y;
}
Mint operator-(Mint x, Mint y) {
return x -= y;
}
Mint operator*(Mint x, Mint y) {
return x *= y;
}
string to_string(Mint x) {
return to_string(x.val);
}
template<typename T>
Mint power(Mint x, T p) {
Mint res = 1;
while (p > 0) {
if (p & 1) {
res *= x;
}
x *= x;
p >>= 1;
}
return res;
}
int valid(int N, int inputSeq[])
{
auto A = inverse_fuck(inputSeq, N);
int prev = -1;
for (int i = 0; i < N; ++i) {
if (A[i] <= N) {
if (prev != -1) {
if (i - prev != (A[i] - A[prev] + N) % N) {
return false;
}
}
prev = i;
}
}
return set<int>(A.begin(), A.end()).size() == A.size();
}
//----------------------
int replacement(int N, int inputSeq[], int replacementSeq[])
{
auto A = inverse_fuck(inputSeq, N);
int mn = min_element(A.begin(), A.end()) - A.begin();
//vector<int> cur(N);
if (A[mn] <= N) {
vector<int> sa(N);
for (int i = 0; i < N; ++i) {
sa[(A[mn] - 1 + i) % N] = A[(mn + i) % N];
//cur[(mn + i) % N] = i + 1;
}
swap(A, sa);
}
int mx = max_element(A.begin(), A.end()) - A.begin();
vector<int> pos(A[mx] + 1, -1);
for (int i = 0; i < N; ++i) {
pos[A[i]] = i;
}
vector<int> ans;
for (int i = N + 1; i <= A[mx]; ++i) {
int use = (pos[i] == -1 ? mx : pos[i]);
ans.push_back(use);
//cur[use] = i;
}
for (int i = 0; i < int(ans.size()); ++i) {
replacementSeq[i] = ans[i] + 1;
}
return int(ans.size());
}
//----------------------
int countReplacement(int N, int inputSeq[])
{
auto A = inverse_fuck(inputSeq, N);
if (!valid(N, inputSeq)) {
return 0;
}
sort(A.begin(), A.end());
Mint res = 1;
int prev = N;
for (int i = 0; i < N; ++i) {
if (A[i] <= N) {
continue;
}
res *= power(N - i, A[i] - prev - 1);
prev = A[i];
}
return res();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
280 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
6 ms |
2516 KB |
Output is correct |
7 |
Correct |
7 ms |
1388 KB |
Output is correct |
8 |
Correct |
14 ms |
4540 KB |
Output is correct |
9 |
Correct |
6 ms |
1620 KB |
Output is correct |
10 |
Correct |
13 ms |
5292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 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 |
212 KB |
Output is correct |
6 |
Correct |
6 ms |
2516 KB |
Output is correct |
7 |
Correct |
7 ms |
1492 KB |
Output is correct |
8 |
Correct |
14 ms |
4532 KB |
Output is correct |
9 |
Correct |
4 ms |
1620 KB |
Output is correct |
10 |
Correct |
13 ms |
5256 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
7 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
308 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 |
212 KB |
Output is correct |
6 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
304 KB |
Output is correct |
6 |
Incorrect |
1 ms |
308 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
304 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 |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
29 ms |
5008 KB |
Output is correct |
10 |
Correct |
22 ms |
4180 KB |
Output is correct |
11 |
Correct |
7 ms |
1676 KB |
Output is correct |
12 |
Correct |
10 ms |
2084 KB |
Output is correct |
13 |
Incorrect |
2 ms |
708 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
308 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
31 ms |
5008 KB |
Output is correct |
10 |
Correct |
21 ms |
4228 KB |
Output is correct |
11 |
Correct |
8 ms |
1748 KB |
Output is correct |
12 |
Correct |
10 ms |
1984 KB |
Output is correct |
13 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |