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>
#include "gondola.h"
using namespace std;
typedef long long int ll;
const ll MOD = 1e9 + 9;
const int MAXN = 1e5 + 10;
int expect[MAXN];
map<int, int> marc;
ll mult(ll a, ll b)
{
return ((a * b) % MOD);
}
ll exp(ll x, ll n)
{
x %= MOD;
assert(n > 0);
if (n == 0) return 1LL;
if (n == 1) return (x % MOD);
if (n % 2) return ((x * (exp(x, n-1LL) % MOD)) % MOD);
else
{
ll aux = exp(x, n/2)%MOD;
// cerr << x << " " << n << " " << aux <<'\n';
assert(aux > 0);
return ((aux * aux) % MOD);
}
}
int valid(int n, int inputSeq[])
{
for (int i = 0; i < n; i++)
{
if (marc[inputSeq[i]]) return 0;
marc[inputSeq[i]] = 1;
}
bool Achei = false;
for (int i = 0; i < n; i++)
{
if (!Achei && inputSeq[i] > n) continue;
else if (!Achei && inputSeq[i] <= n)
{
expect[i] = inputSeq[i];
Achei = true;
}
else if (Achei) expect[i] = (expect[i-1]+1);
if (expect[i] > n) expect[i] -= n;
}
if (!expect[0]) expect[0] = expect[n-1]+1;
if (expect[0] > n) expect[0] -= n;
for (int i = 1; i < n && !expect[i]; i++)
{
expect[i] = expect[i-1]+1;
if (expect[i] > n) expect[i] -= n;
}
for (int i = 0; i < n; i++)
{
if (inputSeq[i] > n) continue;
if (expect[i] != inputSeq[i]) return 0;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
if (!valid(n, gondolaSeq)) return 0;
int tam = 0;
int atual = n+1;
set<tuple<int, int, int> > fora;
for (int i = 0; i < n; i++) if (gondolaSeq[i] > n) fora.emplace(gondolaSeq[i], expect[i], i);
while (!fora.empty())
{
auto [quero, tenho, id] = *fora.begin();
fora.erase(fora.begin());
replacementSeq[tam++] = tenho;
if (atual < quero) fora.emplace(quero, atual, id);
atual++;
}
return tam;
}
//----------------------
int countReplacement(int n, int inputSeq[])
{
// cerr << "ABA" << '\n';
if (!valid(n, inputSeq)) return 0;
set<ll> fora;
// cerr << "CABA" << '\n';
for (int i = 0; i < n; i++) if (inputSeq[i] > n) fora.emplace((long long)inputSeq[i]);
ll atual = n+1;
ll resp = 1LL;
// cerr << "SABA" << '\n';
while (!fora.empty())
{
if (atual == *fora.begin()) {atual++; fora.erase(fora.begin()); continue;}
resp %= MOD;
resp *= exp((long long)fora.size(), ((long long)(*fora.begin()) - atual));
resp %= MOD;
// cerr << "||" << fora.size() << '\n';
assert(resp > 0);
atual = (*fora.begin());
atual++;
fora.erase(fora.begin());
}
// cerr << "VABA" << '\n';
return (int)resp;
}
# | 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... |