xupy21 commited on
Commit
aa69f0b
·
verified ·
1 Parent(s): c3dba47

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. problems/DP-04/Atcoder/abc176_f/editorial.md +27 -0
  2. problems/DP-04/Atcoder/abc176_f/judge.json +15 -0
  3. problems/DP-04/Atcoder/abc176_f/meta.json +16 -0
  4. problems/DP-04/Atcoder/abc176_f/solution.cpp +87 -0
  5. problems/DP-04/Atcoder/abc176_f/statement.md +69 -0
  6. problems/DP-04/Atcoder/abc179_d/editorial.md +15 -0
  7. problems/DP-04/Atcoder/abc179_d/judge.json +15 -0
  8. problems/DP-04/Atcoder/abc179_d/meta.json +16 -0
  9. problems/DP-04/Atcoder/abc179_d/solution.cpp +34 -0
  10. problems/DP-04/Atcoder/abc179_d/statement.md +85 -0
  11. problems/DP-04/Atcoder/abc182_f/editorial.md +20 -0
  12. problems/DP-04/Atcoder/abc182_f/judge.json +15 -0
  13. problems/DP-04/Atcoder/abc182_f/meta.json +16 -0
  14. problems/DP-04/Atcoder/abc182_f/solution.cpp +42 -0
  15. problems/DP-04/Atcoder/abc182_f/statement.md +96 -0
  16. problems/DP-04/Atcoder/abc188_f/editorial.md +36 -0
  17. problems/DP-04/Atcoder/abc188_f/judge.json +15 -0
  18. problems/DP-04/Atcoder/abc188_f/meta.json +16 -0
  19. problems/DP-04/Atcoder/abc188_f/solution.cpp +37 -0
  20. problems/DP-04/Atcoder/abc188_f/statement.md +73 -0
  21. problems/DP-04/Atcoder/abc199_f/editorial.md +15 -0
  22. problems/DP-04/Atcoder/abc199_f/judge.json +15 -0
  23. problems/DP-04/Atcoder/abc199_f/meta.json +16 -0
  24. problems/DP-04/Atcoder/abc199_f/solution.cpp +73 -0
  25. problems/DP-04/Atcoder/abc199_f/statement.md +103 -0
  26. problems/DP-04/Atcoder/abc200_e/editorial.md +16 -0
  27. problems/DP-04/Atcoder/abc200_e/judge.json +15 -0
  28. problems/DP-04/Atcoder/abc200_e/meta.json +16 -0
  29. problems/DP-04/Atcoder/abc200_e/solution.cpp +40 -0
  30. problems/DP-04/Atcoder/abc200_e/statement.md +66 -0
  31. problems/DP-04/Atcoder/abc201_f/editorial.md +32 -0
  32. problems/DP-04/Atcoder/abc201_f/judge.json +15 -0
  33. problems/DP-04/Atcoder/abc201_f/meta.json +16 -0
  34. problems/DP-04/Atcoder/abc201_f/solution.cpp +42 -0
  35. problems/DP-04/Atcoder/abc201_f/statement.md +119 -0
  36. problems/DP-04/Atcoder/abc203_e/editorial.md +31 -0
  37. problems/DP-04/Atcoder/abc203_e/judge.json +15 -0
  38. problems/DP-04/Atcoder/abc203_e/meta.json +16 -0
  39. problems/DP-04/Atcoder/abc203_e/solution.cpp +42 -0
  40. problems/DP-04/Atcoder/abc203_e/statement.md +72 -0
  41. problems/DP-04/Atcoder/abc204_f/data/1.in +1 -0
  42. problems/DP-04/Atcoder/abc204_f/data/1.out +1 -0
  43. problems/DP-04/Atcoder/abc204_f/data/10.in +1 -0
  44. problems/DP-04/Atcoder/abc204_f/data/10.out +1 -0
  45. problems/DP-04/Atcoder/abc204_f/data/11.in +1 -0
  46. problems/DP-04/Atcoder/abc204_f/data/11.out +1 -0
  47. problems/DP-04/Atcoder/abc204_f/data/12.in +1 -0
  48. problems/DP-04/Atcoder/abc204_f/data/12.out +1 -0
  49. problems/DP-04/Atcoder/abc204_f/data/13.in +1 -0
  50. problems/DP-04/Atcoder/abc204_f/data/13.out +1 -0
