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 pb push_back
using namespace std;
int valid(int n, int a[]){
vector<int> A, S;
for(int i = 0; i < n; i++) A.pb(a[i]);
sort(A.begin(), A.end());
for(int i = 0; i + 1 < n; i++) if(A[i] == A[i + 1]) return 0;
for(int i = 0; i < n; i++) if(a[i] <= n) S.pb((i - a[i] + n) % n);
sort(S.begin(), S.end());
for(int i = 0; i + 1 < (int) S.size(); i++) if(S[i] != S[i + 1]) return 0;
return 1;
}
//----------------------
const int MX = 250010;
int replacement(int n, int a[], int ans[]){
vector< vector<int> > V(n);
int L = (*max_element(a, a + n)) - n;
vector<int> mk(MX, 0);
int idx = 0;
for(int i = 0; i < n; i++){
if(a[i] < n) idx = (i - a[i] + 1 + n) % n;
mk[a[i]] = 1;
}
int mx = max_element(a, a + n) - a;
for(int i = 1; i <= n; i++) V[(idx + i - 1) % n].pb(i);
for(int i = n + 1; i <= n + L; i++){
if(!mk[i])
V[mx].pb(i);
}
for(int i = 0; i < n; i++){
if(a[i] > n) V[i].pb(a[i]);
}
for(int i = 0; i < n; i++)
for(int j = 1; j < (int) V[i].size(); j++)
ans[V[i][j] - (n + 1)] = V[i][j - 1];
return L;
}
//----------------------
const int Mod = 1e9 + 9;
int mul(int a, int b){
return (1ll * a * b) % Mod;
}
int bin_pow(int b, int p){
int res = 1;
for(int j = 1, pw = b; j <= p; j <<= 1, pw = mul(pw, pw))
if(p & j)
res = mul(res, pw);
return res;
}
int countReplacement(int n, int a[]){
if(!valid(n, a)) return 0;
vector<int> A;
for(int i = 0; i < n; i++) A.pb(a[i]);
sort(A.begin(), A.end());
int res = 1;
int la = n;
for(int i = 0; i < n; i++){
if(A[i] <= n) continue;
res = (1ll * res * bin_pow(n - i, A[i] - la - 1)) % Mod;
la = A[i];
}
if(n < A[0]){
res = (1ll * res * n) % Mod;
return res;
}
return res;
}
/*
1
30
16 26 18 19 20 13 22 21 24 25 17 27 28 29 30 1 2 3 11 5 6 8 7 9 10 12 4 23 14 15
3
7
1 2 3 4 5 6 5
7
4
1 2 7 6
4
4
1 2 7 6
*/
# | 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... |