Submission #1914644


Source Code Expand

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

long long N, a[500009], bit[500009], mod = 1000000007; bool used[500009]; vector<long long>A, B;

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (long long i = 0; i < 63; i++) {
		if ((b&(1LL << i)) != 0) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}
long long Div(long long a, long long b, long long m) {
	a %= m;
	return (a*modpow(b, m - 2, m)) % m;
}
void Add(long long p, long long x) {
	p++;
	while (p <= 500008) { bit[p] += x; p += (p&-p); }
}
long long Sum(long long p) {
	p++; long long s = 0;
	while (p >= 1) { s += bit[p]; p -= (p&-p); }
	return s;
}

int main() {
	cin >> N;
	for (long long i = 1; i <= N; i++) {
		scanf("%lld", &a[i]); if (a[i] >= 1)used[a[i]] = true;
	}
	for (long long i = 1; i <= N; i++) {
		if (used[i] == true)A.push_back(i);
		if (used[i] == false)B.push_back(i);
	}
	long long fact2 = 1;
	for (long long i = 0; i < B.size(); i++) { fact2 *= (i + 1); fact2 %= mod; }
	long long cnt = 0, fact = 1, ret = 0, sum = 0, cnt2 = 0;
	for (long long i = N; i >= 1; i--) {
		if (a[i] == 0) {
			long long V = sum; V = Div(V, B.size(), mod);
			long long W = cnt2; W = Div(W, 2, mod);
			long long X = V + W; X *= fact2; X %= mod;
			ret += X * fact; ret %= mod; cnt2++;
		}
		else {
			long long pos1 = lower_bound(B.begin(), B.end(), a[i]) - B.begin();
			sum += (B.size() - pos1);
			long long V = Sum(a[i]); Add(a[i], 1);
			long long W = pos1; W = Div(W, B.size(), mod); W *= cnt2; W %= mod;
			long long X = V + W; X *= fact2; X %= mod;
			ret += X*fact; ret %= mod;
		}
		cnt++; fact *= cnt; fact %= mod;
	}
	ret += fact2; ret %= mod;
	cout << ret << endl;
	return 0;
}

Submission Info

Submission Time
Task E - Encyclopedia of Permutations
User E869120
Language C++14 (GCC 5.4.1)
Score 1200
Code Size 1824 Byte
Status AC
Exec Time 1118 ms
Memory 14828 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:34:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &a[i]); if (a[i] >= 1)used[a[i]] = true;
                       ^

Judge Result

Set Name Sample Subtask All
Score / Max Score 0 / 0 500 / 500 700 / 700
Status
AC × 5
AC × 33
AC × 58
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 2 ms 4352 KB
0_001.txt AC 2 ms 2304 KB
0_002.txt AC 2 ms 4352 KB
0_003.txt AC 2 ms 2304 KB
0_004.txt AC 2 ms 4352 KB
1_005.txt AC 2 ms 4352 KB
1_006.txt AC 2 ms 4352 KB
1_007.txt AC 2 ms 4352 KB
1_008.txt AC 9 ms 4480 KB
1_009.txt AC 5 ms 4352 KB
1_010.txt AC 9 ms 4480 KB
1_011.txt AC 7 ms 4480 KB
1_012.txt AC 8 ms 4480 KB
1_013.txt AC 4 ms 4352 KB
1_014.txt AC 8 ms 4480 KB
1_015.txt AC 4 ms 4352 KB
1_016.txt AC 7 ms 4480 KB
1_017.txt AC 2 ms 4352 KB
1_018.txt AC 7 ms 4480 KB
1_019.txt AC 4 ms 4352 KB
1_020.txt AC 7 ms 4480 KB
1_021.txt AC 4 ms 4352 KB
1_022.txt AC 6 ms 4480 KB
1_023.txt AC 5 ms 4480 KB
1_024.txt AC 6 ms 4480 KB
1_025.txt AC 2 ms 4352 KB
1_026.txt AC 5 ms 4480 KB
1_027.txt AC 4 ms 4480 KB
1_028.txt AC 9 ms 4480 KB
1_029.txt AC 6 ms 4480 KB
1_030.txt AC 5 ms 4480 KB
1_031.txt AC 5 ms 4480 KB
1_032.txt AC 7 ms 4480 KB
2_033.txt AC 1118 ms 10604 KB
2_034.txt AC 559 ms 8432 KB
2_035.txt AC 1080 ms 12908 KB
2_036.txt AC 990 ms 12524 KB
2_037.txt AC 1035 ms 13420 KB
2_038.txt AC 908 ms 13164 KB
2_039.txt AC 990 ms 14056 KB
2_040.txt AC 495 ms 10992 KB
2_041.txt AC 943 ms 14004 KB
2_042.txt AC 180 ms 6088 KB
2_043.txt AC 897 ms 14048 KB
2_044.txt AC 587 ms 11064 KB
2_045.txt AC 846 ms 14828 KB
2_046.txt AC 811 ms 13668 KB
2_047.txt AC 793 ms 13420 KB
2_048.txt AC 348 ms 8304 KB
2_049.txt AC 737 ms 12908 KB
2_050.txt AC 484 ms 11500 KB
2_051.txt AC 573 ms 12524 KB
2_052.txt AC 518 ms 12012 KB
2_053.txt AC 1118 ms 10736 KB
2_054.txt AC 626 ms 12524 KB
2_055.txt AC 564 ms 12524 KB
2_056.txt AC 565 ms 12524 KB
2_057.txt AC 880 ms 12644 KB