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"
#define ll long long
using namespace std;
int valid(int n, int inputSeq[])
{
int h=1;
for(int i=1;i<n;i++){
if(inputSeq[i]!=(inputSeq[i-1]%n+1))
h=0;
}
return h;
}
int replacement(int n, int gs[], int rs[])
{
vector<pair<ll,ll> > fi(n);
fi[0]=make_pair(-1,-1);
for(int i=0;i<n;i++){
if(gs[i]<=n){
for(int j=i;j<i+n;j++){
fi[j%n]=make_pair(-gs[j%n],(gs[i]-1+(j-i))%n+1);
}
break;
}
}
ll pt=0;
if(fi[0].second==-1)
for(int i=0;i<n;i++){
fi[i]=make_pair(-gs[i],i+1);
}
sort(fi.begin(),fi.end());
ll ind=n+1;
while(fi.size()){
ll k=fi.size()-1;
while(ind<=-fi[k].first){
rs[pt]=fi[k].second;
fi[k].second=ind;
ind++;
pt++;
}
fi.pop_back();
}
return pt;
}
const ll M=1000000007;
ll exp(ll a, ll b){
ll val=1;
ll cur=1;
while(b){
cur*=a;
cur%=M;
if(b&1) val*=cur;
b/=2;
val%=M;
}
return val;
}
//----------------------
int countReplacement(int n, int gs[])
{
set<int> ss;
for(int i=0;i<n;i++){
if(gs[i]<1)
return 0;
ss.insert(gs[i]);
}
if(ss.size()!=n) return 0;
for(int i=00;i<n;i++){
if(gs[i]<=n){
for(int j=i;j<=i+n;j++){
if(gs[j%n]<=n&&gs[j%n]!=(gs[i]-1+(j-i))%n+1)
return 0;
}
break;
}
}
ll ans=1;
bool h;
vector<pair<ll,ll> > fi(n);
fi[0]=make_pair(-1,-1);
for(int i=0;i<n;i++){
if(gs[i]<=n){
for(int j=i;j<i+n;j++){
fi[j%n]=make_pair(-gs[j%n],(gs[i]-1+(j-i))%n+1);
}
break;
}
}
ll pt=0;
h=(fi[0].second==-1);
if(fi[0].second==-1)
for(int i=0;i<n;i++){
fi[i]=make_pair(-gs[i],i+1);
}
sort(fi.begin(),fi.end());
ll ind=n+1;ll rest=n;
while(fi.size()){
ll k=fi.size()-1;
if(ind>=-fi[k].first){
if(ind==fi[k].first)
ind++;
fi.pop_back();
rest--;
continue;
}
ll t=(-fi[k].first-ind);
ans*=exp(rest,t);
ind=-fi[k].first+1;
ans%=M;
}
if(h)
ans*=n;
ans%=M;
return ans;
}
Compilation message (stderr)
gondola.cpp: In function 'int countReplacement(int, int*)':
gondola.cpp:70:17: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
70 | if(ss.size()!=n) return 0;
| ~~~~~~~~~^~~
gondola.cpp:92:6: warning: unused variable 'pt' [-Wunused-variable]
92 | ll pt=0;
| ^~
# | 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... |