# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1084831 |
2024-09-07T04:45:20 Z |
kilkuwu |
Trains (BOI24_trains) |
C++17 |
|
350 ms |
132220 KB |
#include <bits/stdc++.h>
#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
#define nl '\n'
template <int md>
struct Modular {
int v;
constexpr Modular() : v(0) {}
template <typename T>
static inline int normalize(const T& x) {
int res = -mod() <= x && x < mod() ? static_cast<int>(x) : static_cast<int>(x % mod());
return res + (res < 0) * mod();
}
static constexpr int mod() {
return md;
}
template <typename U>
Modular(const U& x) : v(normalize(x)) {}
const int& operator()() const {
return v;
}
template <typename U>
explicit operator U() const {
return static_cast<U>(v);
}
using M = Modular;
template <typename U>
friend std::enable_if_t<std::is_integral_v<U>, M> power(M b, U e) {
assert(e >= 0);
M ans = 1;
while (e) {
if (e & 1) ans *= b;
b *= b;
e >>= 1;
}
return ans;
}
M inv() const {
M res = power(*this, mod() - 2);
return res;
}
M& operator+=(const M& y) {
return v += y.v, v -= (v >= mod()) * mod(), *this;
}
M& operator-=(const M& y) {
return v -= y.v, v += (v < 0) * mod(), *this;
}
M& operator*=(const M& y) {
return v = (int)((int64_t)v * y.v % mod()), *this;
}
M& operator/=(const M& y) {
return *this *= y.inv();
}
M& operator++() {
return v++, v = (v == mod()) ? 0 : v, *this;
}
M& operator--() {
return v = (v == 0) ? mod() : v, v--, *this;
}
M operator++(int) {
M res(*this);
return ++(*this), res;
}
M operator--(int) {
M res(*this);
return --(*this), res;
}
M operator+() const {
return M(v);
}
M operator-() const {
return M(-v);
}
friend bool operator==(const M& x, const M& y) {
return x.v == y.v;
}
friend bool operator<(const M& x, const M& y) {
return x.v < y.v;
}
friend bool operator>(const M& x, const M& y) {
return x.v > y.v;
}
friend bool operator<=(const M& x, const M& y) {
return x.v <= y.v;
}
friend bool operator>=(const M& x, const M& y) {
return x.v >= y.v;
}
friend bool operator!=(const M& x, const M& y) {
return x.v != y.v;
}
template <typename Istream>
friend Istream& operator>>(Istream& is, M& y) {
int64_t x;
is >> x;
y.v = y.normalize(x);
return is;
}
template <typename Ostream>
friend Ostream& operator<<(Ostream& os, const M& y) {
return os << y.v;
}
friend M operator+(const M& x, const M& y) {
return M(x) += y;
}
friend M operator-(const M& x, const M& y) {
return M(x) -= y;
}
friend M operator*(const M& x, const M& y) {
return M(x) *= y;
}
friend M operator/(const M& x, const M& y) {
return M(x) /= y;
}
};
constexpr int md = 1e9 + 7;
using Mint = Modular<md>;
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::vector<int> d(N), x(N);
for (int i = 0; i < N; i++) {
std::cin >> d[i] >> x[i];
}
std::vector<std::pair<int, int>> compressed;
for (int i = 0; i < N; i++) {
if (d[i] != 0) {
compressed.emplace_back(i % d[i], d[i]);
}
}
std::sort(compressed.begin(), compressed.end());
compressed.erase(std::unique(compressed.begin(), compressed.end()), compressed.end());
auto get_id = [&](std::pair<int, int> key) -> int {
return std::lower_bound(compressed.begin(), compressed.end(), key) - compressed.begin();
};
int sz = compressed.size();
std::vector<int> belong(N, -1);
for (int i = 0; i < N; i++) {
if (d[i] != 0) {
belong[i] = get_id({i % d[i], d[i]});
}
}
int B = std::sqrt(N);
//dbg(B);
int num_block = (N - 1) / B + 1;
std::vector<std::vector<Mint>> add(sz, std::vector<Mint>(num_block));
std::vector<int> has_update(num_block, 0);
std::vector<Mint> dp(N);
auto collapse_block = [&](int b) -> void { // O(N)
if (!has_update[b]) return;
has_update[b] = 0;
int lb = b * B;
int rb = std::min((b + 1) * B, N);
for (int i = 0; i < sz; i++) {
// moi thang nay co 1 delta
auto [from, delta] = compressed[i];
// from + x * delta >= lb
if (from < lb) {
int t = (lb - from + delta - 1) / delta;
from += t * delta;
}
if (from >= rb) continue;
for (int j = from; j < rb; j += delta) {
dp[j] += add[i][b];
}
}
};
// 1, x[i] = 3
// 1 + 1 * 1 -> 1 + 3 * 1
// 2 -> 4
auto update = [&](int index) -> void {
int id = belong[index];
auto val = dp[index];
int l = index + d[index];
int max_t = (N - 1 - index) / d[index];
int r = index + std::min(max_t, x[index]) * d[index];
if (l > r) return;
int bl = l / B;
int br = r / B;
if (bl == br) {
for (int i = l; i <= r; i += d[index]) {
dp[i] += val;
}
return;
}
for (int i = l; i < B * (bl + 1); i += d[index]) {
dp[i] += val;
}
for (int b = bl + 1; b < br; b++) {
has_update[b] = 1;
add[id][b] += val;
}
for (int i = r; i >= br * B; i -= d[index]) {
dp[i] += val;
}
};
// dbg("got here");
dp[0] = 1;
Mint res = 0;
//
// bought already we can go there if bought lmao
//
for (int i = 0; i < N; i++) {
collapse_block(i / B);
res += dp[i];
// dbg(i, dp[i]);
if (d[i] == 0) continue;
update(i);
}
std::cout << res << nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
424 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
416 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
424 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
416 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
13 ms |
4696 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
4 ms |
1628 KB |
Output is correct |
25 |
Correct |
6 ms |
2136 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
4 ms |
1112 KB |
Output is correct |
30 |
Correct |
6 ms |
4956 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
8 ms |
2396 KB |
Output is correct |
34 |
Correct |
9 ms |
4956 KB |
Output is correct |
35 |
Correct |
7 ms |
4872 KB |
Output is correct |
36 |
Correct |
7 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
472 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
10 ms |
2524 KB |
Output is correct |
6 |
Correct |
50 ms |
3528 KB |
Output is correct |
7 |
Correct |
49 ms |
3540 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
3 ms |
860 KB |
Output is correct |
12 |
Correct |
53 ms |
4056 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
860 KB |
Output is correct |
15 |
Correct |
51 ms |
3800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
2264 KB |
Output is correct |
2 |
Correct |
18 ms |
1776 KB |
Output is correct |
3 |
Correct |
39 ms |
3952 KB |
Output is correct |
4 |
Correct |
26 ms |
3036 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
51 ms |
3928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
424 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
416 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
3 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
13 ms |
4696 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
4 ms |
1628 KB |
Output is correct |
25 |
Correct |
6 ms |
2136 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
4 ms |
1112 KB |
Output is correct |
30 |
Correct |
6 ms |
4956 KB |
Output is correct |
31 |
Correct |
3 ms |
604 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
8 ms |
2396 KB |
Output is correct |
34 |
Correct |
9 ms |
4956 KB |
Output is correct |
35 |
Correct |
7 ms |
4872 KB |
Output is correct |
36 |
Correct |
7 ms |
2396 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
472 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
3 ms |
724 KB |
Output is correct |
41 |
Correct |
10 ms |
2524 KB |
Output is correct |
42 |
Correct |
50 ms |
3528 KB |
Output is correct |
43 |
Correct |
49 ms |
3540 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
3 ms |
860 KB |
Output is correct |
48 |
Correct |
53 ms |
4056 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
4 ms |
860 KB |
Output is correct |
51 |
Correct |
51 ms |
3800 KB |
Output is correct |
52 |
Correct |
18 ms |
2264 KB |
Output is correct |
53 |
Correct |
18 ms |
1776 KB |
Output is correct |
54 |
Correct |
39 ms |
3952 KB |
Output is correct |
55 |
Correct |
26 ms |
3036 KB |
Output is correct |
56 |
Correct |
1 ms |
600 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
3 ms |
860 KB |
Output is correct |
59 |
Correct |
51 ms |
3928 KB |
Output is correct |
60 |
Correct |
36 ms |
4312 KB |
Output is correct |
61 |
Correct |
39 ms |
3628 KB |
Output is correct |
62 |
Correct |
29 ms |
3288 KB |
Output is correct |
63 |
Correct |
38 ms |
3544 KB |
Output is correct |
64 |
Correct |
120 ms |
132220 KB |
Output is correct |
65 |
Correct |
350 ms |
110880 KB |
Output is correct |
66 |
Correct |
33 ms |
3796 KB |
Output is correct |
67 |
Correct |
130 ms |
131808 KB |
Output is correct |
68 |
Correct |
32 ms |
3548 KB |
Output is correct |
69 |
Correct |
161 ms |
52872 KB |
Output is correct |
70 |
Correct |
183 ms |
131312 KB |
Output is correct |
71 |
Correct |
198 ms |
131104 KB |
Output is correct |
72 |
Correct |
164 ms |
53520 KB |
Output is correct |