Datasets:
Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- problems/DP-02/Atcoder/abc180_e/editorial.md +20 -0
- problems/DP-02/Atcoder/abc180_e/judge.json +15 -0
- problems/DP-02/Atcoder/abc180_e/meta.json +16 -0
- problems/DP-02/Atcoder/abc180_e/solution.cpp +54 -0
- problems/DP-02/Atcoder/abc180_e/statement.md +98 -0
- problems/DP-02/Atcoder/abc187_f/editorial.md +33 -0
- problems/DP-02/Atcoder/abc187_f/judge.json +15 -0
- problems/DP-02/Atcoder/abc187_f/meta.json +16 -0
- problems/DP-02/Atcoder/abc187_f/solution.cpp +49 -0
- problems/DP-02/Atcoder/abc187_f/statement.md +101 -0
- problems/DP-02/Atcoder/abc190_e/editorial.md +25 -0
- problems/DP-02/Atcoder/abc190_e/judge.json +15 -0
- problems/DP-02/Atcoder/abc190_e/meta.json +16 -0
- problems/DP-02/Atcoder/abc190_e/solution.cpp +80 -0
- problems/DP-02/Atcoder/abc190_e/statement.md +88 -0
- problems/DP-02/Atcoder/abc192_f/editorial.md +13 -0
- problems/DP-02/Atcoder/abc192_f/judge.json +15 -0
- problems/DP-02/Atcoder/abc192_f/meta.json +16 -0
- problems/DP-02/Atcoder/abc192_f/solution.cpp +39 -0
- problems/DP-02/Atcoder/abc192_f/statement.md +62 -0
- problems/DP-02/Atcoder/abc195_f/editorial.md +9 -0
- problems/DP-02/Atcoder/abc195_f/judge.json +15 -0
- problems/DP-02/Atcoder/abc195_f/meta.json +16 -0
- problems/DP-02/Atcoder/abc195_f/solution.cpp +48 -0
- problems/DP-02/Atcoder/abc195_f/statement.md +85 -0
- problems/DP-02/Atcoder/abc199_e/editorial.md +18 -0
- problems/DP-02/Atcoder/abc199_e/judge.json +15 -0
- problems/DP-02/Atcoder/abc199_e/meta.json +16 -0
- problems/DP-02/Atcoder/abc199_e/solution.cpp +43 -0
- problems/DP-02/Atcoder/abc199_e/statement.md +77 -0
- problems/DP-02/Atcoder/abc213_g/editorial.md +86 -0
- problems/DP-02/Atcoder/abc213_g/judge.json +15 -0
- problems/DP-02/Atcoder/abc213_g/meta.json +16 -0
- problems/DP-02/Atcoder/abc213_g/solution.cpp +65 -0
- problems/DP-02/Atcoder/abc213_g/statement.md +74 -0
- problems/DP-02/Atcoder/abc215_e/editorial.md +18 -0
- problems/DP-02/Atcoder/abc215_e/judge.json +15 -0
- problems/DP-02/Atcoder/abc215_e/meta.json +16 -0
- problems/DP-02/Atcoder/abc215_e/solution.cpp +42 -0
- problems/DP-02/Atcoder/abc215_e/statement.md +56 -0
- problems/DP-02/Atcoder/abc221_h/editorial.md +40 -0
- problems/DP-02/Atcoder/abc221_h/judge.json +15 -0
- problems/DP-02/Atcoder/abc221_h/meta.json +16 -0
- problems/DP-02/Atcoder/abc221_h/solution.cpp +35 -0
- problems/DP-02/Atcoder/abc221_h/statement.md +58 -0
- problems/DP-02/Atcoder/abc222_e/editorial.md +15 -0
- problems/DP-02/Atcoder/abc222_e/judge.json +15 -0
- problems/DP-02/Atcoder/abc222_e/meta.json +16 -0
- problems/DP-02/Atcoder/abc222_e/solution.cpp +55 -0
- problems/DP-02/Atcoder/abc222_e/statement.md +106 -0
problems/DP-02/Atcoder/abc180_e/editorial.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Traveling Salesman among Aerial Cities (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc180/editorial/249 by evima
|
| 4 |
+
|
| 5 |
+
Since the triangular inequalities hold among the costs of traveling between the cities, so there is no need to travel from one city to another via other cities other than those two cities.
|
| 6 |
+
|
| 7 |
+
Searching all the orders of visiting towns requires a total of \(O(N! * N)\) time, in which the answer cannot be obtained in time under the constraints this time. The problem can be solved by the following DP:
|
| 8 |
+
|
| 9 |
+
\(DP[i][S]=\) The minimum cost when you are currently at city \(i\) and the set of towns you have already visited is \(S\)
|
| 10 |
+
|
| 11 |
+
\(DP[i][S]=\min\{DP[j][S\setminus \{i\}]+dist(i,j) \mid j\in S\setminus\{i\}\}\)
|
| 12 |
+
|
| 13 |
+
Here, you may hold the set \(S\) itself as a key, but you can treat those sets as integers by associating them with binaries, for example, Associating \(\{0,3,5\}\) with \(2^0+2^3+2^5=41\). That’s why this kind of DP is sometimes called bitDP.
|
| 14 |
+
|
| 15 |
+
The integer obtained by the correspondence, \(f(S)\), satisfies the following properties:
|
| 16 |
+
|
| 17 |
+
- \(i\in S \Longleftrightarrow\) The \(i\)-th bit of \(f(S)\) is \(1\)
|
| 18 |
+
- \(T \subset S \Longleftrightarrow (f(T) \text{ bitwiseor } f(S))=f(S) \Longrightarrow f(T) \leq f(S)\)
|
| 19 |
+
|
| 20 |
+
so operations and properties on sets can be represented concisely by bit operations.
|
problems/DP-02/Atcoder/abc180_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": 23,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc180_e/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc180_e",
|
| 3 |
+
"contest": "abc180",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "E",
|
| 6 |
+
"title": "Traveling Salesman among Aerial Cities",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc180/tasks/abc180_e",
|
| 8 |
+
"difficulty": 1256,
|
| 9 |
+
"score": 500,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 23,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc180/editorial/249",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1457"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc180_e/solution.cpp
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc180_e: E - Traveling Salesman among Aerial Cities
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc180/editorial/249
|
| 3 |
+
// Judged AC on all 23 tests, max 0.02 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 |
+
cin >> N;
|
| 14 |
+
vector<long long> X(N), Y(N), Z(N);
|
| 15 |
+
for (int i = 0; i < N; ++i) {
|
| 16 |
+
cin >> X[i] >> Y[i] >> Z[i];
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
long long cost[17][17];
|
| 20 |
+
for (int i = 0; i < N; ++i) {
|
| 21 |
+
for (int j = 0; j < N; ++j) {
|
| 22 |
+
cost[i][j] = abs(X[j] - X[i]) + abs(Y[j] - Y[i])
|
| 23 |
+
+ max(0LL, Z[j] - Z[i]);
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
const int states = 1 << N;
|
| 28 |
+
const int full = states - 1;
|
| 29 |
+
const long long INF = 1LL << 60;
|
| 30 |
+
vector<long long> dp(static_cast<size_t>(states) * N, INF);
|
| 31 |
+
dp[N] = 0; // Visited only city 0, currently at city 0.
|
| 32 |
+
|
| 33 |
+
for (int mask = 1; mask < states; mask += 2) {
|
| 34 |
+
for (int i = 0; i < N; ++i) {
|
| 35 |
+
long long current = dp[mask * N + i];
|
| 36 |
+
if (current == INF) continue;
|
| 37 |
+
int remaining = full ^ mask;
|
| 38 |
+
while (remaining) {
|
| 39 |
+
int j = __builtin_ctz(remaining);
|
| 40 |
+
remaining &= remaining - 1;
|
| 41 |
+
int next_mask = mask | (1 << j);
|
| 42 |
+
long long &next = dp[next_mask * N + j];
|
| 43 |
+
next = min(next, current + cost[i][j]);
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
long long answer = INF;
|
| 49 |
+
for (int i = 0; i < N; ++i) {
|
| 50 |
+
answer = min(answer, dp[full * N + i] + cost[i][0]);
|
| 51 |
+
}
|
| 52 |
+
cout << answer << '\n';
|
| 53 |
+
return 0;
|
| 54 |
+
}
|
problems/DP-02/Atcoder/abc180_e/statement.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Traveling Salesman among Aerial Cities
|
| 2 |
+
|
| 3 |
+
**Score:** 500 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
|
| 7 |
+
In a three-dimensional space, there are \(N\) cities: City \(1\) through City \(N\). City \(i\) is at point \((X_i,Y_i,Z_i)\).
|
| 8 |
+
|
| 9 |
+
The cost it takes to travel from a city at point \((a,b,c)\) to a city at point \((p,q,r)\) is \(|p-a|+|q-b|+\max(0,r-c)\).
|
| 10 |
+
|
| 11 |
+
Find the minimum total cost it takes to start at City \(1\), visit all other cities at least once, and return to City \(1\).
|
| 12 |
+
|
| 13 |
+
## Constraints
|
| 14 |
+
- \(2 \leq N \leq 17\)
|
| 15 |
+
- \(-10^6 \leq X_i,Y_i,Z_i \leq 10^6\)
|
| 16 |
+
- No two cities are at the same point.
|
| 17 |
+
- All values in input are integers.
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
## Input
|
| 22 |
+
Input is given from Standard Input in the following format:
|
| 23 |
+
|
| 24 |
+
```
|
| 25 |
+
N
|
| 26 |
+
X_1 Y_1 Z_1
|
| 27 |
+
\vdots
|
| 28 |
+
X_N Y_N Z_N
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Output
|
| 32 |
+
Print the minimum total cost it takes to start at City \(1\), visit all other cities at least once, and return to City \(1\).
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
## Sample Input 1
|
| 37 |
+
```
|
| 38 |
+
2
|
| 39 |
+
0 0 0
|
| 40 |
+
1 2 3
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Sample Output 1
|
| 44 |
+
```
|
| 45 |
+
9
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
The cost it takes to travel from City \(1\) to City \(2\) is \(|1-0|+|2-0|+\max(0,3-0)=6\).
|
| 49 |
+
|
| 50 |
+
The cost it takes to travel from City \(2\) to City \(1\) is \(|0-1|+|0-2|+\max(0,0-3)=3\).
|
| 51 |
+
|
| 52 |
+
Thus, the total cost will be \(9\).
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
## Sample Input 2
|
| 57 |
+
```
|
| 58 |
+
3
|
| 59 |
+
0 0 0
|
| 60 |
+
1 1 1
|
| 61 |
+
-1 -1 -1
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
## Sample Output 2
|
| 65 |
+
```
|
| 66 |
+
10
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
For example, we can visit the cities in the order \(1\), \(2\), \(1\), \(3\), \(1\) to make the total cost \(10\). Note that we can come back to City \(1\) on the way.
|
| 70 |
+
|
| 71 |
+
---
|
| 72 |
+
|
| 73 |
+
## Sample Input 3
|
| 74 |
+
```
|
| 75 |
+
17
|
| 76 |
+
14142 13562 373095
|
| 77 |
+
-17320 508075 68877
|
| 78 |
+
223606 -79774 9979
|
| 79 |
+
-24494 -89742 783178
|
| 80 |
+
26457 513110 -64591
|
| 81 |
+
-282842 7124 -74619
|
| 82 |
+
31622 -77660 -168379
|
| 83 |
+
-33166 -24790 -3554
|
| 84 |
+
346410 16151 37755
|
| 85 |
+
-36055 51275 463989
|
| 86 |
+
37416 -573867 73941
|
| 87 |
+
-3872 -983346 207417
|
| 88 |
+
412310 56256 -17661
|
| 89 |
+
-42426 40687 -119285
|
| 90 |
+
43588 -989435 -40674
|
| 91 |
+
-447213 -59549 -99579
|
| 92 |
+
45825 7569 45584
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
## Sample Output 3
|
| 96 |
+
```
|
| 97 |
+
6519344
|
| 98 |
+
```
|
problems/DP-02/Atcoder/abc187_f/editorial.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Close Group (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc187/editorial/502 by en_translator
|
| 4 |
+
|
| 5 |
+
Let \(V\) be the vertex set of the graph, \(S\) be a non-empty subset of \(V\) and \(f(S)\) be the answer for this problem when the input is the subgraph induced by \(S\).
|
| 6 |
+
What we want is \(f(V)\), but it is necessary to calculate all \(f(S)\) when obtaining \(f(V)\), so let us consider calculating all \(f(S)\).
|
| 7 |
+
|
| 8 |
+
If the induced subgraph by \(S\) is complete, then \(f(S) = 1\); otherwise \(f(S)\) is determined by \(\displaystyle \min_{T \subset S}f(T)+f(S \setminus T)\).
|
| 9 |
+
Therefore, one can determine \(f(S)\) in order that smaller subsets are calculated earlier.
|
| 10 |
+
|
| 11 |
+
Let us consider the complexity.
|
| 12 |
+
One can determine if each induced subgraph is complete in \(O(2^N N^2)\) time for all \(S\).
|
| 13 |
+
The problem is \(\displaystyle \min_{T \subset S}f(T)+f(S \setminus T)\) part.
|
| 14 |
+
If \(T\) is iterated through all subset of \(V\), the total computation time for all \(S\) will be \(O(2^N \cdot 2^N)=O(4^N)\); actually, however, \(S\) can be processed in a total of \(O(3^N)\) time, since \(T\) can only be a subset of \(S\), and
|
| 15 |
+
|
| 16 |
+
\[ \sum_{S \subset V} 2^{|S|}=3^N. \]
|
| 17 |
+
|
| 18 |
+
This formula can also be understood from the fact that each element of \(V\) can be either
|
| 19 |
+
|
| 20 |
+
- contained in both \(S\) and \(T\),
|
| 21 |
+
- contained only in \(S\), or
|
| 22 |
+
- not contained in either set,
|
| 23 |
+
|
| 24 |
+
independently.
|
| 25 |
+
|
| 26 |
+
The answer for this problem is the chromatic number of complement graph when
|
| 27 |
+
|
| 28 |
+
- two vertices directly connected by an edge can be painted with the same color (if not, the edge is removed), and
|
| 29 |
+
- two vertices not directly connected by edges cannot be painted with the same color.
|
| 30 |
+
|
| 31 |
+
It is known that the chromatic number of a graph calculated in a total of \(O(2^NN)\) time, so the original problem can be solved in a total of \(O(2^NN)\) time as well.
|
| 32 |
+
|
| 33 |
+
Related Materials (Japanese): https://www.slideshare.net/wata_orz/ss-12131479
|
problems/DP-02/Atcoder/abc187_f/judge.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"time_limit_s": 3.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": 44,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc187_f/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc187_f",
|
| 3 |
+
"contest": "abc187",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "F",
|
| 6 |
+
"title": "Close Group",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc187/tasks/abc187_f",
|
| 8 |
+
"difficulty": 1895,
|
| 9 |
+
"score": 600,
|
| 10 |
+
"time_limit_sec": 3.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 44,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc187/editorial/502",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1500"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc187_f/solution.cpp
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc187_f: F - Close Group
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc187/editorial/502
|
| 3 |
+
// Judged AC on all 44 tests, max 0.24 s of the 3 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, M;
|
| 13 |
+
cin >> N >> M;
|
| 14 |
+
vector<int> adj(N, 0);
|
| 15 |
+
for (int i = 0; i < M; ++i) {
|
| 16 |
+
int a, b;
|
| 17 |
+
cin >> a >> b;
|
| 18 |
+
--a;
|
| 19 |
+
--b;
|
| 20 |
+
adj[a] |= 1 << b;
|
| 21 |
+
adj[b] |= 1 << a;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
int size = 1 << N;
|
| 25 |
+
vector<unsigned char> clique(size, false);
|
| 26 |
+
vector<int> dp(size, 0);
|
| 27 |
+
clique[0] = true;
|
| 28 |
+
|
| 29 |
+
for (int s = 1; s < size; ++s) {
|
| 30 |
+
int v = __builtin_ctz(static_cast<unsigned>(s));
|
| 31 |
+
int rest = s ^ (1 << v);
|
| 32 |
+
clique[s] = clique[rest] && ((adj[v] & rest) == rest);
|
| 33 |
+
if (clique[s]) {
|
| 34 |
+
dp[s] = 1;
|
| 35 |
+
continue;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
int best = dp[rest] + 1;
|
| 39 |
+
// The two sides of a partition are interchangeable.
|
| 40 |
+
for (int t = (s - 1) & s; t > (s ^ t) && best > 2;
|
| 41 |
+
t = (t - 1) & s) {
|
| 42 |
+
best = min(best, dp[t] + dp[s ^ t]);
|
| 43 |
+
}
|
| 44 |
+
dp[s] = best;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
cout << dp[size - 1] << '\n';
|
| 48 |
+
return 0;
|
| 49 |
+
}
|
problems/DP-02/Atcoder/abc187_f/statement.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Close Group
|
| 2 |
+
|
| 3 |
+
**Score**: 600 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
Given is a simple undirected graph with \(N\) vertices and \(M\) edges. The vertices are numbered \(1, 2, \dots, N\), and the \(i\)-th edge connects Vertices \(A_i\) and \(B_i\).
|
| 7 |
+
|
| 8 |
+
Find the minimum possible number of connected components in the graph after removing zero or more edges so that the following condition will be satisfied:
|
| 9 |
+
|
| 10 |
+
**Condition**:
|
| 11 |
+
For every pair of vertices \((a, b)\) such that \(1 \le a < b \le N\), if Vertices \(a\) and \(b\) belong to the same connected component, there is an edge that directly connects Vertices \(a\) and \(b\).
|
| 12 |
+
|
| 13 |
+
## Constraints
|
| 14 |
+
- All values in input are integers.
|
| 15 |
+
- \(1 \le N \le 18\)
|
| 16 |
+
- \(0 \le M \le \frac{N(N - 1)}{2}\)
|
| 17 |
+
- \(1 \le A_i < B_i \le N\)
|
| 18 |
+
- \((A_i, B_i) \neq (A_j, B_j)\) for \(i \neq j\).
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## Input
|
| 23 |
+
Input is given from Standard Input in the following format:
|
| 24 |
+
```
|
| 25 |
+
N M
|
| 26 |
+
A_1 B_1
|
| 27 |
+
...
|
| 28 |
+
A_M B_M
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Output
|
| 32 |
+
Print the answer.
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
## Sample Input 1
|
| 37 |
+
```
|
| 38 |
+
3 2
|
| 39 |
+
1 2
|
| 40 |
+
1 3
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### Sample Output 1
|
| 44 |
+
```
|
| 45 |
+
2
|
| 46 |
+
```
|
| 47 |
+
Without removing edges, the pair \((2, 3)\) violates the condition.
|
| 48 |
+
Removing one of the edges disconnects Vertices \(2\) and \(3\), satisfying the condition.
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## Sample Input 2
|
| 53 |
+
```
|
| 54 |
+
4 6
|
| 55 |
+
1 2
|
| 56 |
+
1 3
|
| 57 |
+
1 4
|
| 58 |
+
2 3
|
| 59 |
+
2 4
|
| 60 |
+
3 4
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
### Sample Output 2
|
| 64 |
+
```
|
| 65 |
+
1
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
---
|
| 69 |
+
|
| 70 |
+
## Sample Input 3
|
| 71 |
+
```
|
| 72 |
+
10 11
|
| 73 |
+
9 10
|
| 74 |
+
2 10
|
| 75 |
+
8 9
|
| 76 |
+
3 4
|
| 77 |
+
5 8
|
| 78 |
+
1 8
|
| 79 |
+
5 6
|
| 80 |
+
2 5
|
| 81 |
+
3 6
|
| 82 |
+
6 9
|
| 83 |
+
1 9
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
### Sample Output 3
|
| 87 |
+
```
|
| 88 |
+
5
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
## Sample Input 4
|
| 94 |
+
```
|
| 95 |
+
18 0
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
### Sample Output 4
|
| 99 |
+
```
|
| 100 |
+
18
|
| 101 |
+
```
|
problems/DP-02/Atcoder/abc190_e/editorial.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Magical Ornament (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc190/editorial/691 by en_translator
|
| 4 |
+
|
| 5 |
+
Let us regard it as a graph, where the magical gems are vertices and the pairs of them that can be adjacent are edges.
|
| 6 |
+
|
| 7 |
+
Notice that the constraints of \(K\) is fairly small (\(K ≤ 17\)).
|
| 8 |
+
It can see that this problem is equivalent to the shortest Hamiltonian problem for vertices \(C_1, C_2, \dots,\) and \(C_K\).
|
| 9 |
+
Now let us consider solving this problem efficiently with Dynamic Programming (DP).
|
| 10 |
+
|
| 11 |
+
Let \(C\) be a set \(C = \{C_1, C_2, \dots, C_K\}\), \(S\) be a subset of \(C\), and \(i\) be an element of \(S\); define
|
| 12 |
+
|
| 13 |
+
\(dp[S][i] = {}\)(Minimum length of path that contains \(S\) and ends at \(i\))
|
| 14 |
+
|
| 15 |
+
then the answer for the problem is \(\displaystyle \min_{i}\{dp[C][i]\} + 1\).
|
| 16 |
+
This DP satisfies the following recurrence relations:
|
| 17 |
+
|
| 18 |
+
\(\displaystyle dp[\{i\}][i] = 0\)
|
| 19 |
+
\(\displaystyle dp[S][i] = \min_{j}\{dp[S \setminus \{i\}][j] + \text{dist}(i, j)\}\)
|
| 20 |
+
|
| 21 |
+
where \(\text{dist}(i, j)\) denotes the distance between vertices \(i, j\), so this dp can be calculated from smaller subsets to bigger (as long as \(\text{dist}(i, j)\) is known).
|
| 22 |
+
|
| 23 |
+
As for \(\text{dist}(i, j)\), since \(i, j \in C\), they can be calculated with \(K\) times of BFS, beginning with each of \(C_1, C_2, \dots,\) and \(C_K\).
|
| 24 |
+
|
| 25 |
+
Therefore, the problem can be solved in a total of \(O(2^KK^2+K(N+M))\) time.
|
problems/DP-02/Atcoder/abc190_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-02/Atcoder/abc190_e/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc190_e",
|
| 3 |
+
"contest": "abc190",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "E",
|
| 6 |
+
"title": "Magical Ornament",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc190/tasks/abc190_e",
|
| 8 |
+
"difficulty": 1645,
|
| 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/abc190/editorial/691",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1517"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc190_e/solution.cpp
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc190_e: E - Magical Ornament
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc190/editorial/691
|
| 3 |
+
// Judged AC on all 40 tests, max 0.22 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, M;
|
| 13 |
+
cin >> N >> M;
|
| 14 |
+
vector<vector<int>> graph(N);
|
| 15 |
+
for (int i = 0; i < M; ++i) {
|
| 16 |
+
int a, b;
|
| 17 |
+
cin >> a >> b;
|
| 18 |
+
--a;
|
| 19 |
+
--b;
|
| 20 |
+
graph[a].push_back(b);
|
| 21 |
+
graph[b].push_back(a);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
int K;
|
| 25 |
+
cin >> K;
|
| 26 |
+
vector<int> C(K);
|
| 27 |
+
for (int &c : C) {
|
| 28 |
+
cin >> c;
|
| 29 |
+
--c;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
vector<vector<int>> distance(K, vector<int>(K));
|
| 33 |
+
vector<int> dist(N), que(N);
|
| 34 |
+
for (int i = 0; i < K; ++i) {
|
| 35 |
+
fill(dist.begin(), dist.end(), -1);
|
| 36 |
+
int head = 0, tail = 0;
|
| 37 |
+
dist[C[i]] = 0;
|
| 38 |
+
que[tail++] = C[i];
|
| 39 |
+
while (head < tail) {
|
| 40 |
+
int v = que[head++];
|
| 41 |
+
for (int u : graph[v]) {
|
| 42 |
+
if (dist[u] != -1) continue;
|
| 43 |
+
dist[u] = dist[v] + 1;
|
| 44 |
+
que[tail++] = u;
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
for (int j = 0; j < K; ++j) {
|
| 48 |
+
if (dist[C[j]] == -1) {
|
| 49 |
+
cout << -1 << '\n';
|
| 50 |
+
return 0;
|
| 51 |
+
}
|
| 52 |
+
distance[i][j] = dist[C[j]];
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
const int INF = 1000000000;
|
| 57 |
+
const int states = 1 << K;
|
| 58 |
+
const int full = states - 1;
|
| 59 |
+
vector<int> dp(states * K, INF);
|
| 60 |
+
for (int i = 0; i < K; ++i) dp[(1 << i) * K + i] = 0;
|
| 61 |
+
|
| 62 |
+
for (int mask = 1; mask < states; ++mask) {
|
| 63 |
+
int remaining = full ^ mask;
|
| 64 |
+
for (int bits = mask; bits; bits &= bits - 1) {
|
| 65 |
+
int i = __builtin_ctz((unsigned)bits);
|
| 66 |
+
int current = dp[mask * K + i];
|
| 67 |
+
if (current == INF) continue;
|
| 68 |
+
for (int next = remaining; next; next &= next - 1) {
|
| 69 |
+
int j = __builtin_ctz((unsigned)next);
|
| 70 |
+
int &target = dp[(mask | (1 << j)) * K + j];
|
| 71 |
+
target = min(target, current + distance[i][j]);
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
int answer = INF;
|
| 77 |
+
for (int i = 0; i < K; ++i) answer = min(answer, dp[full * K + i]);
|
| 78 |
+
cout << answer + 1 << '\n';
|
| 79 |
+
return 0;
|
| 80 |
+
}
|
problems/DP-02/Atcoder/abc190_e/statement.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Magical Ornament
|
| 2 |
+
|
| 3 |
+
**Score :** 500 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
There are `N` kinds of magical gems, numbered `1, 2, \ldots, N`, distributed in the AtCoder Kingdom.
|
| 7 |
+
Takahashi is trying to make an ornament by arranging gems in a row.
|
| 8 |
+
For some pairs of gems, we can put the two gems next to each other; for other pairs, we cannot. We have `M` pairs for which the two gems can be adjacent: (Gem `A_1`, Gem `B_1`), (Gem `A_2`, Gem `B_2`), `\ldots`, (Gem `A_M`, Gem `B_M`). For the other pairs, the two gems cannot be adjacent. (Order does not matter in these pairs.)
|
| 9 |
+
Determine whether it is possible to form a sequence of gems that has one or more gems of each of the kinds `C_1, C_2, \dots, C_K`. If the answer is yes, find the minimum number of stones needed to form such a sequence.
|
| 10 |
+
|
| 11 |
+
## Constraints
|
| 12 |
+
- All values in input are integers.
|
| 13 |
+
- `1 ≤ N ≤ 10^5`
|
| 14 |
+
- `0 ≤ M ≤ 10^5`
|
| 15 |
+
- `1 ≤ A_i < B_i ≤ N`
|
| 16 |
+
- If `i ≠ j`, `(A_i, B_i) ≠ (A_j, B_j)`.
|
| 17 |
+
- `1 ≤ K ≤ 17`
|
| 18 |
+
- `1 ≤ C_1 < C_2 < \dots < C_K ≤ N`
|
| 19 |
+
|
| 20 |
+
## Input
|
| 21 |
+
Input is given from Standard Input in the following format:
|
| 22 |
+
```
|
| 23 |
+
N M
|
| 24 |
+
A_1 B_1
|
| 25 |
+
A_2 B_2
|
| 26 |
+
...
|
| 27 |
+
A_M B_M
|
| 28 |
+
K
|
| 29 |
+
C_1 C_2 ... C_K
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## Output
|
| 33 |
+
Print the minimum number of stones needed to form a sequence of gems that has one or more gems of each of the kinds `C_1, C_2, \dots, C_K`.
|
| 34 |
+
If such a sequence cannot be formed, print `-1` instead.
|
| 35 |
+
|
| 36 |
+
## Sample Input 1
|
| 37 |
+
```
|
| 38 |
+
4 3
|
| 39 |
+
1 4
|
| 40 |
+
2 4
|
| 41 |
+
3 4
|
| 42 |
+
3
|
| 43 |
+
1 2 3
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
## Sample Output 1
|
| 47 |
+
```
|
| 48 |
+
5
|
| 49 |
+
```
|
| 50 |
+
For example, by arranging the gems in the order `[1, 4, 2, 4, 3]`, we can form a sequence of length `5` with Gems `1, 2, 3`.
|
| 51 |
+
|
| 52 |
+
## Sample Input 2
|
| 53 |
+
```
|
| 54 |
+
4 3
|
| 55 |
+
1 4
|
| 56 |
+
2 4
|
| 57 |
+
1 2
|
| 58 |
+
3
|
| 59 |
+
1 2 3
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## Sample Output 2
|
| 63 |
+
```
|
| 64 |
+
-1
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
## Sample Input 3
|
| 68 |
+
```
|
| 69 |
+
10 10
|
| 70 |
+
3 9
|
| 71 |
+
3 8
|
| 72 |
+
8 10
|
| 73 |
+
2 10
|
| 74 |
+
5 8
|
| 75 |
+
6 8
|
| 76 |
+
5 7
|
| 77 |
+
6 7
|
| 78 |
+
1 6
|
| 79 |
+
2 4
|
| 80 |
+
4
|
| 81 |
+
1 2 7 9
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
## Sample Output 3
|
| 85 |
+
```
|
| 86 |
+
11
|
| 87 |
+
```
|
| 88 |
+
For example, by arranging the gems in the order `[1, 6, 7, 5, 8, 3, 9, 3, 8, 10, 2]`, we can form a sequence of length `11` with Gems `1, 2, 7, 9`.
|
problems/DP-02/Atcoder/abc192_f/editorial.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Potion (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc192/editorial/763 by en_translator
|
| 4 |
+
|
| 5 |
+
We brute force through how many materials to mix.
|
| 6 |
+
|
| 7 |
+
Due to the constraint of \(\sum A_i \leq X\), when the number of materials \(C\) to mix is fixed, it is sufficient to solve the problem “what is the maximum of sum of exactly \(C\) elements chosen from \(A_i\), such that the sum is equal to \(X\) under \(\bmod C\)?”
|
| 8 |
+
|
| 9 |
+
This can be calculated in a total of \(O(NC^2)\) time with such DP that
|
| 10 |
+
|
| 11 |
+
\(dp[i][j][k]=\) the maximum of sum of \(j\) elements chosen from the first \(i\) materials, such that the sum is equal to \(k\) \(\bmod C\),
|
| 12 |
+
|
| 13 |
+
so the answer can be calculated in a total of \(O(N^4)\) time.
|
problems/DP-02/Atcoder/abc192_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": 34,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc192_f/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc192_f",
|
| 3 |
+
"contest": "abc192",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "F",
|
| 6 |
+
"title": "Potion",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc192/tasks/abc192_f",
|
| 8 |
+
"difficulty": 1783,
|
| 9 |
+
"score": 600,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 34,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc192/editorial/763",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1530"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc192_f/solution.cpp
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc192_f: F - Potion
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc192/editorial/763
|
| 3 |
+
// Judged AC on all 34 tests, max 0.03 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<int> A(N);
|
| 16 |
+
for (int &a : A) cin >> a;
|
| 17 |
+
|
| 18 |
+
long long answer = LLONG_MAX;
|
| 19 |
+
for (int C = 1; C <= N; ++C) {
|
| 20 |
+
vector<vector<long long>> dp(C + 1, vector<long long>(C, -1));
|
| 21 |
+
dp[0][0] = 0;
|
| 22 |
+
for (int i = 0; i < N; ++i) {
|
| 23 |
+
int rem = A[i] % C;
|
| 24 |
+
for (int j = min(i, C - 1); j >= 0; --j) {
|
| 25 |
+
for (int r = 0; r < C; ++r) {
|
| 26 |
+
if (dp[j][r] < 0) continue;
|
| 27 |
+
int nr = r + rem;
|
| 28 |
+
if (nr >= C) nr -= C;
|
| 29 |
+
dp[j + 1][nr] = max(dp[j + 1][nr], dp[j][r] + A[i]);
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
long long sum = dp[C][X % C];
|
| 34 |
+
if (sum >= 0) answer = min(answer, (X - sum) / C);
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
cout << answer << '\n';
|
| 38 |
+
return 0;
|
| 39 |
+
}
|
problems/DP-02/Atcoder/abc192_f/statement.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Potion
|
| 2 |
+
|
| 3 |
+
**Score:** 600 points
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Problem Statement
|
| 8 |
+
There are \(N\) kinds of materials. Material \(i\) has a magic power of \(A_i\).
|
| 9 |
+
Takahashi, the magician, wants to make a potion by choosing one or more kinds from these materials and mixing them.
|
| 10 |
+
At the moment when he makes a potion by mixing \(k\) kinds of materials, the magic power of the potion is the sum of the materials used. Then, every second, its magic power increases by \(k\). Note that the increase of the magic power is a discrete - not continuous - process.
|
| 11 |
+
Takahashi will mix materials just once, at time \(0\). What is the earliest time he can get a potion with a magic power of exactly \(X\)?
|
| 12 |
+
Under the constraints, it can be proved that it is possible to make a potion with a magic power of exactly \(X\).
|
| 13 |
+
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
## Constraints
|
| 17 |
+
- \(1 \leq N \leq 100\)
|
| 18 |
+
- \(1 \leq A_i \leq 10^7\)
|
| 19 |
+
- \(10^9 \leq X \leq 10^{18}\)
|
| 20 |
+
- All values in input are integers.
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
## Input
|
| 25 |
+
Input is given from Standard Input in the following format:
|
| 26 |
+
```
|
| 27 |
+
N X
|
| 28 |
+
A_1 ... A_N
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
## Output
|
| 34 |
+
Print the earliest time he can get a potion with a magic power of exactly \(X\).
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## Sample Input 1
|
| 39 |
+
```
|
| 40 |
+
3 9999999999
|
| 41 |
+
3 6 8
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
## Sample Output 1
|
| 45 |
+
```
|
| 46 |
+
4999999994
|
| 47 |
+
```
|
| 48 |
+
The potion made by mixing Material \(1\) and Material \(3\) has a magic power of \(3+8=11\) at time \(0\), and it increases by \(2\) every second, so it will be \(9999999999\) at time \(4999999994\), which is the earliest possible time.
|
| 49 |
+
The potion made by mixing all the materials \(1, 2, 3\) will have a magic power of \(9999999998\) at time \(3333333327\) and \(10000000001\) at time \(3333333328\), so it will never be exactly \(9999999999\).
|
| 50 |
+
|
| 51 |
+
---
|
| 52 |
+
|
| 53 |
+
## Sample Input 2
|
| 54 |
+
```
|
| 55 |
+
1 1000000000000000000
|
| 56 |
+
1
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
## Sample Output 2
|
| 60 |
+
```
|
| 61 |
+
999999999999999999
|
| 62 |
+
```
|
problems/DP-02/Atcoder/abc195_f/editorial.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Coprime Present (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc195/editorial/896 by en_translator
|
| 4 |
+
|
| 5 |
+
For any distinct pair of integers \(n > m\) between \(A\) through \(B\) (inclusive), \(GCD(n,m)=GCD(n-m,m)\leq n-m \leq B-A\). So, if \(n\) and \(m\) are not pairwise coprime, then both \(n\) and \(m\) are a multiple of an integer between \(2\) and \(B-A\), inclusive.
|
| 6 |
+
|
| 7 |
+
Therefore, a set satisfies the conditions if and only if, for any integer \(d\) between \(2\) and \(B-A\), inclusive, the set contains at most one multiple of \(d\). Also, we can assume that \(d\) is a prime.
|
| 8 |
+
|
| 9 |
+
One can find the answer with a bit DP, where the combination of primes are retained as the state, in a total of \(O(N2^{\pi(N)})\) time, where \(N=B-A\) and \(\pi(N)\) denotes the number of primes less than or equal to \(N\). There are at most \(20\) primes that are less than \(72\) (the maximum of \(B-A\)), so this solution is fast enough.
|
problems/DP-02/Atcoder/abc195_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": 36,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc195_f/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc195_f",
|
| 3 |
+
"contest": "abc195",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "F",
|
| 6 |
+
"title": "Coprime Present",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc195/tasks/abc195_f",
|
| 8 |
+
"difficulty": 2068,
|
| 9 |
+
"score": 600,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 36,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc195/editorial/896",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1548"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc195_f/solution.cpp
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc195_f: F - Coprime Present
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc195/editorial/896
|
| 3 |
+
// Judged AC on all 36 tests, max 0.05 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 |
+
long long A, B;
|
| 13 |
+
cin >> A >> B;
|
| 14 |
+
int n = static_cast<int>(B - A);
|
| 15 |
+
|
| 16 |
+
vector<int> primes;
|
| 17 |
+
for (int x = 2; x <= n; ++x) {
|
| 18 |
+
bool prime = true;
|
| 19 |
+
for (int d = 2; d * d <= x; ++d) {
|
| 20 |
+
if (x % d == 0) {
|
| 21 |
+
prime = false;
|
| 22 |
+
break;
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
if (prime) primes.push_back(x);
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
int states = 1 << primes.size();
|
| 29 |
+
vector<long long> dp(states, 0);
|
| 30 |
+
dp[0] = 1;
|
| 31 |
+
|
| 32 |
+
for (long long x = A; x <= B; ++x) {
|
| 33 |
+
int mask = 0;
|
| 34 |
+
for (int i = 0; i < static_cast<int>(primes.size()); ++i) {
|
| 35 |
+
if (x % primes[i] == 0) mask |= 1 << i;
|
| 36 |
+
}
|
| 37 |
+
for (int s = states - 1; s >= 0; --s) {
|
| 38 |
+
if ((s & mask) == 0) {
|
| 39 |
+
dp[s | mask] += dp[s];
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
long long answer = 0;
|
| 45 |
+
for (long long count : dp) answer += count;
|
| 46 |
+
cout << answer << '\n';
|
| 47 |
+
return 0;
|
| 48 |
+
}
|
problems/DP-02/Atcoder/abc195_f/statement.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# F - Coprime Present
|
| 2 |
+
|
| 3 |
+
**Score**: 600 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
|
| 7 |
+
You have \( B-A+1 \) cards: for each integer from \( A \) through \( B \), you have one card with that integer written on it.
|
| 8 |
+
You will give some of them (possibly none) to your pet, Snuke.
|
| 9 |
+
Snuke will be happy if, for every pair of different cards, the numbers written on them are pairwise coprime; otherwise, he will be sad.
|
| 10 |
+
How many sets of cards will make Snuke happy?
|
| 11 |
+
|
| 12 |
+
## Constraints
|
| 13 |
+
|
| 14 |
+
- \( 1 \leq A \leq B \leq 10^{18} \)
|
| 15 |
+
- \( B-A \leq 72 \)
|
| 16 |
+
- All values in input are integers.
|
| 17 |
+
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
## Input
|
| 21 |
+
|
| 22 |
+
Input is given from Standard Input in the following format:
|
| 23 |
+
|
| 24 |
+
```
|
| 25 |
+
A B
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## Output
|
| 29 |
+
|
| 30 |
+
Print the number of sets of cards that will make Snuke happy.
|
| 31 |
+
**The constraints guarantee that the answer is less than \( 2^{63} \).**
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
## Sample Input 1
|
| 36 |
+
|
| 37 |
+
```
|
| 38 |
+
2 4
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
### Sample Output 1
|
| 42 |
+
|
| 43 |
+
```
|
| 44 |
+
6
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
You have three cards with \( 2 \), \( 3 \), and \( 4 \) written on them. The following six sets of cards will make Snuke happy:
|
| 48 |
+
- \(\{\}\)
|
| 49 |
+
- \(\{2\}\)
|
| 50 |
+
- \(\{3\}\)
|
| 51 |
+
- \(\{4\}\)
|
| 52 |
+
- \(\{2,3\}\)
|
| 53 |
+
- \(\{3,4\}\)
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## Sample Input 2
|
| 58 |
+
|
| 59 |
+
```
|
| 60 |
+
1 1
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
### Sample Output 2
|
| 64 |
+
|
| 65 |
+
```
|
| 66 |
+
2
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
The following two sets of cards will make Snuke happy:
|
| 70 |
+
- \(\{\}\)
|
| 71 |
+
- \(\{1\}\)
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
## Sample Input 3
|
| 76 |
+
|
| 77 |
+
```
|
| 78 |
+
123456789000 123456789050
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
### Sample Output 3
|
| 82 |
+
|
| 83 |
+
```
|
| 84 |
+
2125824
|
| 85 |
+
```
|
problems/DP-02/Atcoder/abc199_e/editorial.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Permutation (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc199/editorial/1170 by en_translator
|
| 4 |
+
|
| 5 |
+
Consider the following DP (Dynamic Programming).
|
| 6 |
+
|
| 7 |
+
\(\mathrm{DP}_S\) (\(S\) is a subset of \(\{1, 2, 3, \dots, N\}\)): the number of permutations where the set first \(|S|\) elements is equal to \(S\) and that satisfies all the given conditions such that \(X_i \le |S|\)
|
| 8 |
+
|
| 9 |
+
Let us consider the transition from an arbitrary \(\mathrm{DP}_S\) to \(\mathrm{DP}_{S \cup \{x\}}\), where the \(|S|+1\)-th element of \(a\) is determined to \(x\). Here, the transition is valid if:
|
| 10 |
+
|
| 11 |
+
- \(x \notin S\), and
|
| 12 |
+
- For all \(i\) such that \(X_i = |S \cup \{x\}|\), \(S \cup \{x\}\) contains at most \(Z_i\) numbers less than or equal to \(Y_i\).
|
| 13 |
+
|
| 14 |
+
The answer for the problem is \(\mathrm{DP}_{\{1, 2, 3, \dots, N\}}\).
|
| 15 |
+
Since there are \(2^N\) states, and each transition takes an \(O(N)\) time, it can be solved in a total of \(O(2^N N)\) time.
|
| 16 |
+
|
| 17 |
+
For implementation, we can regard \(S\) as an integer \(\displaystyle \sum_{i \in S} 2^{(i - 1)}\) so that it can be used as an index of an array. Most languages is equipped with a bit operator which enables to obtain an integer corresponding to \(S \cup \{x\}\) from an integer corresponding to \(S\), which simplifies the implementation.
|
| 18 |
+
Such a technique of implementation is so general that the DP where the set is used as an index is called bitmask DP.
|
problems/DP-02/Atcoder/abc199_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": 29,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc199_e/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc199_e",
|
| 3 |
+
"contest": "abc199",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "E",
|
| 6 |
+
"title": "Permutation",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc199/tasks/abc199_e",
|
| 8 |
+
"difficulty": 1814,
|
| 9 |
+
"score": 500,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 29,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc199/editorial/1170",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1571"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc199_e/solution.cpp
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc199_e: E - Permutation
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc199/editorial/1170
|
| 3 |
+
// Judged AC on all 29 tests, max 0.01 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, M;
|
| 13 |
+
cin >> N >> M;
|
| 14 |
+
vector<vector<pair<int, int>>> constraints(N + 1);
|
| 15 |
+
for (int i = 0; i < M; ++i) {
|
| 16 |
+
int X, Y, Z;
|
| 17 |
+
cin >> X >> Y >> Z;
|
| 18 |
+
constraints[X].push_back({(1 << Y) - 1, Z});
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
int total = 1 << N;
|
| 22 |
+
vector<long long> dp(total, 0);
|
| 23 |
+
dp[0] = 1;
|
| 24 |
+
for (int mask = 1; mask < total; ++mask) {
|
| 25 |
+
int length = __builtin_popcount((unsigned)mask);
|
| 26 |
+
bool valid = true;
|
| 27 |
+
for (auto [low_mask, limit] : constraints[length]) {
|
| 28 |
+
if (__builtin_popcount((unsigned)(mask & low_mask)) > limit) {
|
| 29 |
+
valid = false;
|
| 30 |
+
break;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
if (!valid) continue;
|
| 34 |
+
|
| 35 |
+
for (int bits = mask; bits; bits &= bits - 1) {
|
| 36 |
+
int bit = bits & -bits;
|
| 37 |
+
dp[mask] += dp[mask ^ bit];
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
cout << dp[total - 1] << '\n';
|
| 42 |
+
return 0;
|
| 43 |
+
}
|
problems/DP-02/Atcoder/abc199_e/statement.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Permutation
|
| 2 |
+
|
| 3 |
+
**Score**: 500 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
Print the number of sequences `a` that are permutations of `(1, 2, 3, \dots, N)` and satisfy the following condition:
|
| 7 |
+
- for every integer `i` such that `1 ≤ i ≤ M`, at most `Z_i` numbers among `a_1, a_2, a_3, \dots, a_{X_i}` are less than or equal to `Y_i`.
|
| 8 |
+
|
| 9 |
+
## Constraints
|
| 10 |
+
- `2 ≤ N ≤ 18`
|
| 11 |
+
- `0 ≤ M ≤ 100`
|
| 12 |
+
- `1 ≤ X_i < N`
|
| 13 |
+
- `1 ≤ Y_i < N`
|
| 14 |
+
- `0 ≤ Z_i < N`
|
| 15 |
+
- All values in input are integers.
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## Input
|
| 20 |
+
Input is given from Standard Input in the following format:
|
| 21 |
+
```
|
| 22 |
+
N M
|
| 23 |
+
X_1 Y_1 Z_1
|
| 24 |
+
X_2 Y_2 Z_2
|
| 25 |
+
X_3 Y_3 Z_3
|
| 26 |
+
...
|
| 27 |
+
X_M Y_M Z_M
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## Output
|
| 31 |
+
Print the answer.
|
| 32 |
+
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
## Sample Input 1
|
| 36 |
+
```
|
| 37 |
+
3 1
|
| 38 |
+
2 2 1
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
### Sample Output 1
|
| 42 |
+
```
|
| 43 |
+
4
|
| 44 |
+
```
|
| 45 |
+
The four sequences `a` satisfying the condition are:
|
| 46 |
+
- `(1, 3, 2)`
|
| 47 |
+
- `(2, 3, 1)`
|
| 48 |
+
- `(3, 1, 2)`
|
| 49 |
+
- `(3, 2, 1)`
|
| 50 |
+
|
| 51 |
+
`(1, 2, 3)` and `(2, 1, 3)` violate the condition, since each of them has two numbers less than or equal to `2` among `a_1, a_2`.
|
| 52 |
+
|
| 53 |
+
---
|
| 54 |
+
|
| 55 |
+
## Sample Input 2
|
| 56 |
+
```
|
| 57 |
+
5 2
|
| 58 |
+
3 3 2
|
| 59 |
+
4 4 3
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
### Sample Output 2
|
| 63 |
+
```
|
| 64 |
+
90
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
---
|
| 68 |
+
|
| 69 |
+
## Sample Input 3
|
| 70 |
+
```
|
| 71 |
+
18 0
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### Sample Output 3
|
| 75 |
+
```
|
| 76 |
+
6402373705728000
|
| 77 |
+
```
|
problems/DP-02/Atcoder/abc213_g/editorial.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# G - Connectivity 2 (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc213/editorial/2414 by en_translator
|
| 4 |
+
|
| 5 |
+
This problem is about techniques on combinatorics on **connected graph**.
|
| 6 |
+
As a prerequisite, we do not explain the technique called **bit DP** (DP=Dynamic Programming) in which each integer \(0,1,\dots,2^N-1\) is regarded a subset of \(1,2,\dots,N\).
|
| 7 |
+
|
| 8 |
+
#### Prerequisite: subset enumeration
|
| 9 |
+
|
| 10 |
+
Firstly, the following knowledge is required to solve this problem:
|
| 11 |
+
|
| 12 |
+
For any non-negative integers \(i\) and \(j\), if \(i\&j = j\), let us denote \(j \subseteq i\). (Here, \(\&\) is bitwise logical products.)
|
| 13 |
+
Then, the pairs \((i, j)\) such that \(0 \leq i,j \lt 2^N, j \subseteq i\) can be enumerated in a total of \(\mathrm{O}(3^N)\) time.
|
| 14 |
+
|
| 15 |
+
The equations above make us dizzy, so let us show you the example for \(N = 3\) in a **binary notation**:
|
| 16 |
+
|
| 17 |
+
- \(i = 000_{(2)} \to j = 000_{(2)}\)
|
| 18 |
+
- \(i = 001_{(2)} \to j = 000_{(2)}, 001_{(2)}\)
|
| 19 |
+
- \(i = 110_{(2)} \to j = 000_{(2)}, 010_{(2)},100_{(2)}, 110_{(2)}\)
|
| 20 |
+
|
| 21 |
+
As you can see, \(i\) and \(j\) are in the relationship where “a bit in \(j\) has the value \(1\) only if the corresponding digit in \(i\) has the value \(1\).”
|
| 22 |
+
The counterpart for the sets represented by \(i\) and \(j\) are “all elements in \(j\) are in \(i\),” that is, “\(j\) is a subset of \(i\).”
|
| 23 |
+
Therefore, now we can understand that the enumeration of \((i, j)\) described above is about the algorithm of **enumerating all the subset of \(i\) for every \(i\)**.
|
| 24 |
+
|
| 25 |
+
First, let us count how many pairs \((i,j)\) satisfies the conditions.
|
| 26 |
+
If \(i\) has \(k\) ones in its binary notation, the number of \(j\) satisfying the conditions are \(2^k\); so the number of pairs \((i, j)\) is
|
| 27 |
+
|
| 28 |
+
\[ \sum_{k=0}^N \binom{N}{k} 2^k.\]
|
| 29 |
+
|
| 30 |
+
Here, we can apply binomial theorem to obtain
|
| 31 |
+
|
| 32 |
+
\[ = (2 + 1) ^ N = 3^N.\]
|
| 33 |
+
|
| 34 |
+
Therefore, we have proved that the number of pairs \((i,j)\) are exactly \(3^N\).
|
| 35 |
+
|
| 36 |
+
Now let us actually enumerate \((i,j)\). They can be enumerated in a total of \(\mathrm{O}(4^N)\) with the following naive brute-force algorithm, but spending \(\mathrm{O}(4^N)\) time for iterating through \(\mathrm{O}(3^N)\) elements is not cool.
|
| 37 |
+
|
| 38 |
+
Actually, we can exploit the bit operation as follows to enumerate them in a total of \(\mathrm{O}(3^N)\) time.
|
| 39 |
+
|
| 40 |
+
The key point is (code omitted) in the third line. By taking bitwise and like this, the volume of range of `j` is decreased from \(\mathrm{o}(4^N)\) to \(\mathrm{o}(3^N)\).
|
| 41 |
+
Therefore, we can enumerate the subsets in a total of \(\mathrm{O}(3^N)\) time. We can use this algorithm to solve the problem.
|
| 42 |
+
|
| 43 |
+
### Rephrase what we want to count
|
| 44 |
+
|
| 45 |
+
Now we have enough prerequisite; let us explain the original problem.
|
| 46 |
+
First, the condition “\(1\) and \(k\) are connected” in the original problem seems to be too complex to count. In that case, we want to rephrase the object so that it becomes computable.
|
| 47 |
+
|
| 48 |
+
Let \(V := \lbrace 1,2,\dots,N \rbrace\) be the set of vertices in \(G\). For \(S\) such that \(S \subseteq V\), we define \(f(S)\) and \(g(S)\) as follows:
|
| 49 |
+
|
| 50 |
+
- \(f(S) := \) The number of **connected subgraph** of \(G\) whose vertex set is \(S \subseteq V\)
|
| 51 |
+
- \(g(S) := \) The number of **subgraph** of \(G\) whose vertex set is \(S \subseteq V\)
|
| 52 |
+
|
| 53 |
+
Next, the desired answer can be rephrased in terms of graph theory as “the number of spanning subgraph of \(G\) such that Vertex \(1\) and Vertex \(K\) are connected;” let us denote it by \(C(k)\), and consider how to represent it by \(f(S)\) and \(g(S)\). The cases can be divided by the set of vertices that are connected to \(1\), so we have
|
| 54 |
+
|
| 55 |
+
\[ C(k) = \sum_{\lbrace 1, k\rbrace \subseteq S \subseteq V} f(S) g(V \setminus S).\]
|
| 56 |
+
|
| 57 |
+
(\(V \setminus S\) denotes the set of elements of \(V\) except for those in \(S\).)
|
| 58 |
+
|
| 59 |
+
Therefore, if we can compute \(S \subseteq V\) for all \(f(S), g(S)\), then we can compute \(C(2),\dots,C(N)\) in a total of \(\mathrm{O}(N 2^N)\) with the equation above.
|
| 60 |
+
|
| 61 |
+
### Computing \(f(S)\) and \(g(S)\)
|
| 62 |
+
|
| 63 |
+
Now, how can we compute \(f(S)\) and \(g(S)\)?
|
| 64 |
+
The easier to compute is \(g(S)\). Let \(e\) be the number of edges that connects the vertices in \(S\), then the number of subgraphs is \(2^e\). Thus, let
|
| 65 |
+
|
| 66 |
+
- \(E(S) :=\) the number of edges of \(G\) whose vertices on both ends are included in \(S\),
|
| 67 |
+
|
| 68 |
+
then \(E(S)\) can be computed in a total of \(\mathrm{O}(M)\) time by checking if each edge is included in \(S\). Therefore, for every \(S \subseteq V\), we have found \(g(S)\) in a total of \(\mathrm{O}(M 2^N)\) time.
|
| 69 |
+
|
| 70 |
+
Finally, let us compute \(f(S)\).
|
| 71 |
+
\(f(S)\) can be solved with a DP using inclusion-exclusion principle. (By the way, the DP described below is said to be “exclusion principle” or “exclusion DP” by some people, as only subtraction is used.)
|
| 72 |
+
|
| 73 |
+
\(S = \emptyset\) (\(\emptyset\) denotes an empty set) is trivial, so we assume \(S \neq \emptyset\). \(f(S)\) can be represented by \(g(S)\) as \(f(S) = g(S) - (\) the number of unconnected graph with vertex set \(S\) \()\).
|
| 74 |
+
Let us represent this “unconnected graph” part by \(f\) and \(g\) just as we did for \(C(k)\). Let \(v\) be a vertex in \(S\), then we can divide the cases by the connected component including \(v\) by the following expression. (\(T \subsetneq S\) means \( T \subseteq S\) but \(T \neq S\).)
|
| 75 |
+
|
| 76 |
+
\[ f(S) = g(S) - \sum_{v \in T \subsetneq S} f(T) g(S \setminus T).\]
|
| 77 |
+
|
| 78 |
+
Therefore, we can compute \(f(S)\) in the increasing order, using the algorithm of enumerating the subsets, so that we obtain \(f(S)\) for all \(S \subseteq V\) in a total of \(\mathrm{O}(3^N)\) time.
|
| 79 |
+
|
| 80 |
+
Note that \(g(S)\) can be computed by an algorithm called **Fast Zeta Transform** in a total of \(\mathrm{O}(N 2^N)\) time, but for solving Problem G, naive algorithm is enough, so we didn’t introduce it here.
|
| 81 |
+
Fast Zeta Transform is another important DP technique, so if you are interested, search on the Internet for the words like “Fast Zeta Transform” or “Sum over Subset DP”.
|
| 82 |
+
Also, a faster algorithm of finding \(f(S)\) in a total of \(\mathrm{O}(N^2 2^N)\) time is known, but we will omit it.
|
| 83 |
+
|
| 84 |
+
### Similar problem
|
| 85 |
+
|
| 86 |
+
- [AOJ2345: Network Reliability](https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2345)
|
problems/DP-02/Atcoder/abc213_g/judge.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"time_limit_s": 3.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": 36,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc213_g/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc213_g",
|
| 3 |
+
"contest": "abc213",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "G",
|
| 6 |
+
"title": "Connectivity 2",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc213/tasks/abc213_g",
|
| 8 |
+
"difficulty": 2663,
|
| 9 |
+
"score": 600,
|
| 10 |
+
"time_limit_sec": 3.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 36,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc213/editorial/2414",
|
| 14 |
+
"code_source": "model",
|
| 15 |
+
"pid": "A1659"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc213_g/solution.cpp
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc213_g: G - Connectivity 2
|
| 2 |
+
// Written by a model (gpt-6-astra) from the official editorial, https://atcoder.jp/contests/abc213/editorial/2414
|
| 3 |
+
// Judged AC on all 36 tests, max 0.14 s of the 3 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, M;
|
| 15 |
+
cin >> N >> M;
|
| 16 |
+
vector<int> adj(N, 0);
|
| 17 |
+
for (int i = 0; i < M; ++i) {
|
| 18 |
+
int a, b;
|
| 19 |
+
cin >> a >> b;
|
| 20 |
+
--a;
|
| 21 |
+
--b;
|
| 22 |
+
adj[a] |= 1 << b;
|
| 23 |
+
adj[b] |= 1 << a;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
vector<mint> pow2(M + 1);
|
| 27 |
+
pow2[0] = 1;
|
| 28 |
+
for (int i = 1; i <= M; ++i) pow2[i] = pow2[i - 1] * 2;
|
| 29 |
+
|
| 30 |
+
const int size = 1 << N;
|
| 31 |
+
vector<int> edges(size, 0);
|
| 32 |
+
vector<mint> g(size), f(size);
|
| 33 |
+
g[0] = 1;
|
| 34 |
+
for (int s = 1; s < size; ++s) {
|
| 35 |
+
int v = __builtin_ctz(static_cast<unsigned>(s));
|
| 36 |
+
int rest = s & (s - 1);
|
| 37 |
+
edges[s] = edges[rest] + __builtin_popcount(static_cast<unsigned>(adj[v] & rest));
|
| 38 |
+
g[s] = pow2[edges[s]];
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
for (int s = 1; s < size; ++s) {
|
| 42 |
+
f[s] = g[s];
|
| 43 |
+
int rest = s & (s - 1);
|
| 44 |
+
// The least significant vertex belongs to its connected component.
|
| 45 |
+
// u is the nonempty set of vertices outside that component.
|
| 46 |
+
for (int u = rest; u; u = (u - 1) & rest) {
|
| 47 |
+
f[s] -= f[s ^ u] * g[u];
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
const int full = size - 1;
|
| 52 |
+
vector<mint> answer(N);
|
| 53 |
+
for (int s = 1; s < size; s += 2) {
|
| 54 |
+
mint ways = f[s] * g[full ^ s];
|
| 55 |
+
for (int t = s ^ 1; t; t &= t - 1) {
|
| 56 |
+
int k = __builtin_ctz(static_cast<unsigned>(t));
|
| 57 |
+
answer[k] += ways;
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
for (int k = 1; k < N; ++k) {
|
| 62 |
+
cout << answer[k].val() << '\n';
|
| 63 |
+
}
|
| 64 |
+
return 0;
|
| 65 |
+
}
|
problems/DP-02/Atcoder/abc213_g/statement.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# G - Connectivity 2
|
| 2 |
+
|
| 3 |
+
**Score :** 600 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
Given is a simple undirected graph \( G \) with \( N \) vertices and \( M \) edges. The vertices are numbered \( 1,2,\dots,N \), the edges are numbered \( 1,2,\dots,M \), and Edge \( i \) connects Vertex \( a_i \) and Vertex \( b_i \).
|
| 7 |
+
Consider removing zero or more edges from \( G \) to get a new graph \( H \). There are \( 2^M \) graphs that we can get as \( H \). Among them, find the number of such graphs that Vertex \( 1 \) and Vertex \( k \) are directly or indirectly connected, for each integer \( k \) such that \( 2 \leq k \leq N \).
|
| 8 |
+
Since the counts may be enormous, print them modulo \( 998244353 \).
|
| 9 |
+
|
| 10 |
+
## Constraints
|
| 11 |
+
- \( 2 \leq N \leq 17 \)
|
| 12 |
+
- \( 0 \leq M \leq \frac{N(N-1)}{2} \)
|
| 13 |
+
- \( 1 \leq a_i < b_i \leq N \)
|
| 14 |
+
- \( (a_i, b_i) \neq (a_j, b_j) \) if \( i \neq j \).
|
| 15 |
+
- All values in input are integers.
|
| 16 |
+
|
| 17 |
+
## Input
|
| 18 |
+
Input is given from Standard Input in the following format:
|
| 19 |
+
```
|
| 20 |
+
N M
|
| 21 |
+
a_1 b_1
|
| 22 |
+
...
|
| 23 |
+
a_M b_M
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
## Output
|
| 27 |
+
Print \( N-1 \) lines. The \( i \)-th line should contain the answer for \( k = i + 1 \).
|
| 28 |
+
|
| 29 |
+
## Sample Input 1
|
| 30 |
+
```
|
| 31 |
+
3 2
|
| 32 |
+
1 2
|
| 33 |
+
2 3
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
## Sample Output 1
|
| 37 |
+
```
|
| 38 |
+
2
|
| 39 |
+
1
|
| 40 |
+
```
|
| 41 |
+
We can get the following graphs as \( H \).
|
| 42 |
+
- The graph with no edges. Vertex \( 1 \) is disconnected from any other vertex.
|
| 43 |
+
- The graph with only the edge connecting Vertex \( 1 \) and \( 2 \). Vertex \( 2 \) is reachable from Vertex \( 1 \).
|
| 44 |
+
- The graph with only the edge connecting Vertex \( 2 \) and Vertex \( 3 \). Vertex \( 1 \) is disconnected from any other vertex.
|
| 45 |
+
- The graph with both edges. Vertex \( 2 \) and Vertex \( 3 \) are reachable from Vertex \( 1 \).
|
| 46 |
+
|
| 47 |
+
## Sample Input 2
|
| 48 |
+
```
|
| 49 |
+
5 6
|
| 50 |
+
1 2
|
| 51 |
+
1 4
|
| 52 |
+
1 5
|
| 53 |
+
2 3
|
| 54 |
+
2 5
|
| 55 |
+
3 4
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
## Sample Output 2
|
| 59 |
+
```
|
| 60 |
+
43
|
| 61 |
+
31
|
| 62 |
+
37
|
| 63 |
+
41
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
## Sample Input 3
|
| 67 |
+
```
|
| 68 |
+
2 0
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Sample Output 3
|
| 72 |
+
```
|
| 73 |
+
0
|
| 74 |
+
```
|
problems/DP-02/Atcoder/abc215_e/editorial.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Chain Contestant (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc215/editorial/2514 by en_translator
|
| 4 |
+
|
| 5 |
+
To come to the point, the following bitDP is possible.
|
| 6 |
+
dp[for the first \(i\) contests][set of contests participated so far][the kind of last participated contest]={the number of possible combinations}
|
| 7 |
+
The transition of DP is as follows.
|
| 8 |
+
|
| 9 |
+
Let \(x\) be the kind of \(i\)-th contest.
|
| 10 |
+
|
| 11 |
+
- For every set \(U\) and every kind of contest \(j\), add \(dp[i-1][U][j]\) to \(dp[i][U][j]\) (corresponding to when he does not participate in the \(i\)-th contest)
|
| 12 |
+
|
| 13 |
+
- If \(j=x\), add \(dp[i-1][U][j]\) to it too (corresponding to when he participate in the \(i\)-th contest, which is the same kind of contest to that he participated last time)
|
| 14 |
+
- For every set \(V\) that does not include \(x\) and every kind of contest \(j\), add \(dp[i-1][V][j]\) to \(dp[i][V'][x]\), where \(V'\) is \(V\) with \(x\) added (corresponding to when he participate in the \(i\)-th contest, which is the different kind of contest to that he participated last time)
|
| 15 |
+
- Add \(1\) to \(dp[i][W][x]\), where \(W\) is a set only containing \(x\) (corresponding to the case where the first contest he participate in is the \(i\)-th one)
|
| 16 |
+
|
| 17 |
+
For the bitDP with the transitions above, the final solution is the sum of \(dp[N][U][j]\) over every set \(U\) and every kind of contest \(j\).
|
| 18 |
+
The total computational complexity is \(O(2^KNK)\)(\(K=10\), the number of contest types), which fits in the time limit.
|
problems/DP-02/Atcoder/abc215_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": 22,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc215_e/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc215_e",
|
| 3 |
+
"contest": "abc215",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "E",
|
| 6 |
+
"title": "Chain Contestant",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc215/tasks/abc215_e",
|
| 8 |
+
"difficulty": 1413,
|
| 9 |
+
"score": 500,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 22,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc215/editorial/2514",
|
| 14 |
+
"code_source": "editorial",
|
| 15 |
+
"pid": "A1673"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc215_e/solution.cpp
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc215_e: E - Chain Contestant
|
| 2 |
+
// Code from the official editorial, https://atcoder.jp/contests/abc215/editorial/2514 (by en_translator)
|
| 3 |
+
// Judged AC on all 22 tests, max 0.03 s of the 2 s limit.
|
| 4 |
+
|
| 5 |
+
#include<bits/stdc++.h>
|
| 6 |
+
#define mod 998244353
|
| 7 |
+
|
| 8 |
+
using namespace std;
|
| 9 |
+
|
| 10 |
+
int main(){
|
| 11 |
+
int n;
|
| 12 |
+
string s;
|
| 13 |
+
cin >> n >> s;
|
| 14 |
+
long long dp[1024][1024][10]={0};
|
| 15 |
+
for(int i=1;i<=n;i++){
|
| 16 |
+
int x=s[i-1]-'A';
|
| 17 |
+
for(int u=0;u<1024;u++){
|
| 18 |
+
for(int j=0;j<10;j++){
|
| 19 |
+
dp[i][u][j]=dp[i-1][u][j];
|
| 20 |
+
if(j==x){
|
| 21 |
+
dp[i][u][j]+=dp[i-1][u][j];
|
| 22 |
+
dp[i][u][j]%=mod;
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
for(int v=0;v<1024;v++){
|
| 27 |
+
if(v&(1<<x)){continue;}
|
| 28 |
+
for(int j=0;j<10;j++){
|
| 29 |
+
dp[i][v|(1<<x)][x]+=dp[i-1][v][j];
|
| 30 |
+
dp[i][v|(1<<x)][x]%=mod;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
dp[i][(1<<x)][x]++;
|
| 34 |
+
dp[i][(1<<x)][x]%=mod;
|
| 35 |
+
}
|
| 36 |
+
long long res=0;
|
| 37 |
+
for(int u=0;u<1024;u++){
|
| 38 |
+
for(int j=0;j<10;j++){res+=dp[n][u][j];res%=mod;}
|
| 39 |
+
}
|
| 40 |
+
cout << res << '\n';
|
| 41 |
+
return 0;
|
| 42 |
+
}
|
problems/DP-02/Atcoder/abc215_e/statement.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Chain Contestant
|
| 2 |
+
|
| 3 |
+
**Score:** 500 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
AtCoder in another world holds 10 types of contests called AAC, ..., AJC. There will be N contests from now on.
|
| 7 |
+
The types of these N contests are given to you as a string S: if the i-th character of S is x, the i-th contest will be A x C.
|
| 8 |
+
AtCoDeer will choose and participate in one or more contests from the N so that the following condition is satisfied:
|
| 9 |
+
|
| 10 |
+
- In the sequence of contests he will participate in, the contests of the same type are consecutive.
|
| 11 |
+
- Formally, when AtCoDeer participates in x contests and the i-th of them is of type T_i, for every triple of integers (i,j,k) such that 1 ≤ i < j < k ≤ x, T_i=T_j must hold if T_i=T_k.
|
| 12 |
+
|
| 13 |
+
Find the number of ways for AtCoDeer to choose contests to participate in, modulo 998244353.
|
| 14 |
+
Two ways to choose contests are considered different when there is a contest c such that AtCoDeer participates in c in one way but not in the other.
|
| 15 |
+
|
| 16 |
+
## Constraints
|
| 17 |
+
- \( 1 \le N \le 1000 \)
|
| 18 |
+
- \( |S|=N \)
|
| 19 |
+
- S consists of uppercase English letters from A through J.
|
| 20 |
+
|
| 21 |
+
## Input
|
| 22 |
+
Input is given from Standard Input in the following format:
|
| 23 |
+
```
|
| 24 |
+
N
|
| 25 |
+
S
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## Output
|
| 29 |
+
Print the answer as an integer.
|
| 30 |
+
|
| 31 |
+
## Sample Input 1
|
| 32 |
+
```
|
| 33 |
+
4
|
| 34 |
+
BGBH
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
## Sample Output 1
|
| 38 |
+
```
|
| 39 |
+
13
|
| 40 |
+
```
|
| 41 |
+
For example, participating in the 1-st and 3-rd contests is valid, and so is participating in the 2-nd and 4-th contests.
|
| 42 |
+
On the other hand, participating in the 1-st, 2-nd, 3-rd, and 4-th contests is invalid, since the participations in ABCs are not consecutive, violating the condition for the triple (i,j,k)=(1,2,3).
|
| 43 |
+
Additionally, it is not allowed to participate in zero contests.
|
| 44 |
+
In total, there are 13 valid ways to participate in some contests.
|
| 45 |
+
|
| 46 |
+
## Sample Input 2
|
| 47 |
+
```
|
| 48 |
+
100
|
| 49 |
+
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBIEIJEIJIJCGCCFGIEBIHFCGFBFAEJIEJAJJHHEBBBJJJGJJJCCCBAAADCEHIIFEHHBGF
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
## Sample Output 2
|
| 53 |
+
```
|
| 54 |
+
330219020
|
| 55 |
+
```
|
| 56 |
+
Be sure to find the count modulo 998244353.
|
problems/DP-02/Atcoder/abc221_h/editorial.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# H - Count Multiset (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc221/editorial/2728 by en_translator
|
| 4 |
+
|
| 5 |
+
Instead of counting the number of multisets \(A\) of size \(k\), we will count the number of monotonically non-decreasing integer sequences \(A'=(A'_1,A'_2,\ldots,A'_k)\) of length \(k\).
|
| 6 |
+
|
| 7 |
+
Additionally, for more clarity, we will consider counting the number of progressions of differences, that is, sequences \(B=(B_1,B_2,\ldots,B_k)\) of length \(k\) defined by the following recurrence relations.
|
| 8 |
+
|
| 9 |
+
- \(B_1=A'_1\)
|
| 10 |
+
- \(B_i=A'_i-A'_{i-1}\ (2 \leq i \leq k)\)
|
| 11 |
+
|
| 12 |
+
Then the problem can be rephrased as follows.
|
| 13 |
+
|
| 14 |
+
For each \(k=1,2,\ldots,N\), count the number of sequences of non-negative integers satisfying the following three conditions:
|
| 15 |
+
|
| 16 |
+
- \(\sum_{i=1}^{k} B_i \times (k-i-1) = N\)
|
| 17 |
+
- There are no \(M\) or more consecutive \(0\)’s; i.e. there does not exist an integer \(i\) such that \(B_i=B_{i+1}=\cdots=B_{i+M-1}=0\)
|
| 18 |
+
- \(B_1\) is positive
|
| 19 |
+
|
| 20 |
+
Now we reverse the indices of \(B\). Then the restatement above is equivalent to what follows.
|
| 21 |
+
|
| 22 |
+
For each \(k=1,2,\ldots,N\), count the number of sequences of non-negative integers satisfying the following three conditions:
|
| 23 |
+
|
| 24 |
+
- \(\sum_{i=1}^{k} B_i \times i = N\)
|
| 25 |
+
- There are no \(M\) or more consecutive \(0\)’s; i.e. there does not exist an integer \(i\) such that \(B_i=B_{i+1}=\cdots=B_{i+M-1}=0\)
|
| 26 |
+
- \(B_k\) is positive
|
| 27 |
+
|
| 28 |
+
Now this is easy to handle with.
|
| 29 |
+
|
| 30 |
+
For a pair of integer \((x,y)\) satisfying \(0 \leq x,y \leq N\), define \(f(x,y)\) as follows.
|
| 31 |
+
|
| 32 |
+
- The number of sequences of non-negative integers satisfying the following three conditions:
|
| 33 |
+
|
| 34 |
+
- \(\sum_{i=1}^{k} B_i \times (k-i-1) = N\)
|
| 35 |
+
- There are no \(M\) or more consecutive \(0\)’s; i.e. there does not exist an integer \(i\) such that \(B_i=B_{i+1}=\cdots=B_{i+M-1}=0\)
|
| 36 |
+
- \(B_x\) is positive
|
| 37 |
+
|
| 38 |
+
\(f(x,y)\) can be computed from the set of \(f(p,q)\) for \(p \leq x\) and \(q \leq y\), which we can find the values with DP (Dynamic Programming), requiring a complexity of \(O(N^3M)\) when processed naively. However this can be improved with cumulative sums, resulting to a complexity of \(O(N^2)\).
|
| 39 |
+
|
| 40 |
+
Therefore, the problem has been solved.
|
problems/DP-02/Atcoder/abc221_h/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": 52,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc221_h/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc221_h",
|
| 3 |
+
"contest": "abc221",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "H",
|
| 6 |
+
"title": "Count Multiset",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc221/tasks/abc221_h",
|
| 8 |
+
"difficulty": 2793,
|
| 9 |
+
"score": 600,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 52,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc221/editorial/2728",
|
| 14 |
+
"code_source": "editorial",
|
| 15 |
+
"pid": "A1724"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc221_h/solution.cpp
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc221_h: H - Count Multiset
|
| 2 |
+
// Code from the official editorial, https://atcoder.jp/contests/abc221/editorial/2728 (by en_translator)
|
| 3 |
+
// Judged AC on all 52 tests, max 0.16 s of the 2 s limit.
|
| 4 |
+
|
| 5 |
+
#include<bits/stdc++.h>
|
| 6 |
+
using namespace std;
|
| 7 |
+
|
| 8 |
+
const int mod = 998244353;
|
| 9 |
+
|
| 10 |
+
int main(){
|
| 11 |
+
int N,M; cin >> N >> M;
|
| 12 |
+
vector<vector<int>> dp(N+1,vector<int>(N+1));
|
| 13 |
+
dp[0][0] = 1;
|
| 14 |
+
vector<int> sum(N+1);
|
| 15 |
+
sum[0] = 1;
|
| 16 |
+
for(int i=1; i<=N; i++){
|
| 17 |
+
for(int j=i; j<=N; j++){
|
| 18 |
+
dp[i][j] = sum[j-i]+dp[i][j-i];
|
| 19 |
+
dp[i][j] %= mod;
|
| 20 |
+
}
|
| 21 |
+
for(int j=0; j<=N; j++){
|
| 22 |
+
sum[j] += dp[i][j];
|
| 23 |
+
sum[j] %= mod;
|
| 24 |
+
if(M <= i){
|
| 25 |
+
sum[j] -= dp[i-M][j];
|
| 26 |
+
sum[j] %= mod;
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
for(int i=1; i<=N; i++){
|
| 31 |
+
int ans = dp[i][N];
|
| 32 |
+
if(ans < 0) ans += mod;
|
| 33 |
+
cout << ans << endl;
|
| 34 |
+
}
|
| 35 |
+
}
|
problems/DP-02/Atcoder/abc221_h/statement.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# H - Count Multiset
|
| 2 |
+
|
| 3 |
+
**Score:** 600 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
Given are positive integers \( N \) and \( M \).
|
| 7 |
+
For each \( k=1,2,\ldots,N \), find the following number and print it modulo \( 998244353 \).
|
| 8 |
+
|
| 9 |
+
- The number of multisets \( A \) containing \( k \) positive integers that satisfy both of the following conditions:
|
| 10 |
+
- the sum of the elements of \( A \) is \( N \);
|
| 11 |
+
- for every positive integer \( x \), \( A \) contains at most \( M \) occurrences of \( x \).
|
| 12 |
+
|
| 13 |
+
## Constraints
|
| 14 |
+
- \( 1 \leq M \leq N \leq 5000 \)
|
| 15 |
+
- All values in input are integers.
|
| 16 |
+
|
| 17 |
+
## Input
|
| 18 |
+
Input is given from Standard Input in the following format:
|
| 19 |
+
|
| 20 |
+
```
|
| 21 |
+
N M
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## Output
|
| 25 |
+
Print \( N \) lines; the \( i \)-th line \( (1 \leq i \leq N) \) should contain the answer for the case \( k=i \).
|
| 26 |
+
|
| 27 |
+
## Sample Input 1
|
| 28 |
+
```
|
| 29 |
+
4 2
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## Sample Output 1
|
| 33 |
+
```
|
| 34 |
+
1
|
| 35 |
+
2
|
| 36 |
+
1
|
| 37 |
+
0
|
| 38 |
+
```
|
| 39 |
+
- For \( k=1 \), there is one multiset \( A \) that satisfies the conditions: \( \{4\} \).
|
| 40 |
+
- For \( k=2 \), there are two multisets \( A \) that satisfy the conditions: \( \{1,3\} \) and \( \{2,2\} \).
|
| 41 |
+
- For \( k=3 \), there is one multiset \( A \) that satisfies the conditions: \( \{1,1,2\} \).
|
| 42 |
+
- For \( k=4 \), there is no multiset \( A \) that satisfies the conditions.
|
| 43 |
+
|
| 44 |
+
## Sample Input 2
|
| 45 |
+
```
|
| 46 |
+
7 7
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
## Sample Output 2
|
| 50 |
+
```
|
| 51 |
+
1
|
| 52 |
+
3
|
| 53 |
+
4
|
| 54 |
+
3
|
| 55 |
+
2
|
| 56 |
+
1
|
| 57 |
+
1
|
| 58 |
+
```
|
problems/DP-02/Atcoder/abc222_e/editorial.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Red and Blue Tree (editorial)
|
| 2 |
+
|
| 3 |
+
Source: https://atcoder.jp/contests/abc222/editorial/2762 by en_translator
|
| 4 |
+
|
| 5 |
+
First, we count how many times the piece passes each edge with algorithm like DFS (Depth-First Search). If it passes Edge \(i\) for \(C_i\) times, then the problem is equivalent to “how many ways are there to separate \(C_1,\ldots,C_{N-1}\) into two groups so that the sum of the former \(R\) and the sum of the latter \(B\) satisfies \(R-B=K\)?”
|
| 6 |
+
|
| 7 |
+
Moreover, with \(S=C_1+\ldots+C_{N-1}\), it is boiled down to “how many ways are there to choose some elements from \(C_1,\ldots,C_{N-1}\) so that its sum becomes \(\frac{K+S}{2}\)?”
|
| 8 |
+
|
| 9 |
+
Therefore, this is computed by a DP where
|
| 10 |
+
|
| 11 |
+
\(DP[i][j]=\) The number of ways to choose some elements from \(C_1,\ldots,C_i\) so that its sum becomes \(j\).
|
| 12 |
+
|
| 13 |
+
Note that in some cases \(\frac{K+S}{2}\) may not be an integer or falls below \(0\).
|
| 14 |
+
|
| 15 |
+
The DFS for finding \(C_i\) costs \(O(NM)\) time, and the DP costs \(O(N(NM+|K|))\) time, so the overall time complexity is \(O(N^2M+N|K|)\).
|
problems/DP-02/Atcoder/abc222_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": 44,
|
| 14 |
+
"note": "no sample/secret split: the statement's samples are among the numbered tests"
|
| 15 |
+
}
|
problems/DP-02/Atcoder/abc222_e/meta.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"task_id": "abc222_e",
|
| 3 |
+
"contest": "abc222",
|
| 4 |
+
"type": "ABC",
|
| 5 |
+
"letter": "E",
|
| 6 |
+
"title": "Red and Blue Tree",
|
| 7 |
+
"url": "https://atcoder.jp/contests/abc222/tasks/abc222_e",
|
| 8 |
+
"difficulty": 1491,
|
| 9 |
+
"score": 500,
|
| 10 |
+
"time_limit_sec": 2.0,
|
| 11 |
+
"memory_limit_mb": 1024,
|
| 12 |
+
"n_tests": 44,
|
| 13 |
+
"editorial_url": "https://atcoder.jp/contests/abc222/editorial/2762",
|
| 14 |
+
"code_source": "editorial",
|
| 15 |
+
"pid": "A1729"
|
| 16 |
+
}
|
problems/DP-02/Atcoder/abc222_e/solution.cpp
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// abc222_e: E - Red and Blue Tree
|
| 2 |
+
// Code from the official editorial, https://atcoder.jp/contests/abc222/editorial/2762 (by en_translator)
|
| 3 |
+
// Judged AC on all 44 tests, max 0.15 s of the 2 s limit.
|
| 4 |
+
|
| 5 |
+
#include<bits/stdc++.h>
|
| 6 |
+
using namespace std;
|
| 7 |
+
const int mod=998244353;
|
| 8 |
+
|
| 9 |
+
vector<vector<pair<int,int>>>G;
|
| 10 |
+
vector<int>C;
|
| 11 |
+
|
| 12 |
+
int dfs(int v,int pre,int goal){
|
| 13 |
+
if(v==goal)return 1;
|
| 14 |
+
for(auto [vv,i]:G[v])if(vv!=pre){
|
| 15 |
+
if(dfs(vv,v,goal)){
|
| 16 |
+
C[i]++;
|
| 17 |
+
return 1;
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
return 0;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
int main(){
|
| 24 |
+
int N, M, K;
|
| 25 |
+
cin >> N >> M >> K;
|
| 26 |
+
vector<int>A(M);
|
| 27 |
+
|
| 28 |
+
for(int i=0;i<M;i++){
|
| 29 |
+
cin >> A[i];
|
| 30 |
+
A[i]--;
|
| 31 |
+
}
|
| 32 |
+
G.resize(N);
|
| 33 |
+
for(int i=0;i<N-1;i++){
|
| 34 |
+
int x,y;
|
| 35 |
+
cin >> x >> y;
|
| 36 |
+
x--,y--;
|
| 37 |
+
G[x].push_back({y,i});
|
| 38 |
+
G[y].push_back({x,i});
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
C.resize(N-1);
|
| 42 |
+
for(int i=0;i<M-1;i++)dfs(A[i],-1,A[i+1]);
|
| 43 |
+
|
| 44 |
+
int S=0;
|
| 45 |
+
for(int i=0;i<N-1;i++)S+=C[i];
|
| 46 |
+
if((S+K)%2 || S+K<0){
|
| 47 |
+
cout << 0;
|
| 48 |
+
return 0;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
vector<int>dp(100001);
|
| 52 |
+
dp[0]=1;
|
| 53 |
+
for(int i=0;i<N-1;i++)for(int x=100000;x>=C[i];x--)(dp[x]+=dp[x-C[i]])%=mod;
|
| 54 |
+
cout << dp[(S+K)/2];
|
| 55 |
+
}
|
problems/DP-02/Atcoder/abc222_e/statement.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# E - Red and Blue Tree
|
| 2 |
+
|
| 3 |
+
**Score:** 500 points
|
| 4 |
+
|
| 5 |
+
## Problem Statement
|
| 6 |
+
Given are a tree with \(N\) vertices, a sequence of \(M\) numbers \(A=(A_1,\ldots,A_M)\), and an integer \(K\).
|
| 7 |
+
The vertices are numbered \(1\) through \(N\), and the \(i\)-th edge connects Vertices \(U_i\) and \(V_i\).
|
| 8 |
+
|
| 9 |
+
We will paint each of the \(N-1\) edges of this tree red or blue. Among the \(2^{N-1}\) such ways, find the number of ones that satisfies the following condition, modulo \(998244353\).
|
| 10 |
+
|
| 11 |
+
**Condition:**
|
| 12 |
+
Let us put a piece on Vertex \(A_1\), and for each \(i=1,\ldots,M-1\) in this order, move it from Vertex \(A_i\) to Vertex \(A_{i+1}\) along the edges in the shortest path. After all of these movements, \(R-B=K\) holds, where \(R\) and \(B\) are the numbers of times the piece traverses a red edge and a blue edge, respectively.
|
| 13 |
+
|
| 14 |
+
## Constraints
|
| 15 |
+
- \(2 \leq N \leq 1000\)
|
| 16 |
+
- \(2 \leq M \leq 100\)
|
| 17 |
+
- \(|K| \leq 10^5\)
|
| 18 |
+
- \(1 \leq A_i \leq N\)
|
| 19 |
+
- \(1\leq U_i,V_i\leq N\)
|
| 20 |
+
- The given graph is a tree.
|
| 21 |
+
- All values in input are integers.
|
| 22 |
+
|
| 23 |
+
## Input
|
| 24 |
+
Input is given from Standard Input in the following format:
|
| 25 |
+
```
|
| 26 |
+
N M K
|
| 27 |
+
A_1 A_2 \ldots A_M
|
| 28 |
+
U_1 V_1
|
| 29 |
+
...
|
| 30 |
+
U_{N-1} V_{N-1}
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
## Output
|
| 34 |
+
Print the answer.
|
| 35 |
+
|
| 36 |
+
## Sample Input 1
|
| 37 |
+
```
|
| 38 |
+
4 5 0
|
| 39 |
+
2 3 2 1 4
|
| 40 |
+
1 2
|
| 41 |
+
2 3
|
| 42 |
+
3 4
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
## Sample Output 1
|
| 46 |
+
```
|
| 47 |
+
2
|
| 48 |
+
```
|
| 49 |
+
*If we paint the 1-st and 3-rd edges red and the 2-nd edge blue, the piece will traverse the following numbers of red and blue edges:*
|
| 50 |
+
- \(0\) red edges and \(1\) blue edge when moving from Vertex \(2\) to \(3\),
|
| 51 |
+
- \(0\) red edges and \(1\) blue edge when moving from Vertex \(3\) to \(2\),
|
| 52 |
+
- \(1\) red edge and \(0\) blue edges when moving from Vertex \(2\) to \(1\),
|
| 53 |
+
- \(2\) red edges and \(1\) blue edge when moving from Vertex \(1\) to \(4\).
|
| 54 |
+
|
| 55 |
+
*for a total of \(3\) red edges and \(3\) blue edges, satisfying the condition.*
|
| 56 |
+
|
| 57 |
+
Another way to satisfy the condition is to paint the 1-st and 3-rd edges blue and the 2-nd edge red. There is no other way to satisfy it, so the answer is \(2\).
|
| 58 |
+
|
| 59 |
+
## Sample Input 2
|
| 60 |
+
```
|
| 61 |
+
3 10 10000
|
| 62 |
+
1 2 1 2 1 2 2 1 1 2
|
| 63 |
+
1 2
|
| 64 |
+
1 3
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
## Sample Output 2
|
| 68 |
+
```
|
| 69 |
+
0
|
| 70 |
+
```
|
| 71 |
+
*There may be no way to paint the tree to satisfy the condition.*
|
| 72 |
+
|
| 73 |
+
## Sample Input 3
|
| 74 |
+
```
|
| 75 |
+
10 2 -1
|
| 76 |
+
1 10
|
| 77 |
+
1 2
|
| 78 |
+
2 3
|
| 79 |
+
3 4
|
| 80 |
+
4 5
|
| 81 |
+
5 6
|
| 82 |
+
6 7
|
| 83 |
+
7 8
|
| 84 |
+
8 9
|
| 85 |
+
9 10
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
## Sample Output 3
|
| 89 |
+
```
|
| 90 |
+
126
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
## Sample Input 4
|
| 94 |
+
```
|
| 95 |
+
5 8 -1
|
| 96 |
+
1 4 1 4 2 1 3 5
|
| 97 |
+
1 2
|
| 98 |
+
4 1
|
| 99 |
+
3 1
|
| 100 |
+
1 5
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
## Sample Output 4
|
| 104 |
+
```
|
| 105 |
+
2
|
| 106 |
+
```
|