이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |