이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "gondola.h"
using namespace std;
typedef long long ll;
#define X first
#define Y second
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 9;
ll poww(ll x , ll y){
ll ans = 1;
for(; y ; y >>= 1 , x = x * x % MOD) if(y & 1) ans = ans * x % MOD;
return ans;
}
int fact[MAXN] , inv[MAXN] , res[MAXN];
map<int , int> mark;
int valid(int n, int inputSeq[]){
int ind = -1;
for(int i = 0 ; i < n ; i++){
if(mark[inputSeq[i]]) return 0;
mark[inputSeq[i]] = 1;
if(inputSeq[i] <= n) ind = i;
}
if(ind == -1){
for(int i = 0 ; i < n ; i++) res[i] = i + 1;
return 1;
}
for(int i = 0 ; i < n ; i++){
res[(ind + i) % n] = inputSeq[ind] + i;
if(res[(ind + i) % n] > n) res[(ind + i) % n] -= n;
}
for(int i = 0 ; i < n ; i++){
if(inputSeq[i] <= n && res[i] != inputSeq[i]) return 0;
}
return 1;
}
//----------------------
int replacement(int n, int gondolaSeq[], int replacementSeq[]){
if(valid(n , gondolaSeq) == 0) return -1;
vector<pair<int , int>> vec; int len = 0;
for(int i = 0 ; i < n ; i++){
if(gondolaSeq[i] > n) vec.push_back({gondolaSeq[i] , res[i]});
}
sort(vec.begin() , vec.end());
int prv = n;
for(auto &i : vec){
replacementSeq[len++] = i.Y;
for(int j = prv + 1 ; j < i.X ; j++) replacementSeq[len++] = j;
prv = i.X;
}
return len;
}
//----------------------
int countReplacement(int n, int inputSeq[]){
if(valid(n , inputSeq) == 0) return 0;
fact[0] = 1;
for(int i = 1 ; i < MAXN ; i++) fact[i] = 1ll * fact[i - 1] * i % MOD;
inv[MAXN - 1] = poww(fact[MAXN - 1] , MOD - 2);
for(int i = MAXN - 2 ; i >= 0 ; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
int mx = 0 , k = 0;
for(int i = 0 ; i < n ; i++){
mx = max(mx , inputSeq[i]);
mark[inputSeq[i]] = 1; k += (inputSeq[i] > n);
}
ll ans = 1; int cnt = mx - n;
for(int i = mx ; i >= 1 ; i--){
if(mark[i]) continue;
//cout << i << endl;
ans = ans * (cnt - max(0 , i - n)) % MOD; cnt--;
}
return ans * inv[k] % MOD;
}
# | 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... |