제출 #385849

#제출 시각아이디문제언어결과실행 시간메모리
385849alireza_kaviani곤돌라 (IOI14_gondola)C++11
65 / 100
25 ms3948 KiB
#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] , mark[MAXN]; int valid(int n, int inputSeq[]){ int ind = -1; for(int i = 0 ; i < n ; i++){ 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...