Submission #946662
Source Code Expand
#include <bits/stdc++.h> /* -------------------------------- Template -------------------------------- */ #define REP(i,n) for(int i=0;i<(int)(n);i++) #define ALL(x) (x).begin(),(x).end() using namespace std; using ll = long long; using ld = long double; template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template<typename T> T inf() { assert(false); } template<> constexpr int inf<int>() { return 1e9; } template<> constexpr ll inf<ll>() { return 1e18; } template<> constexpr ld inf<ld>() { return 1e30; } struct yes_no : numpunct<char> { string_type do_truename() const { return "Yes"; } string_type do_falsename() const { return "No"; } }; /* -------------------------------- Library -------------------------------- */ const int mod = 1000000007; struct Mod { int n; Mod () : n(0) {;} Mod (int m) : n(m) { if (n >= mod) n %= mod; else if (n < 0) n = (n % mod + mod) % mod; } }; bool operator==(Mod a, Mod b) { return a.n == b.n; } Mod operator+=(Mod &a, Mod b) { a.n += b.n; if (a.n >= mod) a.n -= mod; return a; } Mod operator-=(Mod &a, Mod b) { a.n -= b.n; if (a.n < 0) a.n += mod; return a; } Mod operator*=(Mod &a, Mod b) { a.n = ((long long)a.n * b.n) % mod; return a; } Mod operator+(Mod a, Mod b) { return a += b; } Mod operator-(Mod a, Mod b) { return a -= b; } Mod operator*(Mod a, Mod b) { return a *= b; } Mod operator^(Mod a, int n) { if (n == 0) return Mod(1); Mod res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } ll inv(ll a, ll p) { return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p); } Mod operator/(Mod a, Mod b) { return a * Mod(inv(b.n, mod)); } #define MAX_N 1024000 Mod fact[MAX_N], factinv[MAX_N]; void init() { fact[0] = Mod(1); factinv[0] = 1; REP(i,MAX_N-1) { fact[i+1] = fact[i] * Mod(i+1); factinv[i+1] = factinv[i] / Mod(i+1); } } Mod comb(int a, int b) { return fact[a] * factinv[b] * factinv[a-b]; } struct Data { int num; Data() : num(0) {;} Data(int n) : num(n) {;} }; inline Data Merge(Data left, Data right) { return Data(left.num + right.num); } struct BIT { static const int len = 1 << 19; vector<Data> data; BIT() : data(len, 0) {} void update(int i, Data value) { for (; i < len; i |= i+1) data[i] = Merge(data[i], value); } Data query(int i) { Data s = 0; for (; i >= 0; i = (i&(i+1))-1) s = Merge(s, data[i]); return s; } }; /* ---------------------------------- Main ---------------------------------- */ int pos[512000]; int val[512000]; int pos_sum[512000]; int val_sum[512000]; int main() { locale loc(locale(), new yes_no); cout << boolalpha; cout.imbue(loc); init(); int N; cin >> N; vector<int> P(N); for (int &i: P) { cin >> i; --i; } REP(i,N) { if (P[i] != -1) { pos[i] = 1; val[P[i]] = 1; } } REP(i,N) { pos_sum[i+1] = pos_sum[i] + pos[i]; val_sum[i+1] = val_sum[i] + val[i]; } Mod res = 0; const int cnt = N - (pos_sum[N] - pos_sum[0]); Mod base = fact[cnt]; const Mod half = inv(2LL, mod); Mod cntinv = cnt == 0 ? 0 : inv(ll(cnt), mod); int c = 0; Mod prob = 0; for (int i = N-1; i >= 0; --i) { if (P[i] != -1) { prob += Mod((N - P[i]) - (val_sum[N] - val_sum[P[i]])) * cntinv; } else { res += prob * fact[c]; prob += half; } ++c; } // cout << (base * res).n << endl; BIT bit; c = 0; int d = 0; for (int i = N-1; i >= 0; --i) { if (P[i] != -1) { // res += (Mod(d) * Mod((N - P[i]) - (val_sum[N] - val_sum[P[i]])) * cntinv res += (Mod(d) * Mod(P[i] - val_sum[P[i]]) * cntinv + Mod(bit.query(P[i]).num)) * fact[c]; bit.update(P[i], 1); } else ++d; ++c; } // cout << (base * res).n << endl; cout << (base * res + fact[cnt]).n << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Encyclopedia of Permutations |
User | asi1024 |
Language | C++14 (GCC 5.4.1) |
Score | 1200 |
Code Size | 4071 Byte |
Status | AC |
Exec Time | 554 ms |
Memory | 20096 KB |
Judge Result
Set Name | Sample | Subtask | All | ||||||
---|---|---|---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 500 / 500 | 700 / 700 | ||||||
Status |
|
|
|
Set Name | Test Cases |
---|---|
Sample | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt |
Subtask | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt |
All | 0_000.txt, 0_001.txt, 0_002.txt, 0_003.txt, 0_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 2_033.txt, 2_034.txt, 2_035.txt, 2_036.txt, 2_037.txt, 2_038.txt, 2_039.txt, 2_040.txt, 2_041.txt, 2_042.txt, 2_043.txt, 2_044.txt, 2_045.txt, 2_046.txt, 2_047.txt, 2_048.txt, 2_049.txt, 2_050.txt, 2_051.txt, 2_052.txt, 2_053.txt, 2_054.txt, 2_055.txt, 2_056.txt, 2_057.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_000.txt | AC | 355 ms | 10368 KB |
0_001.txt | AC | 355 ms | 10240 KB |
0_002.txt | AC | 355 ms | 10368 KB |
0_003.txt | AC | 355 ms | 10368 KB |
0_004.txt | AC | 355 ms | 10368 KB |
1_005.txt | AC | 355 ms | 10368 KB |
1_006.txt | AC | 355 ms | 10368 KB |
1_007.txt | AC | 355 ms | 10368 KB |
1_008.txt | AC | 355 ms | 10368 KB |
1_009.txt | AC | 355 ms | 10368 KB |
1_010.txt | AC | 356 ms | 10368 KB |
1_011.txt | AC | 355 ms | 10368 KB |
1_012.txt | AC | 355 ms | 10368 KB |
1_013.txt | AC | 355 ms | 10368 KB |
1_014.txt | AC | 356 ms | 10368 KB |
1_015.txt | AC | 355 ms | 10368 KB |
1_016.txt | AC | 356 ms | 10368 KB |
1_017.txt | AC | 355 ms | 10368 KB |
1_018.txt | AC | 356 ms | 10368 KB |
1_019.txt | AC | 355 ms | 10368 KB |
1_020.txt | AC | 356 ms | 10368 KB |
1_021.txt | AC | 355 ms | 10368 KB |
1_022.txt | AC | 356 ms | 10368 KB |
1_023.txt | AC | 355 ms | 10368 KB |
1_024.txt | AC | 356 ms | 10368 KB |
1_025.txt | AC | 355 ms | 10368 KB |
1_026.txt | AC | 356 ms | 10368 KB |
1_027.txt | AC | 355 ms | 10368 KB |
1_028.txt | AC | 357 ms | 10368 KB |
1_029.txt | AC | 356 ms | 10368 KB |
1_030.txt | AC | 356 ms | 10368 KB |
1_031.txt | AC | 356 ms | 10368 KB |
1_032.txt | AC | 360 ms | 10368 KB |
2_033.txt | AC | 445 ms | 16128 KB |
2_034.txt | AC | 400 ms | 13184 KB |
2_035.txt | AC | 460 ms | 20096 KB |
2_036.txt | AC | 453 ms | 19328 KB |
2_037.txt | AC | 473 ms | 20096 KB |
2_038.txt | AC | 459 ms | 18816 KB |
2_039.txt | AC | 487 ms | 20096 KB |
2_040.txt | AC | 420 ms | 15232 KB |
2_041.txt | AC | 499 ms | 20096 KB |
2_042.txt | AC | 381 ms | 12160 KB |
2_043.txt | AC | 510 ms | 20096 KB |
2_044.txt | AC | 456 ms | 16768 KB |
2_045.txt | AC | 521 ms | 20096 KB |
2_046.txt | AC | 514 ms | 19712 KB |
2_047.txt | AC | 534 ms | 20096 KB |
2_048.txt | AC | 431 ms | 14592 KB |
2_049.txt | AC | 544 ms | 20096 KB |
2_050.txt | AC | 477 ms | 16768 KB |
2_051.txt | AC | 554 ms | 20096 KB |
2_052.txt | AC | 534 ms | 19200 KB |
2_053.txt | AC | 444 ms | 16128 KB |
2_054.txt | AC | 554 ms | 20096 KB |
2_055.txt | AC | 532 ms | 20096 KB |
2_056.txt | AC | 533 ms | 20096 KB |
2_057.txt | AC | 493 ms | 20096 KB |