Submission #1914601


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 (int 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) {
	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 (int i = 1; i <= N; i++) {
		scanf("%lld", &a[i]); if (a[i] >= 1)used[a[i]] = true;
	}
	for (int 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 (int i = 0; i < B.size(); i++) { fact2 *= (i + 1); fact2 %= mod; }
	long long cnt = 0, fact = 1, ret = 0, sum = 0, cnt2 = 0;
	for (int 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 {
			int 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 500
Code Size 1778 Byte
Status WA
Exec Time 1112 ms
Memory 14956 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:33: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 0 / 700
Status
AC × 5
AC × 33
AC × 51
WA × 7
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 4480 KB
1_010.txt AC 8 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 5 ms 4352 KB
1_016.txt AC 8 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 7 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 1111 ms 10608 KB
2_034.txt AC 556 ms 8432 KB
2_035.txt AC 1073 ms 13036 KB
2_036.txt AC 984 ms 12780 KB
2_037.txt WA 1028 ms 14060 KB
2_038.txt WA 901 ms 12908 KB
2_039.txt WA 984 ms 13928 KB
2_040.txt AC 491 ms 10992 KB
2_041.txt AC 940 ms 13876 KB
2_042.txt AC 178 ms 6088 KB
2_043.txt WA 894 ms 14560 KB
2_044.txt AC 586 ms 10936 KB
2_045.txt WA 849 ms 14956 KB
2_046.txt WA 814 ms 12772 KB
2_047.txt AC 791 ms 14060 KB
2_048.txt AC 345 ms 8304 KB
2_049.txt AC 731 ms 12908 KB
2_050.txt AC 482 ms 12908 KB
2_051.txt AC 578 ms 12524 KB
2_052.txt AC 521 ms 12012 KB
2_053.txt AC 1112 ms 10736 KB
2_054.txt AC 627 ms 12652 KB
2_055.txt AC 568 ms 12524 KB
2_056.txt AC 568 ms 12524 KB
2_057.txt WA 875 ms 12644 KB