aboutsummaryrefslogtreecommitdiff
path: root/03_dynamic_programming/counting_towers_2413.cpp
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/counting_towers_2413.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '03_dynamic_programming/counting_towers_2413.cpp')
-rw-r--r--03_dynamic_programming/counting_towers_2413.cpp38
1 files changed, 38 insertions, 0 deletions
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}

Generated with cgit - Back to sebastiano.tronto.net