Submission #946047
Source Code Expand
#include <cstdio> #include <numeric> #include <iostream> #include <vector> #include <set> #include <cstring> #include <string> #include <map> #include <cmath> #include <ctime> #include <algorithm> #include <bitset> #include <queue> #include <sstream> #include <deque> using namespace std; #define mp make_pair #define pb push_back #define rep(i,n) for(int i = 0; i < (n); i++) #define re return #define fi first #define se second #define sz(x) ((int) (x).size()) #define all(x) (x).begin(), (x).end() #define sqrt(x) sqrt(abs(x)) #define y0 y3487465 #define y1 y8687969 #define fill(x,y) memset(x,y,sizeof(x)) #define prev PREV #define next NEXT typedef vector<int> vi; typedef long long ll; typedef long double ld; typedef double D; typedef pair<int, int> ii; typedef vector<ii> vii; typedef vector<string> vs; typedef vector<vi> vvi; template<class T> T abs(T x) { re x > 0 ? x : -x; } template<class T> inline T sqr (T x) { re x * x; } #define filename "" const int mod = 1000*1000*1000+7; const int N = 500010; int n; int m; int f[N]; int rf[N]; int p[N]; int q[N]; int cnt[N]; int fen[N]; int power (int a, int b) { int c = 1; while (b) { if (b & 1) c = ((ll)c * a) % mod; a = ((ll)a * a) % mod; b /= 2; } re c; } int get (int x) { int y = 0; while (x > 0) { y += fen[x]; x = (x & (x + 1)) - 1; } re y; } void add (int x, int y) { while (x <= n) { fen[x] += y; x |= x + 1; } } int main () { scanf ("%d", &n); int zero = 0; for (int i = 1; i <= n; i++) { scanf ("%d", &p[i]); q[p[i]] = i; zero += int (p[i] == 0); } cnt[0] = 0; int cur = 0, sum = 0, zerop = 0; for (int i = 1; i <= n; i++) { if (q[i] == 0) { zerop = (zerop + cur) % mod; cur++; sum = (sum + i - 1) % mod; } cnt[i] = cur; } f[0] = rf[0] = 1; for (int i = 1; i <= n; i++) { f[i] = ((ll)f[i - 1] * i) % mod; rf[i] = power (f[i], mod - 2); } int ans = 0, pos = 0, tot = 0; for (int i = 1; i <= n; i++) { int cur = 0; if (p[i] != 0) { cur = (cur + (ll)(p[i] - 1) * f[zero]) % mod; if (zero > 0) cur = (cur - (ll)pos * cnt[p[i] - 1] % mod * f[zero - 1] % mod + mod) % mod; cur = (cur - (ll)f[zero] * get (p[i] - 1) % mod + mod) % mod; add (p[i], 1); tot = (tot + zero - cnt[p[i]]) % mod; if (tot < 0) tot += mod; } else { cur = (cur + (ll)sum * (zero - 1)) % mod; if (zero > 1) cur = (cur - (ll)zerop * pos % mod + mod) % mod; cur = (cur - (ll)(zero - 1) * tot % mod + mod) % mod; cur = ((ll)cur * f[zero - 2]) % mod; pos++; } ans = (ans + (ll)cur * f[n - i]) % mod; } ans = (ans + f[zero]) % mod; printf ("%d\n", ans); return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Encyclopedia of Permutations |
User | vepifanov |
Language | C++14 (GCC 5.4.1) |
Score | 1200 |
Code Size | 2776 Byte |
Status | AC |
Exec Time | 252 ms |
Memory | 11904 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:87:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf ("%d", &n); ^ ./Main.cpp:90:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf ("%d", &p[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 | 3 ms | 256 KB |
0_001.txt | AC | 3 ms | 256 KB |
0_002.txt | AC | 3 ms | 384 KB |
0_003.txt | AC | 3 ms | 256 KB |
0_004.txt | AC | 3 ms | 256 KB |
1_005.txt | AC | 3 ms | 256 KB |
1_006.txt | AC | 3 ms | 256 KB |
1_007.txt | AC | 3 ms | 256 KB |
1_008.txt | AC | 4 ms | 384 KB |
1_009.txt | AC | 3 ms | 384 KB |
1_010.txt | AC | 4 ms | 384 KB |
1_011.txt | AC | 4 ms | 256 KB |
1_012.txt | AC | 4 ms | 384 KB |
1_013.txt | AC | 3 ms | 256 KB |
1_014.txt | AC | 4 ms | 384 KB |
1_015.txt | AC | 3 ms | 256 KB |
1_016.txt | AC | 4 ms | 384 KB |
1_017.txt | AC | 3 ms | 256 KB |
1_018.txt | AC | 4 ms | 384 KB |
1_019.txt | AC | 3 ms | 256 KB |
1_020.txt | AC | 4 ms | 384 KB |
1_021.txt | AC | 3 ms | 384 KB |
1_022.txt | AC | 4 ms | 384 KB |
1_023.txt | AC | 4 ms | 256 KB |
1_024.txt | AC | 4 ms | 384 KB |
1_025.txt | AC | 3 ms | 256 KB |
1_026.txt | AC | 4 ms | 384 KB |
1_027.txt | AC | 3 ms | 256 KB |
1_028.txt | AC | 4 ms | 384 KB |
1_029.txt | AC | 4 ms | 384 KB |
1_030.txt | AC | 4 ms | 384 KB |
1_031.txt | AC | 4 ms | 384 KB |
1_032.txt | AC | 4 ms | 384 KB |
2_033.txt | AC | 140 ms | 8064 KB |
2_034.txt | AC | 72 ms | 4224 KB |
2_035.txt | AC | 159 ms | 11904 KB |
2_036.txt | AC | 147 ms | 11008 KB |
2_037.txt | AC | 174 ms | 11904 KB |
2_038.txt | AC | 153 ms | 10496 KB |
2_039.txt | AC | 190 ms | 11904 KB |
2_040.txt | AC | 91 ms | 6144 KB |
2_041.txt | AC | 201 ms | 11904 KB |
2_042.txt | AC | 35 ms | 2432 KB |
2_043.txt | AC | 212 ms | 11904 KB |
2_044.txt | AC | 134 ms | 8064 KB |
2_045.txt | AC | 222 ms | 11904 KB |
2_046.txt | AC | 213 ms | 11520 KB |
2_047.txt | AC | 233 ms | 11904 KB |
2_048.txt | AC | 92 ms | 5376 KB |
2_049.txt | AC | 242 ms | 11904 KB |
2_050.txt | AC | 149 ms | 7936 KB |
2_051.txt | AC | 252 ms | 11904 KB |
2_052.txt | AC | 222 ms | 10880 KB |
2_053.txt | AC | 141 ms | 8192 KB |
2_054.txt | AC | 251 ms | 11904 KB |
2_055.txt | AC | 167 ms | 11904 KB |
2_056.txt | AC | 168 ms | 11904 KB |
2_057.txt | AC | 156 ms | 11904 KB |