Skip to content

Commit 4e35eb4

Browse files
authored
Merge pull request #1265 from AlgorithmWithGod/khj20006
[20251029] BOJ / P1 / 쉽게 제한된 메모리 / 권혁준
2 parents 6861a9c + 122a6c8 commit 4e35eb4

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
using ll = long long;
5+
6+
const ll MOD = 1'000'000'007;
7+
ll N, X, A, B, Q, ans = 0, s[100]{}, e[100]{};
8+
vector<int> q, p;
9+
10+
int main() {
11+
cin.tie(0)->sync_with_stdio(0);
12+
13+
cin >> N >> X >> A >> B >> Q;
14+
q.resize(Q);
15+
p.resize(Q);
16+
iota(p.begin(), p.end(), 0);
17+
for (int& i : q) cin >> i, i++;
18+
fill(e, e + Q, MOD - 1);
19+
sort(q.begin(), q.end());
20+
21+
int rem = Q;
22+
while (rem) {
23+
vector<int> ms;
24+
for (int i = 0; i < Q; i++) if (s[i] < e[i]) ms.push_back((s[i] + e[i] + 1) >> 1);
25+
sort(ms.begin(), ms.end());
26+
ms.erase(unique(ms.begin(), ms.end()), ms.end());
27+
vector<int> cnt(ms.size());
28+
29+
int pos = 0;
30+
for (ll x = X, j = 0; j < N - 1; j++) {
31+
int d = upper_bound(ms.begin(), ms.end(), x) - ms.begin();
32+
if (d != ms.size()) cnt[d]++;
33+
x = (x * A + B) % MOD;
34+
}
35+
for (int i = 1; i < ms.size(); i++) cnt[i] += cnt[i - 1];
36+
37+
for (int i = 0; i < Q; i++) if (s[i] < e[i]) {
38+
int m = (s[i] + e[i] + 1) >> 1;
39+
int idx = lower_bound(ms.begin(), ms.end(), m) - ms.begin();
40+
if (cnt[idx] < q[i]) s[i] = m;
41+
else e[i] = m - 1;
42+
if (s[i] >= e[i]) ans += (s[i] + e[i] + 1) >> 1, rem--;
43+
}
44+
}
45+
46+
cout << ans;
47+
48+
}
49+
```

0 commit comments

Comments
 (0)