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>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, M = 1e9 + 9;
int valid(int n, int a[]) {
int he = -1;
set<int> s;
for(int i = 0; i < n; i++) {
if(s.count(a[i]))
return 0;
s.insert(a[i]);
}
for(int i = 0; i < n; i++) if(a[i] <= n) {
int be = (i - a[i] + n) % n;
if(he != -1 && he != be)
return 0;
he = be;
}
return 1;
}
int replacement(int n, int a[], int b[]) {
assert(valid(n, a));
int sz = 0, mx = 0;
map<int, int> mp;
int pos = -1;
for(int i = 0; i < n; i++) {
mx = max(mx, a[i]);
if(a[i] >= n) {
mp[a[i]] = i;
} else {
pos = i;
}
}
if(pos == -1) {
for(int i = 0; i < n; i++)
a[i] = i + 1;
} else {
for(int i = pos + 1; i < n; i++) {
a[i] = (a[pos] + i - pos + n - 1) % n + 1;
}
for(int i = 0; i < pos; i++) {
a[i] = (a[pos] + i - pos + n - 1) % n + 1;
}
}
for(int i = n + 1; i <= mx; i++) {
if(mp.count(i)) {
b[sz++] = a[mp[i]];
a[mp[i]] = i;
} else {
b[sz++] = a[mp[mx]];
a[mp[mx]] = i;
}
}
return sz;
}
int qpow(int a, int b) {
if(b == 0) return 1;
ll t = qpow(a, b / 2);
t = t * t % M;
if(b & 1) t = t * a % M;
return t;
}
int countReplacement(int n, int a[]) {
if(!valid(n, a)) return 0;
int mx = 0;
vi aux;
for(int i = 0; i < n; i++) {
mx = max(mx, a[i]);
if(a[i] > n) {
aux.PB(a[i]);
}
}
aux.PB(n);
int ans = 1, cnt = 0;
sort(ALL(aux), greater<int>());
for(int i = 0; i + 1 < SZ(aux); i++) {
for(int j = aux[i] - 1; j > aux[i + 1]; j--)
ans = 1LL * ans * (i + 1) % M;
// ans = 1LL * ans * qpow(i + 1, aux[i] - aux[i + 1] - 1) % M;
}
if(SZ(aux) == n) ans = 1LL * ans * n % M;
return ans;
}
Compilation message (stderr)
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:92:18: warning: unused variable 'cnt' [-Wunused-variable]
92 | int ans = 1, cnt = 0;
| ^~~
# | 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... |