This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "gondola.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 9;
bool ch[250500];
int p[100100];
int valid(int n, int a[]) {
int mn = n + 1;
int idx = 0;
for (int i = 0;i < n;i++) {
if (ch[a[i]]) return 0;
ch[a[i]] = 1;
if (a[i] <= n) {
if (a[i] < mn) {
mn = a[i];
idx = i;
}
}
}
if (mn == n + 1) return 1;
idx = (idx - mn + 1 + n) % n;
for (int i = 0;i < n;i++) {
if (a[(i + idx) % n] <= n && a[(i + idx) % n] != i + 1) return 0;
}
return 1;
}
int replacement(int n, int a[], int replacementSeq[]) {
int mn = n + 1;
int idx = 0;
vector<pair<int, int>> v;
for (int i = 0;i < n;i++) {
if (a[i] <= n) {
if (a[i] < mn) {
mn = a[i];
idx = i;
}
}
else v.push_back({ a[i], i });
}
idx = (idx - mn + 1 + n) % n;
for (int i = 0;i < n;i++) {
p[(i + idx) % n] = i + 1;
}
int sz = 0, now = n;
sort(v.begin(), v.end());
for (auto& x : v) {
while (p[x.second] != x.first) {
replacementSeq[sz++] = p[x.second];
p[x.second] = ++now;
}
}
return sz;
}
ll mop(ll a, ll p) {
ll ans = 1;
for (;p != 0ll;p >>= 1ll, a = a * a % MOD) {
if (p & 1ll) ans = ans * a % MOD;
}
return ans;
}
int countReplacement(int n, int a[]) {
if (!valid(n, a)) return 0;
int cnt = 0;
int mx = 0;
vector<int> v;
for (int i = 0;i < n;i++) {
if (a[i] > n) v.push_back(a[i]), cnt++;
mx = max(mx, a[i]);
}
if (mx <= n) return 1;
sort(v.begin(), v.end());
int m = v.size();
bool c = (cnt == n);
ll ans = 1;
int prev = n;
for (auto& x : v) {
ans = ans * mop(cnt, x - prev - 1) % MOD;
cnt--;
prev = x;
}
if (!c) return ans;
return ans * n % MOD;
}
Compilation message (stderr)
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:74:9: warning: unused variable 'm' [-Wunused-variable]
74 | int m = v.size();
| ^
# | 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... |