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 inputSeq[])
{
int index = -1;
for (int i = 0;i<n;++i){
if (inputSeq[i] <=n){
index = i;
}
}
map<int,int>visited;
if (index == -1){
for (int i = 0;i<n;++i){
int x = inputSeq[i];
if (visited[x])return 0;
visited[x] = true;
}
return 1;
}
for (int i = 0;i<n;++i){
int j = (i + index)%n;
int temp = inputSeq[index] + i;
if (temp > n)temp = temp % n;
if (visited[temp] || visited[inputSeq[j]])return 0;
visited[temp] = true;
visited[inputSeq[j]] = true;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[])
{
int index = -1;
int maxxy = 1;
for (int i = 0;i<n;++i){
if (gondolaSeq[i] <=n){
index = i;
}
maxxy = max(gondolaSeq[i],maxxy);
}
map<int,int>visited;
int init = 1;
if (index == -1){
index = 0;
}
else init = gondolaSeq[index];
int extra = n + 1;
for (int i = 0;i<n;++i){
visited[gondolaSeq[i]] = i + 1;
}
int cur = 0;
for (int i = 0;i<n;){
int j = (i + index)%n;
int temp = init + i;
if (temp > n)temp = temp % n;
if (gondolaSeq[j] == temp){
++i;
continue;
}
if (gondolaSeq[j] < extra){
++i;
continue;
}
vector<int>pos;
while(!visited[extra]){
pos.push_back(extra);
extra++;
}
if (visited[extra] && extra!=gondolaSeq[j]){
int temp2 = visited[extra] - index - 1;
if (temp2 <= 0)temp2+=n;
temp2 = init + temp2;
if (temp2 > n)temp2 = temp2 % n;
replacementSeq[cur++] = temp2;
for (auto x:pos){
replacementSeq[cur++] = x;
}
extra++;
}
else{
replacementSeq[cur++] = temp;
for (auto x:pos){
replacementSeq[cur++] = x;
}
extra++;
++i;
}
}
return cur;
}
//----------------------
int countReplacement(int n, int inputSeq[])
{
if (!valid(n,inputSeq))return 0;
int index = -1;
for (int i = 0;i<n;++i){
if (inputSeq[i] <=n){
index = i;
}
}
map<int,int>visited;
const int mod = 1e9 + 9;
long long ans = 1;
int init = 1;
bool ok = false;
if (index == -1){
ok = true;
index = 0;
}
for (int i = 0;i<n;++i){
visited[inputSeq[i]] = i + 1;
}
int extra = n + 1;
vector<int>gondola;
for (int i = 0;i<n;++i)gondola.push_back(inputSeq[i]);
sort(gondola.begin(),gondola.end());
int time = 0;
vector<long long>arr(4 * n,0),pref(4 * n + 1,0);
vector<pair<long long,long long>>brr;
for (int i = 0;i<n;){
time++;
int j = (i + index)%n;
int temp = init + i;
if (temp > n)temp = temp % n;
if (inputSeq[j] == temp){
++i;
continue;
}
if (inputSeq[j] < extra){
++i;
continue;
}
long long counts = 0;
if(!visited[extra]){
int p = upper_bound(gondola.begin(),gondola.end(),extra) - gondola.begin();
counts = gondola[p] - extra;
extra = gondola[p];
brr.push_back({time,counts});
}
if (visited[extra] && extra!=inputSeq[j]){
int temp2 = visited[extra] - index - 1;
if (temp2 <= 0)temp2+=n;
temp2 = init + temp2;
if (temp2 > n)temp2 = temp2 % n;
arr[time]++;
extra++;
}
else{
arr[time]++;
extra++;
++i;
}
}
assert(time < 4 * n);
for (int i = 0;i<4 * n;++i){
pref[i + 1] = (pref[i] + arr[i])%mod;
}
auto power = [&](long long a,long long b){
long long res = 1;
while(b){
if (b & 1){
res = (res * a)%mod;
}
a = (a * a)%mod;
b>>=1;
}
return res;
};
for (auto x:brr){
long long temp = pref[4 * n] - pref[x.first];
//cout<<temp<<" "<<x.second<<'\n';
ans = (ans * power(temp,x.second))%mod;
}
if (ok)ans = (ans * n)%mod;
return ans;
}
# | 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... |