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 <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<int> vi;
int valid(int n, int inputSeq[])
{
FOR(i, 0, n-1) inputSeq[i]--;
unordered_set<int> cont;
FOR(i, 0, n-1) cont.insert(inputSeq[i]);
if ((int)cont.size() < n) return false;
int ch = -1;
FOR(i, 0, n-1) {
if (inputSeq[i] >= n) continue;
ch = i;
break;
}
if (ch == -1) return true;
FOR(i, 0, n-1) {
int id = (ch+i)%n;
if (inputSeq[id] >= n) continue;
int shouldBe = (inputSeq[ch]+i)%n;
if (inputSeq[id] != shouldBe) return false;
}
return true;
}
//----------------------
int replacement(int n, int inputSeq[], int replacementSeq[])
{
FOR(i, 0, n-1) inputSeq[i]--;
int l = 0;
int ch = 0;
FOR(i, 0, n-1) {
if (inputSeq[i] >= n) continue;
ch = i;
break;
}
int cur[n];
cur[ch] = (inputSeq[ch] < n ? inputSeq[ch] : 0);
FOR(i, 0, n-1) {
int id = (ch+i)%n;
int shouldBe = (cur[ch]+i)%n;
cur[id] = shouldBe;
}
vii seq;
FOR(i, 0, n-1) {
seq.pb({inputSeq[i], i});
}
sort(seq.begin(), seq.end());
int crAdd = n;
for (auto p : seq) {
int id = p.s;
while (cur[id] < inputSeq[id]) {
replacementSeq[l] = cur[id]+1;
cur[id] = crAdd;
crAdd++;
l++;
}
}
return l;
}
//----------------------
const ll MOD = 1e9+7;
ll pwr (ll a, ll p) {
if (p == 0) return 1ll;
if (p == 1) return a%MOD;
ll ret = pwr(a, p/2);
ret = (ret*ret)%MOD;
if (p%2 == 1) ret *= a%MOD;
return ret%MOD;
}
int countReplacement(int n, int inputSeq[])
{
/*cout << " 3^76 = " << pwr(3, 76) << endl;
cout << " 3^77 = " << pwr(3, 77) << endl;
cout << " 3^79 = " << pwr(3, 79) << endl;
cout << " 3^80 = " << pwr(3, 80) << endl;
cout << " 3^81 = " << pwr(3, 81) << endl;*/
/*FOR(i, 0, 200) {
if (pwr(3, i) == 224280120) cout << " 3^" << i << " = " << pwr(3,i) << endl;
if (pwr(i, 3) == 224280120) cout << " "<<i << "^3" << " = " << pwr(i,3) << endl;
}*/
ll ans = 1;
vii seq;
FOR(i, 0, n-1) {
seq.pb({inputSeq[i], i});
}
sort(seq.begin(), seq.end());
ll rem = n;
ll lst = n;
bool found = false;
for (auto p : seq) {
int id = p.s;
if (inputSeq[id] > n) {
ll cnt = inputSeq[id] - lst - 1;
//cout << " inputseq id = " << inputSeq[id] << " lst = " << lst << endl;
//cout << " id = " << id << " rem = " << rem << " cnt = " << cnt << endl;
ll cr = pwr(rem, cnt);
ans = (ans*cr)%MOD;
lst = inputSeq[id];
} else found = true;
rem--;
}
if (!found) ans = (ans*n)%MOD;
if (!valid(n, inputSeq)) return 0;
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... |