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 <numeric>
#include <algorithm>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
using llong = long long;
#define BUGO(x) cerr << #x << " = " << x << '\n';
int valid(int n, int a[])
{
if (set<int>(a, a + n).size() != n) return false;
vector<int> must(n, -1);
for (int i = 0; i < n; ++i)
if (a[i] <= n) {
// for (int j = 0; j < n; ++j) {
for (int j = i; j < i + n; ++j) {
must[j % n] = (a[i] + j - i) % n;
if (!must[j % n]) must[j % n] = n;
}
break;
}
if (must[0] == -1)
iota(must.begin(), must.end(), 1);
for (int i = 0; i < n; ++i)
if (must[i] > a[i]) return false;
return true;
}
//----------------------
int replacement(int n, int a[], int ans[])
{
int anslen = 0;
vector<int> must(n, -1);
for (int i = 0; i < n; ++i)
if (a[i] <= n) {
for (int j = i; j < i + n; ++j) {
must[j - i] = (a[i] + j - i) % n;
if (!must[j % n]) must[j % n] = n;
}
break;
}
if (must[0] == -1)
iota(must.begin(), must.end(), 1);
vector<int> inds; inds.reserve(n);
for (int i = 0; i < n; ++i)
if (a[i] > n)
inds.push_back(i);
auto comp = [&a](int l, int r) {
return a[l] < a[r];
};
sort(inds.begin(), inds.end(), comp);
int gondol = n + 1;
for (int i: inds)
while (must[i] < a[i]) {
ans[anslen++] = must[i];
must[i] = gondol++;
}
return anslen;
}
//----------------------
const llong mod = 1000'000'009;
llong binpow(llong a, int p) {
if (p == 0) return 1;
if (p % 2) return (a * binpow(a, p - 1)) % mod;
return binpow((a * a) % mod, p / 2);
}
int countReplacement(int n, int a[])
{
if (!valid(n, a)) return 0;
if (*max_element(a, a + n) == n) return 1;
vector<int> b; b.reserve(n);
for (int i = 0; i < n; ++i)
if (a[i] > n)
b.push_back(a[i]);
llong ans = 1;
b.push_back(n);
sort(b.begin(), b.end());
for (int i = 0; i + 1 < b.size(); ++i)
{
ans *= binpow((int)b.size() - 1 - i, b[i + 1] - b[i] - 1);
ans %= mod;
}
return ans;
}
Compilation message (stderr)
gondola.cpp: In function 'int valid(int, int*)':
gondola.cpp:13:35: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
13 | if (set<int>(a, a + n).size() != n) return false;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int i = 0; i + 1 < b.size(); ++i)
| ~~~~~~^~~~~~~~~~
# | 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... |