답안 #1065928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065928 2024-08-19T13:12:53 Z Shadow1 Cryptography (NOI20_crypto) C++17
100 / 100
298 ms 31684 KB
// Programmer: Shadow1

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!

#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << ' ';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define vt vector
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define umap unordered_map
#define uset unordered_set
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int ll
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());

// T: O(n^3)
// M : O(n + k log n) 

const int mxN = 50005, INF = 2e9;

vector<int64_t> ft;

int lsb(int x) {
	return (x & (-x));
}

void build(const vector<int64_t> &f) {  // Build the fenwick tree array from the frequency array
	int m = sz(f) - 1;
	ft.resize(m+1);
	for(int i=1; i<=m; ++i) {
		ft[i] += f[i];
		if(i + lsb(i) <= m)
			ft[i+lsb(i)] += ft[i];
	}
}

int64_t query(int i, int j) {  // i <= j   // O(log m)
	int64_t sum = 0;
	while(j > 0) {
		sum += ft[j];
		j -= lsb(j);
	}
	--i;
	while(i > 0) {
		sum -= ft[i];
		i -= lsb(i);
	}
	return sum;
}

void update(int i, int64_t v) {
	for(; i<sz(ft); i+=lsb(i))   // O(log m)
		ft[i] += v;
}

const int m = 1e9 + 7;
void solve() {
	int n;
	cin >> n;
	vector<int> a(n+1), fact(n+1), s(n+1);
	map<int,int> r;
	for(int i=1; i<=n; ++i) {
		cin >> a[i];
		s[i] = a[i];
	}
	sort(all(s));
	for(int i=1; i<=n; ++i)
		r[s[i]] = i;
	fact[0] = 1;
	for(int i=1; i<=n; ++i) {
		fact[i] = fact[i-1] * i;
		fact[i] %= m;
	}
	ft.assign(n+2, 0);
	update(r[a[n]], 1);
	int ans = 1;
	for(int i=n-1; i; --i) {
		int cnt = query(1, r[a[i]]-1);
		ans += (cnt * fact[n-i]) % m;
		ans %= m;
		update(r[a[i]], 1);
	}
	cout << ans << '\n';
}
	


signed main() {
	// freopen("output.txt", "w", stdout);
	// freopen("input.txt", "r", stdin);
	ios::sync_with_stdio(false);
	cin.tie(NULL); 
	int T = 1;
	// cin >> T;

	while(T--)
		solve();
	return 0;
}

