aboutsummaryrefslogtreecommitdiff
path: root/03_dynamic_programming
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /03_dynamic_programming
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '03_dynamic_programming')
-rwxr-xr-x03_dynamic_programming/a.outbin0 -> 13520 bytes
-rw-r--r--03_dynamic_programming/coin_combinations_i_1635.cpp25
-rw-r--r--03_dynamic_programming/coin_combinations_ii_1636.cpp28
-rw-r--r--03_dynamic_programming/counting_towers_2413.cpp38
-rw-r--r--03_dynamic_programming/dice_combinations_1633.cpp19
-rw-r--r--03_dynamic_programming/edit_distance_1639.cpp22
-rw-r--r--03_dynamic_programming/longest_common_subsequence_3403.cpp44
-rw-r--r--03_dynamic_programming/minimizing_coins_1634.cpp33
-rw-r--r--03_dynamic_programming/removing_digits_1637.cpp28
9 files changed, 237 insertions, 0 deletions
diff --git a/03_dynamic_programming/a.out b/03_dynamic_programming/a.out
new file mode 100755
index 0000000..f4fc9b1
--- /dev/null
+++ b/03_dynamic_programming/a.out
Binary files differ
diff --git a/03_dynamic_programming/coin_combinations_i_1635.cpp b/03_dynamic_programming/coin_combinations_i_1635.cpp
new file mode 100644
index 0000000..696c1f6
--- /dev/null
+++ b/03_dynamic_programming/coin_combinations_i_1635.cpp
@@ -0,0 +1,25 @@
1#include <iostream>
2#include <vector>
3
4static constexpr int mod = 1000000007;
5static constexpr int X = 1000001;
6
7int f(const std::vector<int>& c, std::vector<int>& a, int x) {
8 if (x < 0) return 0;
9 if (a[x] != -1) return a[x];
10 if (x == 0) return a[x] = 1;
11
12 a[x] = 0;
13 for (auto m : c)
14 a[x] = (a[x] + f(c, a, x-m)) % mod;
15 return a[x];
16}
17
18int main() {
19 int n, x;
20 std::cin >> n >> x;
21 std::vector<int> c(n), a(X, -1);
22 for (int i = 0; i < n; i++)
23 std::cin >> c[i];
24 std::cout << f(c, a, x) << "\n";
25}
diff --git a/03_dynamic_programming/coin_combinations_ii_1636.cpp b/03_dynamic_programming/coin_combinations_ii_1636.cpp
new file mode 100644
index 0000000..75642da
--- /dev/null
+++ b/03_dynamic_programming/coin_combinations_ii_1636.cpp
@@ -0,0 +1,28 @@
1#include <iostream>
2#include <map>
3#include <vector>
4
5static constexpr int mod = 1000000007;
6static constexpr int X = 1000001;
7
8int main() {
9 int n, x;
10 std::cin >> n >> x;
11 std::vector<int> c(n);
12 for (int i = 0; i < n; i++)
13 std::cin >> c[i];
14
15 std::vector<std::vector<int>> a(n, std::vector<int>(X, 0));
16 for (int i = 0; i < (int)c.size(); i++) a[i][0] = 1;
17 for (int j = c.back(); j <= x; j += c.back()) a[c.size()-1][j] = 1;
18 for (int i = c.size()-2; i >= 0; i--) {
19 for (int j = 1; j <= x; j++) {
20 a[i][j] = a[i+1][j];
21 if (j >= c[i]) {
22 a[i][j] += a[i][j-c[i]];
23 a[i][j] %= mod;
24 }
25 }
26 }
27 std::cout << a[0][x] << "\n";
28}
diff --git a/03_dynamic_programming/counting_towers_2413.cpp b/03_dynamic_programming/counting_towers_2413.cpp
new file mode 100644
index 0000000..902956b
--- /dev/null
+++ b/03_dynamic_programming/counting_towers_2413.cpp
@@ -0,0 +1,38 @@
1#include <iostream>
2#include <vector>
3
4// Recurrence relation:
5// f(n) = sum over i from 0 to n-1 of f(i) * p(n-i)
6// where p(n) is the number of indivisible towers of height n,
7// which is easily seen to be 3^(n-1)+1.
8// Then we can expand:
9// f(n) = sum_{i=0}^{n-1} f(i)(3^{n-i-1}+1) = g(n) + h(n)
10// where we define g(n) = sum f(i)3^{n-i-1} and h(n) = sum f(i).
11// Then it's easy to see that:
12// g(n+1) = f(n) + 3g(n)
13// h(n+1) = f(n) + h(n)
14// Initial values are h(1) = 1 and g(1) = 1.
15
16constexpr size_t mod{1000000007};
17constexpr size_t maxn{1000001};
18std::vector<size_t> f(maxn);
19std::vector<size_t> g(maxn);
20std::vector<size_t> h(maxn);
21
22int main() {
23 g[1] = h[1] = 1;
24 f[1] = 2;
25 for (size_t i = 2; i < maxn; i++) {
26 g[i] = (f[i-1] + 3*g[i-1]) % mod;
27 h[i] = (f[i-1] + h[i-1]) % mod;
28 f[i] = (g[i] + h[i]) % mod;
29 }
30
31 size_t t;
32 std::cin >> t;
33 for (size_t i = 0; i < t; i++) {
34 size_t n;
35 std::cin >> n;
36 std::cout << f[n] << "\n";
37 }
38}
diff --git a/03_dynamic_programming/dice_combinations_1633.cpp b/03_dynamic_programming/dice_combinations_1633.cpp
new file mode 100644
index 0000000..4e76d1d
--- /dev/null
+++ b/03_dynamic_programming/dice_combinations_1633.cpp
@@ -0,0 +1,19 @@
1#include <algorithm>
2#include <array>
3#include <iostream>
4
5// We use a funny memory optimization: we only store the last 7 values.
6
7int main() {
8 constexpr unsigned mod = 1e9+7;
9 int n;
10 std::cin >> n;
11 std::array<int, 7> v{0};
12 v[0] = 1;
13 for (int i = 1; i <= n; i++) {
14 v[i%7] = 0;
15 for (int j = std::max(0, i-6); j < i; j++)
16 v[i%7] = (v[i%7]+v[j%7]) % mod;
17 }
18 std::cout << v[n%7] << "\n";
19}
diff --git a/03_dynamic_programming/edit_distance_1639.cpp b/03_dynamic_programming/edit_distance_1639.cpp
new file mode 100644
index 0000000..0d8c6ef
--- /dev/null
+++ b/03_dynamic_programming/edit_distance_1639.cpp
@@ -0,0 +1,22 @@
1#include <algorithm>
2#include <iostream>
3#include <string>
4#include <vector>
5
6int d(const std::string& a, const std::string& b, size_t i, size_t j,
7 std::vector<std::vector<int>>& t) {
8 if (t[i][j] != -1) return t[i][j];
9 if (i == a.size()) return t[i][j] = b.size()-j;
10 if (j == b.size()) return t[i][j] = a.size()-i;
11 if (a[i] == b[j]) return t[i][j] = d(a, b, i+1, j+1, t);
12 return t[i][j] = 1+std::min(d(a, b, i+1, j+1, t),
13 std::min(d(a, b, i+1, j, t), d(a, b, i, j+1, t)));
14}
15
16int main() {
17 std::string a, b;
18 std::cin >> a >> b;
19 std::vector<std::vector<int>>
20 t(a.size()+1, std::vector<int>(b.size()+1, -1));
21 std::cout << d(a, b, 0, 0, t) << "\n";
22}
diff --git a/03_dynamic_programming/longest_common_subsequence_3403.cpp b/03_dynamic_programming/longest_common_subsequence_3403.cpp
new file mode 100644
index 0000000..ca94722
--- /dev/null
+++ b/03_dynamic_programming/longest_common_subsequence_3403.cpp
@@ -0,0 +1,44 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5int f(const std::vector<int>& a, const std::vector<int>& b, size_t i, size_t j,
6 std::vector<std::vector<int>>& t) {
7 if (i == a.size() || j == b.size()) return t[i][j] = 0;
8 if (t[i][j] != -1) return t[i][j];
9 if (a[i] == b[j]) return t[i][j] = 1+f(a, b, i+1, j+1, t);
10 return t[i][j] = std::max(f(a, b, i+1, j, t), f(a, b, i, j+1, t));
11}
12
13std::vector<int> read(size_t n) {
14 std::vector<int> a(n);
15 for (size_t i = 0; i < n; i++)
16 std::cin >> a[i];
17 return a;
18}
19
20int main() {
21 std::size_t n, m;
22 std::cin >> n >> m;
23 std::vector<int> a = read(n);
24 std::vector<int> b = read(m);
25 std::vector<std::vector<int>> t(n+1, std::vector<int>(m+1, -1));
26 int x = f(a, b, 0, 0, t);
27 std::cout << x << "\n";
28
29 std::vector<int> s;
30 size_t i{0}, j{0};
31 while (x > 0) {
32 if (a[i] == b[j]) {
33 s.push_back(a[i]);
34 i++; j++; x--;
35 } else {
36 if (t[i+1][j] == x) i++;
37 else j++;
38 }
39 }
40
41 for (auto x : s)
42 std::cout << x << " ";
43 std::cout << "\n";
44}
diff --git a/03_dynamic_programming/minimizing_coins_1634.cpp b/03_dynamic_programming/minimizing_coins_1634.cpp
new file mode 100644
index 0000000..f6189da
--- /dev/null
+++ b/03_dynamic_programming/minimizing_coins_1634.cpp
@@ -0,0 +1,33 @@
1#include <algorithm>
2#include <iostream>
3#include <queue>
4#include <vector>
5
6int f(const std::vector<int>& c, int x) {
7 static constexpr int max = 999999999;
8 std::vector<int> a(x+1, max);
9 std::queue<int> q;
10 a[0] = 0;
11 q.push(0);
12 while (!q.empty()) {
13 auto i = q.front();
14 q.pop();
15 for (auto k : c) {
16 if (i + k > x || a[i+k] <= a[i]+1) continue;
17 if (i + k == x) return a[i] + 1;
18 a[i+k] = a[i] + 1;
19 q.push(i + k);
20 }
21 }
22 return -1;
23}
24
25int main() {
26 int n, x;
27 std::cin >> n >> x;
28 std::vector<int> c(n);
29 for (int i = 0; i < n; i++)
30 std::cin >> c[i];
31
32 std::cout << f(c, x) << "\n";
33}
diff --git a/03_dynamic_programming/removing_digits_1637.cpp b/03_dynamic_programming/removing_digits_1637.cpp
new file mode 100644
index 0000000..8b5b334
--- /dev/null
+++ b/03_dynamic_programming/removing_digits_1637.cpp
@@ -0,0 +1,28 @@
1#include <algorithm>
2#include <iostream>
3#include <vector>
4
5static constexpr int inf = 1999999999;
6
7std::vector<int> digits(int n) {
8 std::vector<int> d;
9 for (int i = n; i != 0; i /= 10)
10 d.push_back(i % 10);
11 return d;
12}
13
14int f(std::vector<int>& a, int n) {
15 if (a[n] != inf) return a[n];
16 for (auto d : digits(n))
17 if (d != 0)
18 a[n] = std::min(a[n], 1+f(a, n-d));
19 return a[n];
20}
21
22int main() {
23 int n;
24 std::cin >> n;
25 std::vector<int> a(n+1, inf);
26 a[0] = 0;
27 std::cout << f(a, n) << "\n";
28}

Generated with cgit - Back to sebastiano.tronto.net