# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
253863 | ChrisT | Gondola (IOI14_gondola) | C++17 | 0 ms | 0 KiB |
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<bits/stdc++.h>
using namespace std;
#include "gondola.h"
int valid (int n, int *a) {
set<int> seen;
for (int i = 0; i < n; i++) {
if (seen.count(a[i])) return 0;
seen.insert(a[i]);
}
for (int i = 0; i < n; i++) {
if (a[i] <= n) {
for (int j = (i+1)%n, cur = a[i]%n+1; j != i; j = (j + 1) % n, cur = cur % n + 1) {
if (a[j] <= n && a[j] != cur) return 0;
}
return 1;
}
}
return 1;
}
int replacement (int n, int *a, int *ret) {
vector<int> pos(250001,-1), cur(n); int mx = 0;
for (int i = 0; i < n; i++) if (a[i] <= n) {
cur[i] = a[i];
for (int j = (i+1)%n, go = a[i]%n+1; j != i; j = (j + 1) % n, go = go % n + 1) cur[j] = go;
break;
}
if (!cur[0]) for (int i = 0; i < n; i++) cur[i] = i+1;
for (int i = 0; i < n; i++) {
pos[a[i]] = i;
if (a[i] > a[mx]) mx = i;
}
int pp = 0;
for (int i = n+1; i <= a[mx]; i++) {
if (~pos[i]) ret[pp++] = cur[pos[i]], cur[pos[i]] = i;
else ret[pp++] = cur[mx], cur[mx] = i;
}
return pp;
}
int fpow (int base, int exp) {
int res = 1;
while (exp) {
if (exp&1) res = 1LL * res * base % MOD;
base = 1LL * base * base % MOD;
exp >>= 1;
}
return res;
}
int countReplacement (int n, int *a) {
if (!valid(n,a)) return 0;
sort(a,a+n);
int lst = n, ret = 1;
for (int i = 0; i < n; i++) if (a[i] > n) {
ret = ret * 1LL * fpow(n-i,a[i]-lst-1) % MOD;
lst = a[i];
}
if(a[0]>n)ret=ret*1LL*n%MOD;
return ret;
}