Submission #946419
Source Code Expand
/** * author: [itmo] enot.1.10 * created: 23.10.2016 15:26:20 **/ #define __USE_MINGW_ANSI_STDIO 0 #include <bits/stdc++.h> #define F first #define S second #define pb push_back #define mp make_pair #define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i) #define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr) #define sz(a) ((int)(a).size()) #define all(a) (a).begin(),a.end() #define pw(x) (1LL<<(x)) using namespace std; typedef long long ll; typedef double dbl; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = 1.01e9; const dbl eps = 1e-9; /* --- main part --- */ const int N = 5e5 + 10; const int mod = 1e9 + 7; int a[N]; int u[N]; int b[N], bc = 0; int fact[N]; int rfact[N]; int t[N]; inline void upd(int x, int dx) { x++; for (; x < N; x += x & -x) t[x] += dx; } inline int get(int x) { x++; int res = 0; for (; x > 0; x -= x & -x) res += t[x]; return res; } int t2[N]; inline void upd2(int x, int dx) { x++; for (; x < N; x += x & -x) t2[x] += dx; } inline int get2(int x) { x++; int res = 0; for (; x > 0; x -= x & -x) res += t2[x]; return res; } int rev(int x, int m) { if (x == 1) return 1; return (1 - rev(m % x, x) * (ll)m) / x + m; } int cnk(int n, int k) { return fact[n] * (ll)rfact[k] % mod * rfact[n - k] % mod; } int main() { #ifdef home assert(freopen("1.in", "r", stdin)); assert(freopen("1.out", "w", stdout)); #endif fact[0] = 1; for (int i = 1; i < N; ++i) fact[i] = fact[i - 1] * (ll)i % mod; for (int i = 0; i < N; ++i) rfact[i] = rev(fact[i], mod); int res = 0; int n; scanf("%d", &n); forn(i, n) scanf("%d", a + i); forn(i, n) a[i]--; forn(i, n) if (a[i] != -1) u[a[i]] = 1; forn(i, n) if (!u[i]) b[bc++] = i; forn(i, n) if (!u[i]) upd2(n - i, 1); int sumless = 0; int f = 0; for (int i = n - 1; i >= 0; --i) { if (a[i] >= 0) { int lss = get(a[i]); //for (int j = i + 1; j < n; ++j) if (a[j] != -1 && a[j] < a[i]) lss++; int big = get2(n - a[i]); int small = bc - big; int val = cnk(bc, f) * (ll)lss % mod; if (bc >= 1 && f > 0) val = (val + cnk(bc - 1, f - 1) * (ll)small) % mod; res = (res + val * (ll)fact[f] % mod * (ll)fact[bc - f] % mod * fact[n - i - 1]) % mod; upd(a[i], 1); sumless = (sumless + get2(n - a[i])) % mod; } else { int add = 0; int sum = bc * (ll)(bc - 1) % mod * rev(2, mod) % mod; int val = cnk(bc - 1, f) * (ll)sumless % mod; if (bc >= 2 && f > 0) val = (val + cnk(bc - 2, f - 1) * (ll)sum) % mod; res = (res + val * (ll)fact[f] % mod * fact[bc - 1 - f] % mod * fact[n - i - 1]) % mod; f++; } } res = (res + fact[bc]) % mod; printf("%d\n", res); #ifdef home eprintf("time = %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC)); #endif return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Encyclopedia of Permutations |
User | enot |
Language | C++14 (GCC 5.4.1) |
Score | 1200 |
Code Size | 3246 Byte |
Status | AC |
Exec Time | 313 ms |
Memory | 13696 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:96:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^ ./Main.cpp:97:34: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] forn(i, n) scanf("%d", a + i); ^
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 | 205 ms | 4224 KB |
0_001.txt | AC | 205 ms | 4224 KB |
0_002.txt | AC | 205 ms | 4224 KB |
0_003.txt | AC | 205 ms | 4224 KB |
0_004.txt | AC | 205 ms | 4224 KB |
1_005.txt | AC | 205 ms | 4224 KB |
1_006.txt | AC | 205 ms | 4224 KB |
1_007.txt | AC | 205 ms | 4352 KB |
1_008.txt | AC | 206 ms | 4352 KB |
1_009.txt | AC | 205 ms | 4224 KB |
1_010.txt | AC | 206 ms | 4224 KB |
1_011.txt | AC | 206 ms | 4224 KB |
1_012.txt | AC | 206 ms | 4224 KB |
1_013.txt | AC | 205 ms | 4352 KB |
1_014.txt | AC | 205 ms | 4224 KB |
1_015.txt | AC | 205 ms | 4224 KB |
1_016.txt | AC | 206 ms | 4352 KB |
1_017.txt | AC | 205 ms | 4224 KB |
1_018.txt | AC | 206 ms | 4224 KB |
1_019.txt | AC | 205 ms | 4224 KB |
1_020.txt | AC | 206 ms | 4224 KB |
1_021.txt | AC | 205 ms | 4224 KB |
1_022.txt | AC | 205 ms | 4224 KB |
1_023.txt | AC | 205 ms | 4224 KB |
1_024.txt | AC | 206 ms | 4224 KB |
1_025.txt | AC | 205 ms | 4352 KB |
1_026.txt | AC | 206 ms | 4224 KB |
1_027.txt | AC | 205 ms | 4224 KB |
1_028.txt | AC | 205 ms | 4224 KB |
1_029.txt | AC | 206 ms | 4224 KB |
1_030.txt | AC | 206 ms | 4224 KB |
1_031.txt | AC | 205 ms | 4224 KB |
1_032.txt | AC | 205 ms | 4224 KB |
2_033.txt | AC | 268 ms | 9984 KB |
2_034.txt | AC | 236 ms | 7040 KB |
2_035.txt | AC | 279 ms | 13696 KB |
2_036.txt | AC | 279 ms | 12928 KB |
2_037.txt | AC | 287 ms | 13440 KB |
2_038.txt | AC | 276 ms | 12416 KB |
2_039.txt | AC | 294 ms | 13312 KB |
2_040.txt | AC | 249 ms | 8704 KB |
2_041.txt | AC | 300 ms | 13056 KB |
2_042.txt | AC | 222 ms | 5888 KB |
2_043.txt | AC | 303 ms | 12800 KB |
2_044.txt | AC | 269 ms | 9856 KB |
2_045.txt | AC | 307 ms | 12672 KB |
2_046.txt | AC | 303 ms | 12288 KB |
2_047.txt | AC | 310 ms | 12416 KB |
2_048.txt | AC | 249 ms | 7808 KB |
2_049.txt | AC | 313 ms | 12160 KB |
2_050.txt | AC | 273 ms | 9472 KB |
2_051.txt | AC | 306 ms | 9984 KB |
2_052.txt | AC | 298 ms | 9472 KB |
2_053.txt | AC | 268 ms | 9984 KB |
2_054.txt | AC | 310 ms | 10112 KB |
2_055.txt | AC | 292 ms | 9984 KB |
2_056.txt | AC | 292 ms | 9984 KB |
2_057.txt | AC | 285 ms | 12928 KB |