problems/DP-04/Atcoder/abc176_f/editorial.md ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Brave CHAIN (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc176/editorial/81 by evima
4
+
5
+ (Editorial: ynymxiaolongbao)
6
+
7
+ Among the five cards that is arranged in the \(i\)-th operation, the numbers on the rightmost \(3\) cards are \(A[3i]\), \(A[3i+1]\) and \(A[3i+2]\), which is independent of the operations before the \(i\)-th one. On the other hand, the pair of numbers on the leftmost \(2\) cards is dependent on the operations before the \(i\)-th one.
8
+
9
+ We use Dynamic Programming (DP).
10
+
11
+ Let \(DP[i][x][y]\) be the maximum score after the \(i\)-th operation where the leftmost number is \(x\) and the second leftmost number is \(y\).
12
+
13
+ We assume that, if the third, fourth and fifth number from the left are the same, we always remove those three cards. One can prove by contradiction that this does not lose the optimality. In such case, we can add \(1\) to the answer and regard those \(3\) card as being disappeared.
14
+
15
+ Among the third, fourth and fifth numbers, if two of them are the same, let \(p\) be the common number, \(q\) the other, and \(k\) be any integer between \(1\) and \(N\) (inclusive), then maximize \(DP[i+1][k][q]\) by \(DP[i][p][k]+1\) or \(DP[i][k][p]+1\) and so on. The time complexity is \(O(N)\).
16
+
17
+ Among the third, fourth and fifth numbers, let \(p\) be one of them and \(q\) and \(r\) be the other, then maximize \(DP[i+1][q][r]\) by \(DP[i][p][p] + 1\). The time complexity is \(O(1)\).
18
+
19
+ Let \(p\), \(q\) and \(r\) be the third, fourth and fifth numbers respectively, and \(k\) and \(l\) be any integer between \(1\) and \(N\) (inclusive), then maximize \(DP[i+1][p][q]\), \(DP[i+1][p][r]\) or \(DP[i+1][q][r]\) and others with the maximum value of \(DP[i][k][l]\). This can be processed in an \(O(1)\) time if the maximum value of \(DP[k][l]\) is managed.
20
+
21
+ Among the third, fourth and fifth numbers, let \(p\) be one of them, and \(k\) and \(l\) be any integer between \(1\) and \(N\) (inclusive), then maximize \(DP[i+1][k][p]\) with the maximum value of \(DP[i][k][l]\) (where only \(l\) is a variable), and maximize \(DP[i+1][l][p]\) by the maximum value of \(DP[i][k][l]\) (where only \(k\) is a variable). If the values above are managed, they can be processed in \(O(N)\) time.
22
+
23
+ Let \(k\) and \(l\) be any integer between \(1\) and \(N\) (inclusive), then maximize \(DP[i+1][k][l]\) by \(DP[i][k][l]\). However, if you reuse the array and use \(DP[i][k][l]\) as the initial value of \(DP[i+1][k][l]\), you do not need to do this process.
24
+
25
+ The maximum value can be processed in an \(O(1)\) time each, since the value of DP never decreases.
26
+
27
+ The time complexity for each \(i\) is \(O(N)\), so the overall time complexity is \(O(N^2)\).
problems/DP-04/Atcoder/abc176_f/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 55,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc176_f/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc176_f",
3
+ "contest": "abc176",
4
+ "type": "ABC",
5
+ "letter": "F",
6
+ "title": "Brave CHAIN",
7
+ "url": "https://atcoder.jp/contests/abc176/tasks/abc176_f",
8
+ "difficulty": 2912,
9
+ "score": 600,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 55,
13
+ "editorial_url": "https://atcoder.jp/contests/abc176/editorial/81",
14
+ "code_source": "model",
15
+ "pid": "A1434"
16
+ }
problems/DP-04/Atcoder/abc176_f/solution.cpp ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc176_f: F - Brave CHAIN
2
+ // Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc176/editorial/81
3
+ // Judged AC on all 55 tests, max 0.20 s of the 2 s limit (submission 1).
4
+
5
+ #include <bits/stdc++.h>
6
+ using namespace std;
7
+
8
+ struct Update {
9
+ int x, y, value;
10
+ };
11
+
12
+ int main() {
13
+ ios::sync_with_stdio(false);
14
+ cin.tie(nullptr);
15
+
16
+ int N;
17
+ cin >> N;
18
+ vector<int> A(3 * N);
19
+ for (int &x : A) cin >> x;
20
+
21
+ const int S = N + 1;
22
+ vector<int> dp(S * S, -1);
23
+ vector<int> row_max(S, -1);
24
+ int best = 0;
25
+ int bonus = 0;
26
+
27
+ auto relax = [&](int x, int y, int value) {
28
+ if (value <= dp[x * S + y]) return;
29
+ dp[x * S + y] = value;
30
+ dp[y * S + x] = value;
31
+ row_max[x] = max(row_max[x], value);
32
+ row_max[y] = max(row_max[y], value);
33
+ best = max(best, value);
34
+ };
35
+ relax(A[0], A[1], 0);
36
+
37
+ vector<Update> updates;
38
+ updates.reserve(4 * N + 3);
39
+
40
+ for (int round = 0; round < N - 1; ++round) {
41
+ int pos = 2 + 3 * round;
42
+ int x = A[pos], y = A[pos + 1], z = A[pos + 2];
43
+ if (x == y && y == z) {
44
+ ++bonus;
45
+ continue;
46
+ }
47
+
48
+ int repeated = -1, other = -1;
49
+ if (x == y) {
50
+ repeated = x;
51
+ other = z;
52
+ } else if (x == z) {
53
+ repeated = x;
54
+ other = y;
55
+ } else if (y == z) {
56
+ repeated = y;
57
+ other = x;
58
+ }
59
+
60
+ // Collect every transition before modifying the previous DP layer.
61
+ updates.clear();
62
+ for (int k = 1; k <= N; ++k) {
63
+ if (row_max[k] >= 0) {
64
+ updates.push_back({k, x, row_max[k]});
65
+ updates.push_back({k, y, row_max[k]});
66
+ updates.push_back({k, z, row_max[k]});
67
+ }
68
+ if (repeated != -1) {
69
+ int value = dp[k * S + repeated];
70
+ if (value >= 0) {
71
+ updates.push_back({k, other, value + 1});
72
+ }
73
+ }
74
+ }
75
+
76
+ updates.push_back({x, y, max(best, dp[z * S + z] + 1)});
77
+ updates.push_back({x, z, max(best, dp[y * S + y] + 1)});
78
+ updates.push_back({y, z, max(best, dp[x * S + x] + 1)});
79
+
80
+ // Retaining the old pair earns zero, so old entries remain valid.
81
+ for (const auto &u : updates) relax(u.x, u.y, u.value);
82
+ }
83
+
84
+ int last = A.back();
85
+ cout << bonus + max(best, dp[last * S + last] + 1) << '\n';
86
+ return 0;
87
+ }
problems/DP-04/Atcoder/abc176_f/statement.md ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Brave CHAIN
2
+
3
+ **Score :** 600 points
4
+
5
+ ### Problem Statement
6
+ We have `3N` cards arranged in a row from left to right, where each card has an integer between `1` and `N` (inclusive) written on it. The integer written on the `i`-th card from the left is `A_i`.
7
+
8
+ You will do the following operation `N-1` times:
9
+ - Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain `1` point.
10
+
11
+ After these `N-1` operations, if the integers written on the remaining three cards are all equal, you will gain `1` additional point.
12
+
13
+ **Find the maximum number of points you can gain.**
14
+
15
+ ### Constraints
16
+ - `1 ≤ N ≤ 2000`
17
+ - `1 ≤ A_i ≤ N`
18
+
19
+ ---
20
+
21
+ ### Input
22
+ Input is given from Standard Input in the following format:
23
+ ```
24
+ N
25
+ A_1 A_2 ... A_{3N}
26
+ ```
27
+
28
+ ### Output
29
+ Print the maximum number of points you can gain.
30
+
31
+ ---
32
+
33
+ ### Sample Input 1
34
+ ```
35
+ 2
36
+ 1 2 1 2 2 1
37
+ ```
38
+
39
+ ### Sample Output 1
40
+ ```
41
+ 2
42
+ ```
43
+ Let us rearrange the five leftmost cards so that the integers written on the six cards will be `2 2 2 1 1 1` from left to right. Then, remove the three leftmost cards, all of which have the same integer `2`, gaining `1` point. Now, the integers written on the remaining cards are `1 1 1`. Since these three cards have the same integer `1`, we gain `1` more point. In this way, we can gain `2` points - which is the maximum possible.
44
+
45
+ ---
46
+
47
+ ### Sample Input 2
48
+ ```
49
+ 3
50
+ 1 1 2 2 3 3 3 2 1
51
+ ```
52
+
53
+ ### Sample Output 2
54
+ ```
55
+ 1
56
+ ```
57
+
58
+ ---
59
+
60
+ ### Sample Input 3
61
+ ```
62
+ 3
63
+ 1 1 2 2 2 3 3 3 1
64
+ ```
65
+
66
+ ### Sample Output 3
67
+ ```
68
+ 3
69
+ ```
problems/DP-04/Atcoder/abc179_d/editorial.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # D - Leaping Tak (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc179/editorial/133 by evima
4
+
5
+ First, the naivest solution is a Dynamic Programming (DP) where \(f_i\) := the number of ways to go to Cell \(i\), and check all the possible move for each \(i\), but this needs a time complexity of \(\mathrm{O}(N^2)\).
6
+
7
+ Let us consider optimizing by making use of the fact that the ways of moving can be written as a sum of small number of segments (\(S = \cup [l_j, r_j]\)). This can be solved either in so-called “distributing DP” or “receiving DP.” This time, we will explain based on distributing DP.
8
+
9
+ Assume that we have already obtained the values until \(f_i\). When transiting from \(i\), for all \(d \in S\), we want to add \(f_i\) to \(f_{i+d}\).
10
+
11
+ Here, we want to use the fact that the difference of neighboring values of \(f\) changes at \(O(K)\) positions. Let \(a_i = f_{i} - f_{i-1}\), then this can be optimized by performing the operations of \(a_{i + l_j} := a_{i + l_j} + f_i, a_{i + r_j + 1} := a_{i + r_j + 1} - f_i\) for each \(j\), and then restore \(f\) by \(f_i = f_{i-1} + a_i\).
12
+
13
+ The total time complexity is \(\mathrm{O}(KN)\).
14
+
15
+ Note that it is known that this problem can be solved in a total of \(\mathrm{O}(N \log N)\) time even when the transitions cannot be written as a sum of small number of segments.
problems/DP-04/Atcoder/abc179_d/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 28,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc179_d/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc179_d",
3
+ "contest": "abc179",
4
+ "type": "ABC",
5
+ "letter": "D",
6
+ "title": "Leaping Tak",
7
+ "url": "https://atcoder.jp/contests/abc179/tasks/abc179_d",
8
+ "difficulty": 1251,
9
+ "score": 400,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 28,
13
+ "editorial_url": "https://atcoder.jp/contests/abc179/editorial/133",
14
+ "code_source": "model",
15
+ "pid": "A1450"
16
+ }
problems/DP-04/Atcoder/abc179_d/solution.cpp ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc179_d: D - Leaping Tak
2
+ // Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc179/editorial/133
3
+ // Judged AC on all 28 tests, max 0.01 s of the 2 s limit (submission 1).
4
+
5
+ #include <bits/stdc++.h>
6
+ #include <atcoder/modint>
7
+ using namespace std;
8
+ using mint = atcoder::modint998244353;
9
+
10
+ int main() {
11
+ ios::sync_with_stdio(false);
12
+ cin.tie(nullptr);
13
+
14
+ int N, K;
15
+ cin >> N >> K;
16
+ vector<pair<int, int>> segments(K);
17
+ for (auto &[l, r] : segments) cin >> l >> r;
18
+
19
+ vector<mint> diff(N + 2);
20
+ diff[1] = 1;
21
+ diff[2] = -1;
22
+ mint current = 0;
23
+
24
+ for (int i = 1; i <= N; ++i) {
25
+ current += diff[i];
26
+ for (auto [l, r] : segments) {
27
+ if (i + l <= N) diff[i + l] += current;
28
+ if (i + r + 1 <= N) diff[i + r + 1] -= current;
29
+ }
30
+ }
31
+
32
+ cout << current.val() << '\n';
33
+ return 0;
34
+ }
problems/DP-04/Atcoder/abc179_d/statement.md ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # D - Leaping Tak
2
+
3
+ **Score:** 400 points
4
+
5
+ ## Problem Statement
6
+ There are \( N \) cells arranged in a row, numbered \( 1, 2, \ldots, N \) from left to right. Tak lives in these cells and is currently on Cell \( 1 \). He is trying to reach Cell \( N \) by using the procedure described below. You are given an integer \( K \) that is less than or equal to \( 10 \), and \( K \) non-intersecting segments \( [L_1, R_1], [L_2, R_2], \ldots, [L_K, R_K] \). Let \( S \) be the union of these \( K \) segments. Here, the segment \( [l, r] \) denotes the set consisting of all integers \( i \) that satisfy \( l \leq i \leq r \).
7
+
8
+ - When you are on Cell \( i \), pick an integer \( d \) from \( S \) and move to Cell \( i + d \). You cannot move out of the cells.
9
+
10
+ To help Tak, find the number of ways to go to Cell \( N \), modulo \( 998244353 \).
11
+
12
+ ## Constraints
13
+ - \( 2 \leq N \leq 2 \times 10^5 \)
14
+ - \( 1 \leq K \leq \min(N, 10) \)
15
+ - \( 1 \leq L_i \leq R_i \leq N \)
16
+ - \( [L_i, R_i] \) and \( [L_j, R_j] \) do not intersect (\( i \neq j \))
17
+ - All values in input are integers.
18
+
19
+ ## Input
20
+ Input is given from Standard Input in the following format:
21
+ ```
22
+ N K
23
+ L_1 R_1
24
+ L_2 R_2
25
+ :
26
+ L_K R_K
27
+ ```
28
+
29
+ ## Output
30
+ Print the number of ways for Tak to go from Cell \( 1 \) to Cell \( N \), modulo \( 998244353 \).
31
+
32
+ ## Sample Input 1
33
+ ```
34
+ 5 2
35
+ 1 1
36
+ 3 4
37
+ ```
38
+
39
+ ## Sample Output 1
40
+ ```
41
+ 4
42
+ ```
43
+ The set \( S \) is the union of the segment \( [1, 1] \) and the segment \( [3, 4] \), therefore \( S = \{ 1, 3, 4 \} \) holds. There are \( 4 \) possible ways to get to Cell \( 5 \):
44
+ - \( 1 \to 2 \to 3 \to 4 \to 5 \),
45
+ - \( 1 \to 2 \to 5 \),
46
+ - \( 1 \to 4 \to 5 \) and
47
+ - \( 1 \to 5 \).
48
+
49
+ ## Sample Input 2
50
+ ```
51
+ 5 2
52
+ 3 3
53
+ 5 5
54
+ ```
55
+
56
+ ## Sample Output 2
57
+ ```
58
+ 0
59
+ ```
60
+ Because \( S = \{ 3, 5 \} \) holds, you cannot reach to Cell \( 5 \). Print \( 0 \).
61
+
62
+ ## Sample Input 3
63
+ ```
64
+ 5 1
65
+ 1 2
66
+ ```
67
+
68
+ ## Sample Output 3
69
+ ```
70
+ 5
71
+ ```
72
+
73
+ ## Sample Input 4
74
+ ```
75
+ 60 3
76
+ 5 8
77
+ 1 3
78
+ 10 15
79
+ ```
80
+
81
+ ## Sample Output 4
82
+ ```
83
+ 221823067
84
+ ```
85
+ Note that you have to print the answer modulo \( 998244353 \).
problems/DP-04/Atcoder/abc182_f/editorial.md ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Valid payments (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc182/editorial/327 by evima
4
+
5
+ Regarding the change as paying negative number of coins, the problem statement can be rephrased as follows:
6
+
7
+ - Print the number of integer sequences \(x_1, x_2, x_3, \dots, x_N\) such that:
8
+
9
+ - \(-\frac{A_{i + 1}}{A_i} \lt x_i \lt \frac{A_{i + 1}}{A_i} (1 \le i \lt N)\)
10
+ - \(\sum_{1 \le i \le N} A_ix_i = X\)
11
+
12
+ If we fix some \(y\), one can uniquely determine the combinations of coins that Lunlun will pay and that clerk returns, thus \(x\) is uniquely determined; conversely, once \(x\) is fixed, \(y\) is also determined, so the paraphrase is valid.
13
+
14
+ Consider deciding \(x\) one by one in the decreasing order of \(x\). Assume that we have determined \(x_i\). Let \(d = (\sum_{j \ge i} A_jx_j) - X\). (That is, \(d\) is the difference between the balance so far and the target balance \(X\).) Since \(|\sum_{j \lt i} A_jx_j| \lt A_i\), it is necessary that \(|d| \lt A_i\) in order to make \(d = 0\) ultimately. Since \(\sum_{j \ge i} A_jx_j\) is a multiple of \(A_i\), there are at most two integers for \(d\) such that \(|d| \lt A_i\).
15
+
16
+ With this observation, the following DP is available. dp[\(i\)][\(j\)] : The number of possible combinations of \((x_i, \ldots, x_n)\) such that \(d = j\)
17
+
18
+ Here, \(j\) can be a large number like \(10^{15}\), but note that for each \(i\) there are at most two \(j\)’s. Therefore, it is good idea to use associated arrays for the second dimension. Transition from dp[\(i\)][\(j\)] depends on the choice of \(x_i\), which is done by considering if it is possible to transition to each of at most two \(j\)’s corresponding to \(i+1\).
19
+
20
+ Therefore, each transition is done in \(O(1)\) time, and the entire problem can be solved in a total of \(O(N)\) time.
problems/DP-04/Atcoder/abc182_f/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 35,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc182_f/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc182_f",
3
+ "contest": "abc182",
4
+ "type": "ABC",
5
+ "letter": "F",
6
+ "title": "Valid payments",
7
+ "url": "https://atcoder.jp/contests/abc182/tasks/abc182_f",
8
+ "difficulty": 2121,
9
+ "score": 600,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 35,
13
+ "editorial_url": "https://atcoder.jp/contests/abc182/editorial/327",
14
+ "code_source": "model",
15
+ "pid": "A1470"
16
+ }
problems/DP-04/Atcoder/abc182_f/solution.cpp ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc182_f: F - Valid payments
2
+ // Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc182/editorial/327
3
+ // Judged AC on all 35 tests, max 0.00 s of the 2 s limit (submission 1).
4
+
5
+ #include <bits/stdc++.h>
6
+ using namespace std;
7
+
8
+ int main() {
9
+ ios::sync_with_stdio(false);
10
+ cin.tie(nullptr);
11
+
12
+ int N;
13
+ long long X;
14
+ cin >> N >> X;
15
+ vector<long long> A(N);
16
+ for (auto &a : A) cin >> a;
17
+
18
+ // States are (balance minus X, number of choices for processed coins).
19
+ long long rem = X % A.back();
20
+ vector<pair<long long, long long>> dp = {{-rem, 1}};
21
+ if (rem != 0) dp.push_back({A.back() - rem, 1});
22
+
23
+ for (int i = N - 2; i >= 0; --i) {
24
+ rem = X % A[i];
25
+ vector<pair<long long, long long>> next = {{-rem, 0}};
26
+ if (rem != 0) next.push_back({A[i] - rem, 0});
27
+
28
+ long long ratio = A[i + 1] / A[i];
29
+ for (auto &[new_difference, count] : next) {
30
+ for (auto [difference, ways] : dp) {
31
+ long long coins = (new_difference - difference) / A[i];
32
+ if (-ratio < coins && coins < ratio) {
33
+ count += ways;
34
+ }
35
+ }
36
+ }
37
+ dp = move(next);
38
+ }
39
+
40
+ cout << dp[0].second << '\n';
41
+ return 0;
42
+ }
problems/DP-04/Atcoder/abc182_f/statement.md ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Valid payments
2
+
3
+ **Score :** 600 points
4
+
5
+ ---
6
+
7
+ ## Problem Statement
8
+
9
+ In Republic of AtCoder, N kinds of coins are used: A₁-yen, A₂-yen, A₃-yen, ..., Aₙ-yen coins.
10
+ Here, A₁ = 1 holds, and for each i such that 1 ≤ i < N, Aᵢ < Aᵢ₊₁ holds and Aᵢ₊₁ is a multiple of Aᵢ.
11
+
12
+ At some shop in this country, Lunlun the dog bought an X-yen product by giving y (≥ X) yen to the clerk and receiving the change of y - X yen from the clerk. (The change may have been 0 yen.)
13
+ Here, both Lunlun and the clerk used the minimum number of coins needed to represent those amounts of money.
14
+ Additionally, the clerk did not return any kind of coins that Lunlun gave him.
15
+
16
+ Given X, find the number of possible values for y.
17
+
18
+ ---
19
+
20
+ ### Constraints
21
+ - 1 ≤ N ≤ 50
22
+ - 1 = A₁ < A₂ < A₃ < ... < Aₙ ≤ 10¹⁵
23
+ - Aᵢ₊₁ is a multiple of Aᵢ. (1 ≤ i < N)
24
+ - 1 ≤ X ≤ 10¹⁵
25
+ - All values in input are integers.
26
+
27
+ ---
28
+
29
+ ### Input
30
+ Input is given from Standard Input in the following format:
31
+ ```
32
+ N X
33
+ A₁ A₂ A₃ ... Aₙ
34
+ ```
35
+
36
+ ---
37
+
38
+ ### Output
39
+ Print the number of possible values for y.
40
+
41
+ ---
42
+
43
+ ### Sample Input 1
44
+ ```
45
+ 3 9
46
+ 1 5 10
47
+ ```
48
+
49
+ ### Sample Output 1
50
+ ```
51
+ 3
52
+ ```
53
+ y can be 9, 10, or 14.
54
+ For example, when y = 14, Lunlun gives one 10-yen coin and four 1-yen coins, and the clerk returns one 5-yen coin.
55
+ Here, the clerk is not returning any kind of coins that Lunlun gives him, satisfying the requirement.
56
+
57
+ ---
58
+
59
+ ### Sample Input 2
60
+ ```
61
+ 5 198
62
+ 1 5 10 50 100
63
+ ```
64
+
65
+ ### Sample Output 2
66
+ ```
67
+ 5
68
+ ```
69
+ y can be 198, 200, 203, 208, or 248.
70
+
71
+ ---
72
+
73
+ ### Sample Input 3
74
+ ```
75
+ 4 44
76
+ 1 4 20 100
77
+ ```
78
+
79
+ ### Sample Output 3
80
+ ```
81
+ 4
82
+ ```
83
+ y can be 44, 60, 100, or 104.
84
+
85
+ ---
86
+
87
+ ### Sample Input 4
88
+ ```
89
+ 9 11837029798
90
+ 1 942454037 2827362111 19791534777 257289952101 771869856303 3859349281515 30874794252120 216123559764840
91
+ ```
92
+
93
+ ### Sample Output 4
94
+ ```
95
+ 21
96
+ ```
problems/DP-04/Atcoder/abc188_f/editorial.md ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - +1-1x2 (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc188/editorial/554 by en_translator
4
+
5
+ Considering the opposite operations, we have the following equivalent problem:
6
+
7
+ Find the minimum number of operations of the following three types to change \(Y\) to \(X\):
8
+
9
+ - Type \(1\) : Add \(1\) to \(Y\)
10
+ - Type \(2\) : Subtract \(1\) from \(Y\)
11
+ - Type \(3\) : Divide \(Y\) by \(2\) (only if \(Y\) is divisible by \(2\))
12
+
13
+ Here, it is obvious that Type \(1\) and Type \(2\) does not appear consecutively.
14
+ Also, instead of adding \(1\) \(k (k \ge 2)\) times and then dividing by \(2\), we can add \(1\) \(k-2\) times, divide by \(2\), and then add \(1\) once, so that the number of operations is fewer (and the same applies for subtraction). Thus, we can see that we don’t have to perform Type \(1\) or Type \(2\) consecutively, except for after the last operation of Type \(3\).
15
+ Conversely, if \(Y = y\) after the last operation of Type \(3\), the number of remaining operations is determined to \(|X - y\).
16
+
17
+ With the observations above, consider the following recursive function:
18
+
19
+ \(\mathrm{solve}(y)\) : Answer for \(Y = y\)
20
+ \(\mathrm{solve}(y) = \)
21
+
22
+ - If \(y = 1\) : \(|X - y|\)
23
+ - If \(y\) is odd but not \(1\) : \(\min(|X - y|, \mathrm{solve}(\frac{y + 1}{2}) + 2, \mathrm{solve}(\frac{y - 1}{2}) + 2)\)
24
+ - If \(y\) is even : \(\min(|X - y|, \mathrm{solve}(\frac{y}{2}) + 1)\)
25
+
26
+ Memorize this by \(y\) and call \(\mathrm{solve}(Y)\), and we obtain the answer.
27
+
28
+ Now let’s consider the time complexity.
29
+ Consider the number of \(y\) in \(\mathrm{solve}(y)\) that originates from \(\mathrm{solve}(Y)\).
30
+ If we regard the execution of \(\mathrm{solve}(Y)\) as Level-\(0\) recursion, then \(y\) in Level-\(d\) recursion satisfies:
31
+
32
+ - \(y \ge \frac{\frac{\frac{Y - 1}{2} - 1}{2} - 1}{2}_\ddots\) (\(d\) fractions) \( = \frac{Y}{2^d} - \sum_{i = 1}^d \frac{1}{2^d} \gt \frac{Y}{2^d} - 1\) (If the recursion is repeated with \(\mathrm{solve}(\frac{y - 1}{2})\))
33
+ - \(y \le \frac{\frac{\frac{Y + 1}{2} + 1}{2} + 1}{2}_\ddots\) (\(d\) fractions) \( = \frac{Y}{2^d} + \sum_{i = 1}^d \frac{1}{2^d} \lt \frac{Y}{2^d} + 1\) (If the recursion is repeated with \(\mathrm{solve}(\frac{y + 1}{2})\))
34
+
35
+ Therefore, for each level, at most \(2\) (constant) kinds of \(y\) is called.
36
+ Since there are no more than \(O(\log(Y))\) levels, the total time complexity is \(O(\log(Y))\) if we use an \(O(1)\) hash map.
problems/DP-04/Atcoder/abc188_f/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 50,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc188_f/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc188_f",
3
+ "contest": "abc188",
4
+ "type": "ABC",
5
+ "letter": "F",
6
+ "title": "+1-1x2",
7
+ "url": "https://atcoder.jp/contests/abc188/tasks/abc188_f",
8
+ "difficulty": 1865,
9
+ "score": 600,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 50,
13
+ "editorial_url": "https://atcoder.jp/contests/abc188/editorial/554",
14
+ "code_source": "model",
15
+ "pid": "A1506"
16
+ }
problems/DP-04/Atcoder/abc188_f/solution.cpp ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc188_f: F - +1-1x2
2
+ // Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc188/editorial/554
3
+ // Judged AC on all 50 tests, max 0.00 s of the 2 s limit (submission 1).
4
+
5
+ #include <bits/stdc++.h>
6
+ using namespace std;
7
+
8
+ using int64 = long long;
9
+ int64 X;
10
+ unordered_map<int64, int64> memo;
11
+
12
+ int64 solve(int64 y) {
13
+ if (y == 1) return X - 1;
14
+ auto it = memo.find(y);
15
+ if (it != memo.end()) return it->second;
16
+
17
+ int64 answer = llabs(X - y);
18
+ if (y % 2 == 0) {
19
+ answer = min(answer, solve(y / 2) + 1);
20
+ } else {
21
+ answer = min(answer, solve((y - 1) / 2) + 2);
22
+ answer = min(answer, solve((y + 1) / 2) + 2);
23
+ }
24
+ memo[y] = answer;
25
+ return answer;
26
+ }
27
+
28
+ int main() {
29
+ ios::sync_with_stdio(false);
30
+ cin.tie(nullptr);
31
+
32
+ int64 Y;
33
+ cin >> X >> Y;
34
+ memo.reserve(256);
35
+ cout << solve(Y) << '\n';
36
+ return 0;
37
+ }
problems/DP-04/Atcoder/abc188_f/statement.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - +1-1x2
2
+
3
+ **Score**: `600` points
4
+
5
+ ## Problem Statement
6
+ Takahashi has written an integer `X` on a blackboard.
7
+ He can do the following three kinds of operations any number of times in any order:
8
+ - increase the value written on the blackboard by `1`;
9
+ - decrease the value written on the blackboard by `1`;
10
+ - multiply the value written on the blackboard by `2`.
11
+
12
+ Find the minimum number of operations required to have `Y` written on the blackboard.
13
+
14
+ ### Constraints
15
+ - `1 ≤ X ≤ 10^18`
16
+ - `1 ≤ Y ≤ 10^18`
17
+ - `X` and `Y` are integers.
18
+
19
+ ---
20
+
21
+ ### Input
22
+ Input is given from Standard Input in the following format:
23
+ ```
24
+ X Y
25
+ ```
26
+
27
+ ### Output
28
+ Print the answer.
29
+
30
+ ---
31
+
32
+ ### Sample Input 1
33
+ ```
34
+ 3 9
35
+ ```
36
+
37
+ ### Sample Output 1
38
+ ```
39
+ 3
40
+ ```
41
+ Initially, `3` is written on the blackboard. The following three operations can make it `9`:
42
+ - increase the value by `1`, which results in `4`;
43
+ - multiply the value by `2`, which results in `8`;
44
+ - increase the value by `1`, which results in `9`.
45
+
46
+ ---
47
+
48
+ ### Sample Input 2
49
+ ```
50
+ 7 11
51
+ ```
52
+
53
+ ### Sample Output 2
54
+ ```
55
+ 3
56
+ ```
57
+ The following procedure can make the value on the blackboard `11`:
58
+ - decrease the value by `1`, which results in `6`;
59
+ - multiply the value by `2`, which results in `12`;
60
+ - decrease the value by `1`, which results in `11`.
61
+
62
+ ---
63
+
64
+ ### Sample Input 3
65
+ ```
66
+ 1000000000000000000 1000000000000000000
67
+ ```
68
+
69
+ ### Sample Output 3
70
+ ```
71
+ 0
72
+ ```
73
+ If the value initially written on the blackboard equals `Y`, print `0`.
problems/DP-04/Atcoder/abc199_f/editorial.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Graph Smoothing (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc199/editorial/1171 by en_translator
4
+
5
+ Let \(y_i\) be the expected value of the number written on vertex \(i\) after a single operation, before which each vertex \(i\) has \(x_i\) written on it.
6
+
7
+ Then, there exist an matrix \(m\) such that \(xm = y\), or equivalently \(\displaystyle y_i = \sum_{j = 1}^N x_j m_{j, i}\).
8
+ Specifically, \(m\) can be constructed as follows.
9
+
10
+ - Initially each element of \(m\) is \(0\)
11
+ - Consider each vertex \(i\). Let \(d_i\) be the degree of vertex \(i\) and \(a_{i, 1}, a_{i, 2}, \dots, a_{i, d_i}\) be the adjacent vertices.
12
+ - At the probability of \(\frac{d_i}{M}\), an edge adjacent to vertex \(i\) is chosen. In such case the contribution of \(x_i\) to \(y_i\) decreases from \(1\) to \(\frac{1}{2}\), so \(m_{i, i} = 1 - \frac{d_i}{M} \times \frac{1}{2}\).
13
+ - Also, when an edge connecting \(a_{i, j}\) and \(i\) is chosen (at the probability of \(\frac{1}{M}\), \(x_{a_{i, j}}\) contribute to \(y_i\) at the ratio of \(\frac{1}{2}\), so we let \(m_{a_{i, j}, i} = \frac{1}{2M}\).
14
+
15
+ Since the answer for the problem is \(Am^K\), we can calculate \(m^K\) with fast exponentiation so that the problem is solved in a total of \(O(N^3 \log(K))\) time.
problems/DP-04/Atcoder/abc199_f/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 29,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc199_f/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc199_f",
3
+ "contest": "abc199",
4
+ "type": "ABC",
5
+ "letter": "F",
6
+ "title": "Graph Smoothing",
7
+ "url": "https://atcoder.jp/contests/abc199/tasks/abc199_f",
8
+ "difficulty": 2065,
9
+ "score": 600,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 29,
13
+ "editorial_url": "https://atcoder.jp/contests/abc199/editorial/1171",
14
+ "code_source": "model",
15
+ "pid": "A1572"
16
+ }
problems/DP-04/Atcoder/abc199_f/solution.cpp ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc199_f: F - Graph Smoothing
2
+ // Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc199/editorial/1171
3
+ // Judged AC on all 29 tests, max 0.04 s of the 2 s limit (submission 1).
4
+
5
+ #include <bits/stdc++.h>
6
+ #include <atcoder/modint>
7
+ using namespace std;
8
+ using mint = atcoder::modint1000000007;
9
+ using Matrix = vector<vector<mint>>;
10
+
11
+ Matrix multiply(const Matrix& a, const Matrix& b) {
12
+ int n = (int)a.size();
13
+ Matrix c(n, vector<mint>(n));
14
+ for (int i = 0; i < n; ++i) {
15
+ for (int k = 0; k < n; ++k) {
16
+ mint v = a[i][k];
17
+ if (v.val() == 0) continue;
18
+ for (int j = 0; j < n; ++j) {
19
+ c[i][j] += v * b[k][j];
20
+ }
21
+ }
22
+ }
23
+ return c;
24
+ }
25
+
26
+ vector<mint> multiply(const vector<mint>& a, const Matrix& b) {
27
+ int n = (int)a.size();
28
+ vector<mint> c(n);
29
+ for (int i = 0; i < n; ++i) {
30
+ for (int j = 0; j < n; ++j) {
31
+ c[j] += a[i] * b[i][j];
32
+ }
33
+ }
34
+ return c;
35
+ }
36
+
37
+ int main() {
38
+ ios::sync_with_stdio(false);
39
+ cin.tie(nullptr);
40
+
41
+ int n, m;
42
+ long long k;
43
+ cin >> n >> m >> k;
44
+ vector<mint> a(n);
45
+ for (int i = 0; i < n; ++i) {
46
+ int x;
47
+ cin >> x;
48
+ a[i] = x;
49
+ }
50
+
51
+ Matrix transition(n, vector<mint>(n));
52
+ for (int i = 0; i < n; ++i) transition[i][i] = 1;
53
+ mint probability = mint(2 * m).inv();
54
+ for (int i = 0; i < m; ++i) {
55
+ int x, y;
56
+ cin >> x >> y;
57
+ --x;
58
+ --y;
59
+ transition[x][x] -= probability;
60
+ transition[y][y] -= probability;
61
+ transition[x][y] += probability;
62
+ transition[y][x] += probability;
63
+ }
64
+
65
+ while (k > 0) {
66
+ if (k & 1) a = multiply(a, transition);
67
+ k >>= 1;
68
+ if (k > 0) transition = multiply(transition, transition);
69
+ }
70
+
71
+ for (mint x : a) cout << x.val() << '\n';
72
+ return 0;
73
+ }
problems/DP-04/Atcoder/abc199_f/statement.md ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Graph Smoothing
2
+
3
+ **Score**: 600 points
4
+
5
+ ## Problem Statement
6
+ We have a simple undirected graph with \(N\) vertices and \(M\) edges. The vertices are numbered \(1\) through \(N\) and the edges are numbered \(1\) through \(M\).
7
+ Edge \(i\) connects Vertex \(X_i\) and Vertex \(Y_i\). Initially, Vertex \(i\) has an integer \(A_i\) written on it.
8
+ You will do the following operation \(K\) times:
9
+ - Choose one from the \(M\) edges uniformly at random and independently from other choices. Let \(x\) be the arithmetic mean of the numbers written on the two vertices connected by that edge. Replace each number written on those vertices with \(x\).
10
+
11
+ For each vertex \(i\), find the expected value of the number written on that vertex after \(K\) operations, and print it modulo \((10^9 + 7)\) as described in Notes.
12
+
13
+ ## Notes
14
+ When printing a rational number, first, represent it as a fraction \(\frac{y}{x}\), where \(x\) and \(y\) are integers and \(x\) is not divisible by \((10^9+7)\) (under the Constraints of this problem, such a representation is always possible).
15
+ Then, print the only integer \(z\) between \(0\) and \((10^9+6)\) (inclusive) such that \(xz \equiv y \pmod {10^9+7}\).
16
+
17
+ ## Constraints
18
+ - \(2 \le N \le 100\)
19
+ - \(1 \le M \le \frac{N(N - 1)}{2}\)
20
+ - \(0 \le K \le 10^9\)
21
+ - \(0 \le A_i \le 10^9\)
22
+ - \(1 \le X_i \le N\)
23
+ - \(1 \le Y_i \le N\)
24
+ - The given graph is simple.
25
+ - All values in input are integers.
26
+
27
+ ---
28
+
29
+ ## Input
30
+ Input is given from Standard Input in the following format:
31
+ ```
32
+ N M K
33
+ A_1 A_2 A_3 ... A_N
34
+ X_1 Y_1
35
+ X_2 Y_2
36
+ X_3 Y_3
37
+ ...
38
+ X_M Y_M
39
+ ```
40
+
41
+ ## Output
42
+ Print \(N\) lines.
43
+ The \(i\)-th line should contain the expected value of the number written on that vertex after \(K\) operations, modulo \((10^9 + 7)\), as described in Notes.
44
+
45
+ ---
46
+
47
+ ## Sample Input 1
48
+ ```
49
+ 3 2 1
50
+ 3 1 5
51
+ 1 2
52
+ 1 3
53
+ ```
54
+
55
+ ## Sample Output 1
56
+ ```
57
+ 3
58
+ 500000005
59
+ 500000008
60
+ ```
61
+ - If Edge \(1\) is chosen in the only operation: the vertices written on Vertices \(1, 2, 3\) will be \(2, 2, 5\), respectively.
62
+ - If Edge \(2\) is chosen in the only operation: the vertices written on Vertices \(1, 2, 3\) will be \(4, 1, 4\), respectively.
63
+
64
+ Thus, the expected values of the numbers written on Vertices \(1, 2, 3\) are \(3, \frac{3}{2}, \frac{9}{2}\), respectively.
65
+ If we express them modulo \((10^9 + 7)\) as described in Notes, they will be \(3, 500000005, 500000008\), respectively.
66
+
67
+ ---
68
+
69
+ ## Sample Input 2
70
+ ```
71
+ 3 2 2
72
+ 12 48 36
73
+ 1 2
74
+ 1 3
75
+ ```
76
+
77
+ ## Sample Output 2
78
+ ```
79
+ 750000036
80
+ 36
81
+ 250000031
82
+ ```
83
+
84
+ ---
85
+
86
+ ## Sample Input 3
87
+ ```
88
+ 4 5 1000
89
+ 578 173 489 910
90
+ 1 2
91
+ 2 3
92
+ 3 4
93
+ 4 1
94
+ 1 3
95
+ ```
96
+
97
+ ## Sample Output 3
98
+ ```
99
+ 201113830
100
+ 45921509
101
+ 67803140
102
+ 685163678
103
+ ```
problems/DP-04/Atcoder/abc200_e/editorial.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # E - Patisserie ABC 2 (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc200/editorial/1548 by en_translator
4
+
5
+ There are many ways of solving this problem. We will introduce one of them.
6
+
7
+ First, let \(X\) be the sum of beauty, taste, and popularity, and for each integer from \(3\) through \(3N\) (inclusive), let us find the number of cakes where \(X = k\).
8
+ This number corresponds to the number of ways of choosing an integer from \(\{1,2, \dots , N\}\) three times whose sum is \(k\).
9
+ This problem can be solved by the DP (Dynamic Programming) where dp[the number of integers chosen][their sum]=[the number of such choices]. A naive implementation will need a total of \(O(N^2)\) time, but since the destination of a transition from \(dp[i][j]\) is a segment of \(dp[i+1][j+1],dp[i+1][j+2], \dots dp[i+1][j+N]\), so one can speed this up to \(O(N)\) with cumulative sums.
10
+
11
+ Now we figured out the value \(X\) of the \(K\)-th cake from the left. Then, the problem can be solved in an \(O(N)\) loop, iterating each “beauty” of the cake.
12
+ Specifically, once we know the “beauty” and the value \(X\) of a cake, the possible range of “taste” forms a single segment, so each iteration of the loop will find such segment. For more details, refer to the sample code as well.
13
+
14
+ Therefore, the problem has been solved in a total of \(O(N)\) time.
15
+
16
+ BONUS! This problem can be solved for little more large constraints, and the problem with many queries of cakes can also be solved. Do consider many ways to solve!
problems/DP-04/Atcoder/abc200_e/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 77,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc200_e/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc200_e",
3
+ "contest": "abc200",
4
+ "type": "ABC",
5
+ "letter": "E",
6
+ "title": "Patisserie ABC 2",
7
+ "url": "https://atcoder.jp/contests/abc200/tasks/abc200_e",
8
+ "difficulty": 1955,
9
+ "score": 500,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 77,
13
+ "editorial_url": "https://atcoder.jp/contests/abc200/editorial/1548",
14
+ "code_source": "editorial",
15
+ "pid": "A1577"
16
+ }
problems/DP-04/Atcoder/abc200_e/solution.cpp ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc200_e: E - Patisserie ABC 2
2
+ // Code from the official editorial, https://atcoder.jp/contests/abc200/editorial/1548 (by en_translator)
3
+ // Judged AC on all 77 tests, max 0.03 s of the 2 s limit.
4
+
5
+ #include<bits/stdc++.h>
6
+
7
+ using namespace std;
8
+
9
+ int main(){
10
+ int n;
11
+ long long k;
12
+ cin >> n >> k;
13
+ long long dp[4][3000005]={0};
14
+ dp[0][0]=1;
15
+ for(int i=0;i<3;i++){
16
+ for(int j=0;j<=i*n;j++){
17
+ dp[i+1][j+1]+=dp[i][j];
18
+ dp[i+1][j+n+1]-=dp[i][j];
19
+ }
20
+ for(int j=1;j<=(i+1)*n;j++){
21
+ dp[i+1][j]+=dp[i+1][j-1];
22
+ }
23
+ }
24
+
25
+ int x;
26
+ for(int i=3;i<=3*n;i++){
27
+ if(k<=dp[3][i]){x=i;break;}
28
+ else{k-=dp[3][i];}
29
+ }
30
+ for(int i=1;i<=n;i++){
31
+ int jmi=max(1,x-i-n);
32
+ int jma=min(n,x-i-1);
33
+ if(jmi>jma){continue;}
34
+ if(k>(jma-jmi+1)){k-=(jma-jmi+1);continue;}
35
+ int y=jmi+k-1;
36
+ int z=x-i-y;
37
+ cout << i << ' ' << y << ' ' << z << '\n';
38
+ return 0;
39
+ }
40
+ }
problems/DP-04/Atcoder/abc200_e/statement.md ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # E - Patisserie ABC 2
2
+
3
+ **Score:** 500 points
4
+
5
+ ## Problem Statement
6
+ Takahashi, a pastry chef at *ABC Confiserie*, has decided to make cakes to celebrate AtCoder Beginner Contest 200.
7
+
8
+ A cake made by Takahashi has three parameters: *beauty*, *taste*, and *popularity*, each of which is represented by an integer between `1` and `N` (inclusive).
9
+
10
+ He has made a cake of beauty `i`, taste `j`, and popularity `k` for every triple `(i,j,k) (1 ≤ i,j,k ≤ N)`.
11
+ Then, he has arranged these `N^3` cakes in a row, as follows:
12
+ - The cakes are in ascending order of the sum of beauty, taste, and popularity from left to right.
13
+ - For two cakes with the same sum of beauty, taste, and popularity, the cake with the smaller beauty is to the left.
14
+ - For two cakes with the same sum and the same beauty, the cake with the smaller taste is to the left.
15
+
16
+ Find the beauty, taste, and popularity of the `K`-th cake from the left.
17
+
18
+ ## Constraints
19
+ - All values in input are integers.
20
+ - `1 ≤ N ≤ 10^6`
21
+ - `1 ≤ K ≤ N^3`
22
+
23
+ ## Input
24
+ Input is given from Standard Input in the following format:
25
+ ```
26
+ N K
27
+ ```
28
+
29
+ ## Output
30
+ Print three integers representing the cake's beauty, taste, popularity, in this order, with spaces in between.
31
+
32
+ ## Sample Input 1
33
+ ```
34
+ 2 5
35
+ ```
36
+
37
+ ## Sample Output 1
38
+ ```
39
+ 1 2 2
40
+ ```
41
+ The cakes are in the following order:
42
+ ```
43
+ (1,1,1),(1,1,2),(1,2,1),(2,1,1),(1,2,2),(2,1,2),(2,2,1),(2,2,2).
44
+ ```
45
+ Here, each triple of integers represents the beauty, taste, and popularity of a cake.
46
+
47
+ ## Sample Input 2
48
+ ```
49
+ 1000000 1000000000000000000
50
+ ```
51
+
52
+ ## Sample Output 2
53
+ ```
54
+ 1000000 1000000 1000000
55
+ ```
56
+ The values in input may be large.
57
+
58
+ ## Sample Input 3
59
+ ```
60
+ 9 47
61
+ ```
62
+
63
+ ## Sample Output 3
64
+ ```
65
+ 3 1 4
66
+ ```
problems/DP-04/Atcoder/abc201_f/editorial.md ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Insertion Sort (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc201/editorial/1838 by en_translator
4
+
5
+ First of all, each person has to be moved at most once. So, this problem is equivalent to the task of assigning each person the four statuses of either moving to the left end / moving to the right end / moving arbitrarily / never let them move. We will approach the problem with this premise.
6
+
7
+ First, let us fix an ordered set \(S=(S_1,S_2,\ldots,S_k)\) of people that will never be moved. Here, we assume \(S_1 \lt S_2 \lt \cdots \lt S_k\). Then we can see that:
8
+
9
+ - Let \(\text{pos}[i]\) be the initial position of the person \(i\) counted from the left. Then, it should hold that \(\text{pos}[S_1] \lt \text{pos}[S_2] \lt \cdots \lt \text{pos}[S_k]\).
10
+
11
+ Since every person in \(S\) does not move, it is obvious.
12
+
13
+ With more observation we can see the following three facts:
14
+
15
+ - Each person \(i\) such that \(S_1 \lt i \lt S_k\), \(i \notin S\) should be assigned the operation of being moved to an arbitrary position, paying cost \(A_i\).
16
+ - Each person \(i\) such that \(i \lt S_1\) can be assigned either operation of being moved to an arbitrary position with cost \(A_i\) or being moved to the left end with cost \(B_i\).
17
+ - Each person \(i\) such that \(S_k \lt i\) can be assigned either operation of being moved an arbitrary position with cost \(A_i\) or being moved to the right end with cost \(C_i\).
18
+
19
+ Based on these observations, we will perform a DP (Dynamic Programming) where \(\text{dp}[i]:=(\)the minimum sum of costs required to sort the people with indices less than or equal to \(i\), such that the maximum index of person in \(S\) is \(i)\). The transitions are as follows:
20
+
21
+ \[ dp[i]:=\min(\sum_{j=1}^{i-1}\min(A_j,B_j),\ \min_{j \lt i,\ pos[j] \lt pos[i]}(dp[j]+\sum_{k=j+1}^{i-1}A_k)) \]
22
+
23
+ The DP above costs \(O(N^2)\) naively, but it can be optimized to \(O(N \log N)\) by segment trees and cumulative sums.
24
+
25
+ Once the DP is processed, we can obtain the answer as \(\min_{1 \leq i \leq N}(dp[i]+\sum_{j=i+1}^{N} \min(A_j,C_j))\). Therefore we could solve the problem.
26
+
27
+ Note that \(S\) can never be empty in the optimal solution. If \(S\) is empty, there exists an integer \(i\ (0 \leq i \leq N)\) such that
28
+
29
+ - every person \(j\) such that \(j \leq i\) is assigned either operation of being moved to an arbitrary position with cost \(A_i\) or being moved to the left end with cost \(B_j\), and
30
+ - every person \(j\) such that \(i \lt j\) is assigned either operation of being moved to an arbitrary position with cost \(A_i\) or being moved to the right end with cost \(C_j\).
31
+
32
+ Here, one can add to \(S\) either of the person \(i\) or \(i+1\) that exists (or one of them if both exist) so that the total cost is strictly less than that of empty \(S\).
problems/DP-04/Atcoder/abc201_f/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 27,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc201_f/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc201_f",
3
+ "contest": "abc201",
4
+ "type": "ABC",
5
+ "letter": "F",
6
+ "title": "Insertion Sort",
7
+ "url": "https://atcoder.jp/contests/abc201/tasks/abc201_f",
8
+ "difficulty": 2484,
9
+ "score": 600,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 27,
13
+ "editorial_url": "https://atcoder.jp/contests/abc201/editorial/1838",
14
+ "code_source": "editorial",
15
+ "pid": "A1584"
16
+ }
problems/DP-04/Atcoder/abc201_f/solution.cpp ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc201_f: F - Insertion Sort
2
+ // Code from the official editorial, https://atcoder.jp/contests/abc201/editorial/1838 (by en_translator)
3
+ // Judged AC on all 27 tests, max 0.17 s of the 2 s limit.
4
+
5
+ #include<bits/stdc++.h>
6
+ #include<atcoder/all>
7
+ using namespace std;
8
+ using namespace atcoder;
9
+ using ll = long long;
10
+ const ll inf = (1ll<<60);
11
+
12
+ ll op(ll a,ll b){
13
+ return min(a,b);
14
+ }
15
+
16
+ ll e(){
17
+ return inf;
18
+ }
19
+
20
+ int main(){
21
+ int N; cin >> N;
22
+ vector<int> P(N),A(N),B(N),C(N),pos(N+1);
23
+ for(int i=0; i<N; i++){
24
+ cin >> P[i];
25
+ pos[P[i]] = i+1;
26
+ }
27
+ for(int i=0; i<N; i++) cin >> A[i] >> B[i] >> C[i];
28
+ vector<ll> dp(N+1,inf),Asum(N+1),Bsum(N+1),Csum(N+1);
29
+ segtree<ll,op,e> seg(N+1);
30
+ ll ans = inf;
31
+ for(int i=0; i<N; i++){
32
+ Asum[i+1] = Asum[i]+A[i];
33
+ Bsum[i+1] = Bsum[i]+min(B[i],A[i]);
34
+ Csum[i+1] = Csum[i]+min(C[i],A[i]);
35
+ }
36
+ for(int i=1; i<=N; i++){
37
+ dp[i] = min(Bsum[i-1],seg.prod(0,pos[i])+Asum[i-1]);
38
+ ans = min(ans, dp[i]+Csum[N]-Csum[i]);
39
+ seg.set(pos[i],dp[i]-Asum[i]);
40
+ }
41
+ cout << ans << endl;
42
+ }
problems/DP-04/Atcoder/abc201_f/statement.md ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # F - Insertion Sort
2
+
3
+ **Score:** 600 points
4
+
5
+ ## Problem Statement
6
+
7
+ There are \( N \) people, who are given ID numbers \( 1 \) through \( N \), standing in a row from left to right. Initially, the ID number of the \( i \)-th person from the left is \( P_i \).
8
+
9
+ Your objective is to rearrange the people in ascending order of the ID number from left to right, by repeatedly doing the three kinds of operations below. You can do these operations any number of times (possibly zero) in any order.
10
+
11
+ - Choose an integer \( i\ (1 \leq i \leq N) \), pay the cost \( A_i \), and move Person \( i \) (the person with the ID number \( i \)) to any position of your choice.
12
+ - Choose an integer \( i\ (1 \leq i \leq N) \), pay the cost \( B_i \), and move Person \( i \) to the left end of the row.
13
+ - Choose an integer \( i\ (1 \leq i \leq N) \), pay the cost \( C_i \), and move Person \( i \) to the right end of the row.
14
+
15
+ Minimize the total cost you pay before achieving the objective.
16
+
17
+ ## Constraints
18
+
19
+ - \( 1 \leq N \leq 2 \times 10^5 \)
20
+ - \( 1 \leq P_i \leq N \)
21
+ - \( 1 \leq A_i, B_i, C_i \leq 10^9 \)
22
+ - \( P_i \neq P_j\ (i \neq j) \)
23
+ - All values in input are integers.
24
+
25
+ ## Input
26
+
27
+ Input is given from Standard Input in the following format:
28
+
29
+ ```
30
+ N
31
+ P_1 P_2 ... P_N
32
+ A_1 B_1 C_1
33
+ A_2 B_2 C_2
34
+ ...
35
+ A_N B_N C_N
36
+ ```
37
+
38
+ ## Output
39
+
40
+ Print the minimum total cost you need to pay before achieving the objective.
41
+
42
+ ## Sample Input 1
43
+ ```
44
+ 3
45
+ 3 1 2
46
+ 9 3 5
47
+ 8 6 4
48
+ 9 4 6
49
+ ```
50
+
51
+ ## Sample Output 1
52
+ ```
53
+ 6
54
+ ```
55
+ You can rearrange the people in ascending order of the ID number by paying the cost \( C_3=6 \) to move Person \( 3 \) to the right end. There is no way to rearrange the people that costs less, so the answer is \( 6 \).
56
+
57
+ ## Sample Input 2
58
+ ```
59
+ 6
60
+ 2 6 5 3 4 1
61
+ 10 8 16
62
+ 30 2 10
63
+ 10 17 8
64
+ 11 27 22
65
+ 8 6 5
66
+ 15 29 2
67
+ ```
68
+
69
+ ## Sample Output 2
70
+ ```
71
+ 15
72
+ ```
73
+ The following sequence of operations minimizes the total cost:
74
+ - pay the cost \( B_1=8 \) to move Person \( 1 \) to the left end;
75
+ - pay the cost \( C_5=5 \) to move Person \( 5 \) to the right end;
76
+ - pay the cost \( C_6=2 \) to move Person \( 6 \) to the right end.
77
+
78
+ ## Sample Input 3
79
+ ```
80
+ 9
81
+ 3 8 4 7 6 9 1 5 2
82
+ 7976 3696 9706
83
+ 768 8807 8521
84
+ 1133 8683 7120
85
+ 1189 3331 2259
86
+ 900 7451 1159
87
+ 6126 2639 7107
88
+ 5540 8253 2891
89
+ 8417 4220 9091
90
+ 8732 1417 1540
91
+ ```
92
+
93
+ ## Sample Output 3
94
+ ```
95
+ 15865
96
+ ```
97
+
98
+ ## Sample Input 4
99
+ ```
100
+ 12
101
+ 11 9 1 12 2 7 3 5 10 4 6 8
102
+ 3960 3158 9029
103
+ 6521 6597 7581
104
+ 5688 2299 2123
105
+ 4946 4298 9122
106
+ 394 4350 9142
107
+ 3098 7151 2039
108
+ 8525 3758 6155
109
+ 6970 3658 9353
110
+ 9780 1778 3608
111
+ 6065 5562 923
112
+ 9701 5524 6482
113
+ 9395 6016 705
114
+ ```
115
+
116
+ ## Sample Output 4
117
+ ```
118
+ 20637
119
+ ```
problems/DP-04/Atcoder/abc203_e/editorial.md ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # E - White Pawn (editorial)
2
+
3
+ Source: https://atcoder.jp/contests/abc203/editorial/2010 by en_translator
4
+
5
+ Consider maintaining a set of squares (the indices of columns), updating incrementally for each row.
6
+ In other words, let \(S_i=\{ j\in\mathbb{Z} \mid \)the white pawn can reach \((i,j)\)\(\}\ (0\leq i \leq 2N)\), and we want to derive \(S_{i+1}\) from \(S_i\) for each \(i=0,1,\ldots 2N-1\).
7
+ Of course, if we do this naively, it will exceed the time limit.
8
+
9
+ Actually however, we can only focus on the difference between \(S_i\) and \(S_{i+1}\) to drastically improve the computation time. The key fact is:
10
+
11
+ - Suppose that \((i+1, j)\) has no black pawn. If the white pawn is reachable to \((i, j)\), so is \((i+1 ,j)\); if it cannot reach \((i,j)\), then it cannot to \((i+1 ,j)\) either.
12
+
13
+ First, we can now see that \(S_i=S_{i-1}\) if the \(i\)-th row does not contain black pawns. Also, even for the row with black pawns, the between transitions from \(S_{i-1}\) to \(S_i\) are either:
14
+
15
+ - If ( \(j-1 \in S_{i-1}\) or \(j+1 \in S_{i-1}\) ) and \(j \notin S_{i-1}\), then \(j \notin S_{i-1} \to j \in S_i\).
16
+ - If ( \(j-1 \notin S_{i-1}\) and \(j+1 \notin S_{i-1}\) ) and \(j \in S_{i-1}\), then \(j \in S_{i-1} \to j \notin S_i\).
17
+
18
+ Moreover, since any \(j\) such that \(j<0\) or \(2N<j\) can never be given as an column index of a black pawn, and since \(j\notin S_0\), \(j\notin S_i\) for every \(i\), so we do not have to take care of the left and right boundary.
19
+
20
+ Therefore, only focusing on the squares with black pawns, it can be solved as follows. First, sort \((X_i, Y_i)\) in the increasing order. Next, starting from \(S=\{N\}\), for each chunk with the equal \(X_i\) in the sorted sequence, repeat the following. We assume \(X_l=X_{l+1}=\cdots =X_r\).
21
+
22
+ - Prepare a set \(A\) that will eventually be added to \(S\) and a set \(B\) that will be removed from \(S\), both initialized with empty sets. For each \(i=l,l+1, \ldots, r\), perform the following operations:
23
+
24
+ - If ( \(Y_i-1 \in S\) or \(Y_i+1 \in S\) ) and \(Y_i \notin S\), then add \(Y_i\) to \(A\).
25
+ - If ( \(Y_i-1 \notin S\) and \(Y_i+1 \notin S\) ) and \(Y_i \in S\), then add \(_i\) to \(B\).
26
+ - Otherwise, do nothing.
27
+ - Update \(S\) by adding the elements of \(A\) to \(S\) and removing the elements of \(B\) from \(S\).
28
+
29
+ The final set \(S\) is a set of indices \(j\) such the white pawn is reachable to \((2N, j)\), and the desired answer is the size of the set \(\lvert S\rvert\).
30
+
31
+ The time complexity is \(O(M\log M)\) for the initial sort, \(O(\log M)\) for each lookup, addition, and removal if an ordered “set” is used, so \(O(M\log M)\) in total. Therefore, the problem can be solved in \(O(M\log M)\) time, which is fast enough. Also, by the facts so far, if the white pawn is reachable to \((2N,j)\) then \(j = N\) or the row \(j\) has at least one black pawn, so the answer is at most \(M+1\), which fits in a \(32\)-bit integer.
problems/DP-04/Atcoder/abc203_e/judge.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "time_limit_s": 2.0,
3
+ "time_limit_source": "task page",
4
+ "memory_mb": 1024,
5
+ "memory_source": "task page",
6
+ "mode": "exact",
7
+ "validator_flags": "case_sensitive",
8
+ "comparison": "token-wise, whitespace-insensitive, case-sensitive (AtCoder's checker)",
9
+ "validator": null,
10
+ "tests_dir": "data",
11
+ "answer_suffix": ".out",
12
+ "n_sample": null,
13
+ "n_secret": 40,
14
+ "note": "no sample/secret split: the statement's samples are among the numbered tests"
15
+ }
problems/DP-04/Atcoder/abc203_e/meta.json ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "task_id": "abc203_e",
3
+ "contest": "abc203",
4
+ "type": "ABC",
5
+ "letter": "E",
6
+ "title": "White Pawn",
7
+ "url": "https://atcoder.jp/contests/abc203/tasks/abc203_e",
8
+ "difficulty": 1750,
9
+ "score": 500,
10
+ "time_limit_sec": 2.0,
11
+ "memory_limit_mb": 1024,
12
+ "n_tests": 40,
13
+ "editorial_url": "https://atcoder.jp/contests/abc203/editorial/2010",
14
+ "code_source": "editorial",
15
+ "pid": "A1595"
16
+ }
problems/DP-04/Atcoder/abc203_e/solution.cpp ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // abc203_e: E - White Pawn
2
+ // Code from the official editorial, https://atcoder.jp/contests/abc203/editorial/2010 (by en_translator)
3
+ // Judged AC on all 40 tests, max 0.21 s of the 2 s limit.
4
+
5
+ #include <bits/stdc++.h>
6
+
7
+ using namespace std;
8
+
9
+ int main(void) {
10
+ int n, m;
11
+ vector<pair<int, int> > p;
12
+ vector<int>a, b;
13
+ set<int>s;
14
+ int x, y;
15
+ int l, r;
16
+ cin >> n >> m;
17
+ for (int i = 0; i < m; i++) {
18
+ cin >> x >> y;
19
+ p.push_back({ x,y });
20
+ }
21
+ sort(p.begin(), p.end());
22
+ s.insert(n);
23
+ l = 0;
24
+ while (l < m) {
25
+ a.clear();
26
+ b.clear();
27
+ r = l;
28
+ while (r < m) {
29
+ if (p[r].first == p[l].first)r++;
30
+ else break;
31
+ }
32
+ for (int i = l; i < r; i++) {
33
+ y = p[i].second;
34
+ if (((s.find(y - 1) != s.end()) || (s.find(y + 1) != s.end())) && (s.find(y) == s.end()))a.push_back(y);
35
+ if (((s.find(y - 1) == s.end()) && (s.find(y + 1) == s.end())) && (s.find(y) != s.end()))b.push_back(y);
36
+ }
37
+ for (int i = 0; i < a.size(); i++)s.insert(a[i]);
38
+ for (int i = 0; i < b.size(); i++)s.erase(b[i]);
39
+ l = r;
40
+ }
41
+ cout << s.size() << endl;
42
+ }
problems/DP-04/Atcoder/abc203_e/statement.md ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # E - White Pawn
2
+
3
+ **Score :** 500 points
4
+
5
+ ## Problem Statement
6
+
7
+ Let \( N \) be a positive integer.
8
+ We have a \( (2N+1)\times (2N+1) \) grid where rows are numbered \( 0 \) through \( 2N \) and columns are also numbered \( 0 \) through \( 2N \). Let \( (i,j) \) denote the square at Row \( i \) and Column \( j \).
9
+
10
+ We have one white pawn, which is initially at \( (0, N) \).
11
+ Also, we have \( M \) black pawns, the \( i \)-th of which is at \( (X_i, Y_i) \).
12
+
13
+ When the white pawn is at \( (i, j) \), you can do one of the following operations to move it:
14
+ - If \( 0 \leq i \leq 2N-1 \), \( 0 \leq j \leq 2N \) hold and \( (i+1,j) \) **does not** contain a black pawn, move the white pawn to \( (i+1, j) \).
15
+ - If \( 0 \leq i \leq 2N-1 \), \( 0 \leq j \leq 2N-1 \) hold and \( (i+1,j+1) \) **does** contain a black pawn, move the white pawn to \( (i+1,j+1) \).
16
+ - If \( 0 \leq i \leq 2N-1 \), \( 1 \leq j \leq 2N \) hold and \( (i+1,j-1) \) **does** contain a black pawn, move the white pawn to \( (i+1,j-1) \).
17
+
18
+ You cannot move the black pawns.
19
+ Find the number of integers \( Y \) such that it is possible to have the white pawn at \( (2N, Y) \) by repeating these operations.
20
+
21
+ ## Constraints
22
+ - \( 1 \leq N \leq 10^9 \)
23
+ - \( 0 \leq M \leq 2\times 10^5 \)
24
+ - \( 1 \leq X_i \leq 2N \)
25
+ - \( 0 \leq Y_i \leq 2N \)
26
+ - \( (X_i, Y_i) \neq (X_j, Y_j) \) \( (i \neq j) \)
27
+ - All values in input are integers.
28
+
29
+ ## Input
30
+ Input is given from Standard Input in the following format:
31
+ ```
32
+ N M
33
+ X_1 Y_1
34
+ :
35
+ X_M Y_M
36
+ ```
37
+
38
+ ## Output
39
+ Print the answer.
40
+
41
+ ## Sample Input 1
42
+ ```
43
+ 2 4
44
+ 1 1
45
+ 1 2
46
+ 2 0
47
+ 4 2
48
+ ```
49
+
50
+ ## Sample Output 1
51
+ ```
52
+ 3
53
+ ```
54
+ We can move the white pawn to \( (4,0) \), \( (4,1) \), and \( (4,2) \), as follows:
55
+ - \( (0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,2) \)
56
+ - \( (0,2)\to (1,1)\to (2,1)\to (3,1)\to (4,1) \)
57
+ - \( (0,2)\to (1,1)\to (2,0)\to (3,0)\to (4,0) \)
58
+
59
+ On the other hand, we cannot move it to \( (4,3) \) or \( (4,4) \).
60
+ Thus, we should print \( 3 \).
61
+
62
+ ## Sample Input 2
63
+ ```
64
+ 1 1
65
+ 1 1
66
+ ```
67
+
68
+ ## Sample Output 2
69
+ ```
70
+ 0
71
+ ```
72
+ We cannot move the white pawn from \( (0,1) \).
problems/DP-04/Atcoder/abc204_f/data/1.in ADDED
@@ -0,0 +1 @@
 
 
1
+ 1 1
problems/DP-04/Atcoder/abc204_f/data/1.out ADDED
@@ -0,0 +1 @@
 
 
1
+ 1
problems/DP-04/Atcoder/abc204_f/data/10.in ADDED
@@ -0,0 +1 @@
 
 
1
+ 3 10
problems/DP-04/Atcoder/abc204_f/data/10.out ADDED
@@ -0,0 +1 @@
 
 
1
+ 47177097
problems/DP-04/Atcoder/abc204_f/data/11.in ADDED
@@ -0,0 +1 @@
 
 
1
+ 4 8
problems/DP-04/Atcoder/abc204_f/data/11.out ADDED
@@ -0,0 +1 @@
 
 
1
+ 211351945
problems/DP-04/Atcoder/abc204_f/data/12.in ADDED
@@ -0,0 +1 @@
 
 
1
+ 4 1000000000000
problems/DP-04/Atcoder/abc204_f/data/12.out ADDED
@@ -0,0 +1 @@
 
 
1
+ 874208246
problems/DP-04/Atcoder/abc204_f/data/13.in ADDED
@@ -0,0 +1 @@
 
 
1
+ 5 4
problems/DP-04/Atcoder/abc204_f/data/13.out ADDED
@@ -0,0 +1 @@
 
 
1
+ 120465