/* CHECK :
1. COMPARATOR FUNCTION MUST RETURN FALSE WHEN ARGUMENTS ARE EQUAL!!!
2. Overflow! Typecase int64_t on operations if varaibles are int
3. Check array bounds!!!
4. Check array indexing!!!
5. Edge cases. (N==1)!!!
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 146 ms 31316 KB Output is correct
3 Correct 148 ms 31320 KB Output is correct
4 Correct 183 ms 31316 KB Output is correct
5 Correct 155 ms 31316 KB Output is correct
6 Correct 180 ms 31296 KB Output is correct
7 Correct 156 ms 31436 KB Output is correct
8 Correct 157 ms 31416 KB Output is correct
9 Correct 155 ms 31428 KB Output is correct
10 Correct 157 ms 31460 KB Output is correct
11 Correct 173 ms 31508 KB Output is correct
12 Correct 153 ms 31304 KB Output is correct
13 Correct 143 ms 31436 KB Output is correct
14 Correct 145 ms 31316 KB Output is correct
15 Correct 163 ms 31504 KB Output is correct
16 Correct 173 ms 31432 KB Output is correct
17 Correct 164 ms 31292 KB Output is correct
18 Correct 154 ms 31324 KB Output is correct
19 Correct 149 ms 31316 KB Output is correct
20 Correct 160 ms 31316 KB Output is correct
21 Correct 142 ms 31408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 160 ms 30480 KB Output is correct
3 Correct 153 ms 30548 KB Output is correct
4 Correct 166 ms 30436 KB Output is correct
5 Correct 152 ms 30548 KB Output is correct
6 Correct 167 ms 30392 KB Output is correct
7 Correct 149 ms 30404 KB Output is correct
8 Correct 144 ms 30552 KB Output is correct
9 Correct 144 ms 30380 KB Output is correct
10 Correct 148 ms 30544 KB Output is correct
11 Correct 141 ms 30532 KB Output is correct
12 Correct 139 ms 30548 KB Output is correct
13 Correct 145 ms 30544 KB Output is correct
14 Correct 138 ms 30548 KB Output is correct
15 Correct 151 ms 30548 KB Output is correct
16 Correct 145 ms 30584 KB Output is correct
17 Correct 144 ms 30548 KB Output is correct
18 Correct 144 ms 30544 KB Output is correct
19 Correct 141 ms 30408 KB Output is correct
20 Correct 141 ms 30544 KB Output is correct
21 Correct 152 ms 30548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 644 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 544 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 2 ms 664 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 460 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 644 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 1 ms 544 KB Output is correct
31 Correct 1 ms 724 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 2 ms 604 KB Output is correct
36 Correct 2 ms 604 KB Output is correct
37 Correct 2 ms 664 KB Output is correct
38 Correct 1 ms 724 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 604 KB Output is correct
45 Correct 1 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 604 KB Output is correct
49 Correct 2 ms 604 KB Output is correct
50 Correct 2 ms 724 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 2 ms 604 KB Output is correct
53 Correct 2 ms 604 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 1 ms 604 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 2 ms 604 KB Output is correct
58 Correct 2 ms 604 KB Output is correct
59 Correct 2 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 2 ms 556 KB Output is correct
63 Correct 2 ms 724 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 1 ms 564 KB Output is correct
66 Correct 1 ms 604 KB Output is correct
67 Correct 2 ms 600 KB Output is correct
68 Correct 2 ms 1112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 160 ms 30480 KB Output is correct
3 Correct 153 ms 30548 KB Output is correct
4 Correct 166 ms 30436 KB Output is correct
5 Correct 152 ms 30548 KB Output is correct
6 Correct 167 ms 30392 KB Output is correct
7 Correct 149 ms 30404 KB Output is correct
8 Correct 144 ms 30552 KB Output is correct
9 Correct 144 ms 30380 KB Output is correct
10 Correct 148 ms 30544 KB Output is correct
11 Correct 141 ms 30532 KB Output is correct
12 Correct 139 ms 30548 KB Output is correct
13 Correct 145 ms 30544 KB Output is correct
14 Correct 138 ms 30548 KB Output is correct
15 Correct 151 ms 30548 KB Output is correct
16 Correct 145 ms 30584 KB Output is correct
17 Correct 144 ms 30548 KB Output is correct
18 Correct 144 ms 30544 KB Output is correct
19 Correct 141 ms 30408 KB Output is correct
20 Correct 141 ms 30544 KB Output is correct
21 Correct 152 ms 30548 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 644 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 1 ms 544 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 664 KB Output is correct
35 Correct 1 ms 724 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 604 KB Output is correct
38 Correct 1 ms 604 KB Output is correct
39 Correct 1 ms 604 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 285 ms 30448 KB Output is correct
46 Correct 278 ms 30548 KB Output is correct
47 Correct 262 ms 30396 KB Output is correct
48 Correct 264 ms 30376 KB Output is correct
49 Correct 275 ms 30804 KB Output is correct
50 Correct 282 ms 30552 KB Output is correct
51 Correct 277 ms 30556 KB Output is correct
52 Correct 298 ms 30692 KB Output is correct
53 Correct 268 ms 30460 KB Output is correct
54 Correct 266 ms 30548 KB Output is correct
55 Correct 248 ms 30544 KB Output is correct
56 Correct 251 ms 30540 KB Output is correct
57 Correct 281 ms 30548 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 265 ms 30444 KB Output is correct
60 Correct 260 ms 30576 KB Output is correct
61 Correct 252 ms 30548 KB Output is correct
62 Correct 257 ms 30428 KB Output is correct
63 Correct 276 ms 30548 KB Output is correct
64 Correct 268 ms 30380 KB Output is correct
65 Correct 256 ms 30548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 460 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 146 ms 31316 KB Output is correct
27 Correct 148 ms 31320 KB Output is correct
28 Correct 183 ms 31316 KB Output is correct
29 Correct 155 ms 31316 KB Output is correct
30 Correct 180 ms 31296 KB Output is correct
31 Correct 156 ms 31436 KB Output is correct
32 Correct 157 ms 31416 KB Output is correct
33 Correct 155 ms 31428 KB Output is correct
34 Correct 157 ms 31460 KB Output is correct
35 Correct 173 ms 31508 KB Output is correct
36 Correct 153 ms 31304 KB Output is correct
37 Correct 143 ms 31436 KB Output is correct
38 Correct 145 ms 31316 KB Output is correct
39 Correct 163 ms 31504 KB Output is correct
40 Correct 173 ms 31432 KB Output is correct
41 Correct 164 ms 31292 KB Output is correct
42 Correct 154 ms 31324 KB Output is correct
43 Correct 149 ms 31316 KB Output is correct
44 Correct 160 ms 31316 KB Output is correct
45 Correct 142 ms 31408 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 160 ms 30480 KB Output is correct
48 Correct 153 ms 30548 KB Output is correct
49 Correct 166 ms 30436 KB Output is correct
50 Correct 152 ms 30548 KB Output is correct
51 Correct 167 ms 30392 KB Output is correct
52 Correct 149 ms 30404 KB Output is correct
53 Correct 144 ms 30552 KB Output is correct
54 Correct 144 ms 30380 KB Output is correct
55 Correct 148 ms 30544 KB Output is correct
56 Correct 141 ms 30532 KB Output is correct
57 Correct 139 ms 30548 KB Output is correct
58 Correct 145 ms 30544 KB Output is correct
59 Correct 138 ms 30548 KB Output is correct
60 Correct 151 ms 30548 KB Output is correct
61 Correct 145 ms 30584 KB Output is correct
62 Correct 144 ms 30548 KB Output is correct
63 Correct 144 ms 30544 KB Output is correct
64 Correct 141 ms 30408 KB Output is correct
65 Correct 141 ms 30544 KB Output is correct
66 Correct 152 ms 30548 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 1 ms 604 KB Output is correct
69 Correct 1 ms 604 KB Output is correct
70 Correct 1 ms 644 KB Output is correct
71 Correct 2 ms 604 KB Output is correct
72 Correct 1 ms 544 KB Output is correct
73 Correct 1 ms 724 KB Output is correct
74 Correct 2 ms 604 KB Output is correct
75 Correct 1 ms 604 KB Output is correct
76 Correct 1 ms 604 KB Output is correct
77 Correct 2 ms 604 KB Output is correct
78 Correct 2 ms 604 KB Output is correct
79 Correct 2 ms 664 KB Output is correct
80 Correct 1 ms 724 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 1 ms 604 KB Output is correct
83 Correct 1 ms 604 KB Output is correct
84 Correct 1 ms 604 KB Output is correct
85 Correct 1 ms 604 KB Output is correct
86 Correct 1 ms 604 KB Output is correct
87 Correct 1 ms 604 KB Output is correct
88 Correct 1 ms 604 KB Output is correct
89 Correct 0 ms 348 KB Output is correct
90 Correct 1 ms 604 KB Output is correct
91 Correct 2 ms 604 KB Output is correct
92 Correct 2 ms 724 KB Output is correct
93 Correct 1 ms 604 KB Output is correct
94 Correct 2 ms 604 KB Output is correct
95 Correct 2 ms 604 KB Output is correct
96 Correct 2 ms 604 KB Output is correct
97 Correct 1 ms 604 KB Output is correct
98 Correct 2 ms 604 KB Output is correct
99 Correct 2 ms 604 KB Output is correct
100 Correct 2 ms 604 KB Output is correct
101 Correct 2 ms 604 KB Output is correct
102 Correct 1 ms 604 KB Output is correct
103 Correct 0 ms 348 KB Output is correct
104 Correct 2 ms 556 KB Output is correct
105 Correct 2 ms 724 KB Output is correct
106 Correct 1 ms 604 KB Output is correct
107 Correct 1 ms 564 KB Output is correct
108 Correct 1 ms 604 KB Output is correct
109 Correct 2 ms 600 KB Output is correct
110 Correct 2 ms 1112 KB Output is correct
111 Correct 0 ms 348 KB Output is correct
112 Correct 285 ms 30448 KB Output is correct
113 Correct 278 ms 30548 KB Output is correct
114 Correct 262 ms 30396 KB Output is correct
115 Correct 264 ms 30376 KB Output is correct
116 Correct 275 ms 30804 KB Output is correct
117 Correct 282 ms 30552 KB Output is correct
118 Correct 277 ms 30556 KB Output is correct
119 Correct 298 ms 30692 KB Output is correct
120 Correct 268 ms 30460 KB Output is correct
121 Correct 266 ms 30548 KB Output is correct
122 Correct 248 ms 30544 KB Output is correct
123 Correct 251 ms 30540 KB Output is correct
124 Correct 281 ms 30548 KB Output is correct
125 Correct 0 ms 348 KB Output is correct
126 Correct 265 ms 30444 KB Output is correct
127 Correct 260 ms 30576 KB Output is correct
128 Correct 252 ms 30548 KB Output is correct
129 Correct 257 ms 30428 KB Output is correct
130 Correct 276 ms 30548 KB Output is correct
131 Correct 268 ms 30380 KB Output is correct
132 Correct 256 ms 30548 KB Output is correct
133 Correct 0 ms 348 KB Output is correct
134 Correct 256 ms 31408 KB Output is correct
135 Correct 263 ms 31316 KB Output is correct
136 Correct 253 ms 31572 KB Output is correct
137 Correct 257 ms 31684 KB Output is correct
138 Correct 258 ms 31312 KB Output is correct
139 Correct 257 ms 31316 KB Output is correct
140 Correct 258 ms 31512 KB Output is correct
141 Correct 269 ms 31416 KB Output is correct
142 Correct 282 ms 31316 KB Output is correct
143 Correct 266 ms 31536 KB Output is correct
144 Correct 247 ms 31316 KB Output is correct
145 Correct 250 ms 31316 KB Output is correct
146 Correct 264 ms 31392 KB Output is correct
147 Correct 0 ms 348 KB Output is correct
148 Correct 263 ms 31388 KB Output is correct
149 Correct 276 ms 31640 KB Output is correct
150 Correct 262 ms 31572 KB Output is correct
151 Correct 259 ms 31316 KB Output is correct
152 Correct 261 ms 31320 KB Output is correct
153 Correct 254 ms 31380 KB Output is correct
154 Correct 280 ms 31536 KB Output is correct