Submission #945759
Source Code Expand
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define get dlafkjsdlfajks const int M = 500100; const int mod = 1e9 + 7; int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } int mul(ll x, ll y) { return x * y % mod; } int fact[M]; void pre() { fact[0] = 1; for (int i = 1; i < M; ++i) { fact[i] = mul(i, fact[i - 1]); } } int bin(int x, int to) { int y = 1; while (to) { if (to & 1) y = mul(x, y); x = mul(x, x); to >>= 1; } return y; } int inv(int x) { //assert(x != 0); return bin(x, mod - 2); } int f[M]; int n, p[M], sm[M]; bool u[M]; void addf(int at, int what) { for (int i = at; i < M; i |= (i + 1)) f[i] += what; } int get(int r) { int ans = 0; for (int i = r - 1; i >= 0; i = (i & (i + 1)) - 1) ans += f[i]; return ans; } int get(int l, int r) { return get(r) - get(l); } void read() { cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i]; --p[i]; if (p[i] != -1) { u[p[i]] = true; } } } void kill() { int cnt = 0; for (int i = 0; i < n; ++i) if (!u[i]) { sm[i] = 1; ++cnt; } for (int i = 1; i <= n; ++i) sm[i] += sm[i - 1]; int ans = 0; int ma = 0; int i2 = (mod + 1) / 2; int sum = 0; for (int i = n - 1; i >= 0; --i) { int bon = 0; if (p[i] == -1) { bon = add(bon, mul(ma, i2)); bon = add(bon, mul(sum, inv(cnt))); ++ma; } else { bon = add(bon, mul(mul(ma, sm[p[i]]), inv(cnt))); bon = add(bon, get(-1, p[i])); addf(p[i], 1); sum = add(sum, cnt - sm[p[i]]); } int rest = n - 1 - i; ans = add(ans, mul(bon, fact[rest])); } ans = add(ans, 1); ans = mul(ans, fact[cnt]); cout << ans << endl; } int main() { cout.precision(20); cout << fixed; ios_base::sync_with_stdio(false); pre(); read(); kill(); }
Submission Info
Submission Time | |
---|---|
Task | E - Encyclopedia of Permutations |
User | Arterm |
Language | C++14 (GCC 5.4.1) |
Score | 1200 |
Code Size | 2044 Byte |
Status | AC |
Exec Time | 208 ms |
Memory | 8704 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 | 8 ms | 2304 KB |
0_001.txt | AC | 8 ms | 2176 KB |
0_002.txt | AC | 8 ms | 2304 KB |
0_003.txt | AC | 8 ms | 2176 KB |
0_004.txt | AC | 8 ms | 2304 KB |
1_005.txt | AC | 8 ms | 2304 KB |
1_006.txt | AC | 8 ms | 2304 KB |
1_007.txt | AC | 8 ms | 2304 KB |
1_008.txt | AC | 8 ms | 2304 KB |
1_009.txt | AC | 8 ms | 2304 KB |
1_010.txt | AC | 8 ms | 2304 KB |
1_011.txt | AC | 8 ms | 2304 KB |
1_012.txt | AC | 8 ms | 2304 KB |
1_013.txt | AC | 8 ms | 2304 KB |
1_014.txt | AC | 9 ms | 2304 KB |
1_015.txt | AC | 8 ms | 2304 KB |
1_016.txt | AC | 9 ms | 2304 KB |
1_017.txt | AC | 8 ms | 2304 KB |
1_018.txt | AC | 9 ms | 2304 KB |
1_019.txt | AC | 8 ms | 2304 KB |
1_020.txt | AC | 9 ms | 2304 KB |
1_021.txt | AC | 8 ms | 2304 KB |
1_022.txt | AC | 9 ms | 2304 KB |
1_023.txt | AC | 8 ms | 2304 KB |
1_024.txt | AC | 9 ms | 2432 KB |
1_025.txt | AC | 8 ms | 2304 KB |
1_026.txt | AC | 9 ms | 2304 KB |
1_027.txt | AC | 8 ms | 2304 KB |
1_028.txt | AC | 8 ms | 2304 KB |
1_029.txt | AC | 9 ms | 2304 KB |
1_030.txt | AC | 9 ms | 2304 KB |
1_031.txt | AC | 9 ms | 2432 KB |
1_032.txt | AC | 8 ms | 2304 KB |
2_033.txt | AC | 127 ms | 6144 KB |
2_034.txt | AC | 67 ms | 4224 KB |
2_035.txt | AC | 145 ms | 8576 KB |
2_036.txt | AC | 134 ms | 8064 KB |
2_037.txt | AC | 158 ms | 8576 KB |
2_038.txt | AC | 139 ms | 7808 KB |
2_039.txt | AC | 169 ms | 8576 KB |
2_040.txt | AC | 85 ms | 5376 KB |
2_041.txt | AC | 178 ms | 8704 KB |
2_042.txt | AC | 36 ms | 3456 KB |
2_043.txt | AC | 184 ms | 8576 KB |
2_044.txt | AC | 118 ms | 6400 KB |
2_045.txt | AC | 189 ms | 8576 KB |
2_046.txt | AC | 180 ms | 8320 KB |
2_047.txt | AC | 194 ms | 8576 KB |
2_048.txt | AC | 83 ms | 4992 KB |
2_049.txt | AC | 200 ms | 8576 KB |
2_050.txt | AC | 128 ms | 6400 KB |
2_051.txt | AC | 208 ms | 8576 KB |
2_052.txt | AC | 185 ms | 8064 KB |
2_053.txt | AC | 128 ms | 6144 KB |
2_054.txt | AC | 206 ms | 8576 KB |
2_055.txt | AC | 156 ms | 8576 KB |
2_056.txt | AC | 154 ms | 8704 KB |
2_057.txt | AC | 144 ms | 8576 KB |