Approach
Depth-first search
For Codeforces 1511F — Chainword, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 115 lines of Go from the credited upstream file 1511F.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1package main2 3import (4 . "fmt"5 "io"6)7 89const mod11 = 99824435310 11type matrix11 [][]int12 13func newMatrix11(n, m int) matrix11 {14 a := make(matrix11, n)15 for i := range a {16 a[i] = make([]int, m)17 }18 return a19}20 21func (a matrix11) mul(b matrix11) matrix11 {22 c := newMatrix11(len(a), len(b[0]))23 for i, row := range a {24 for k, x := range row {25 if x == 0 {26 continue27 }28 for j, y := range b[k] {29 c[i][j] = (c[i][j] + x*y) % mod1130 }31 }32 }33 return c34}35 36func (a matrix11) powMul(n int, f0 matrix11) matrix11 {37 res := f038 for ; n > 0; n /= 2 {39 if n%2 > 0 {40 res = a.mul(res)41 }42 a = a.mul(a)43 }44 return res45}46 47func cf1511F(in io.Reader, out io.Writer) {48 var n, k int49 var s string50 Fscan(in, &n, &k)51 52 type node struct {53 son [26]*node54 end bool55 }56 root := &node{}57 for range n {58 Fscan(in, &s)59 o := root60 for _, b := range s {61 b -= 'a'62 if o.son[b] == nil {63 o.son[b] = &node{}64 }65 o = o.son[b]66 }67 o.end = true68 }69 70 m := newMatrix11(145, 145)71 type pair struct{ p, q *node }72 idx := map[pair]int{}73 var dfs func(*node, *node) int74 dfs = func(p, q *node) int {75 t := pair{p, q}76 if i, ok := idx[t]; ok {77 return i78 }79 v := len(idx)80 idx[t] = v81 for i := range 26 {82 a, b := p.son[i], q.son[i]83 if a == nil || b == nil {84 continue85 }86 m[dfs(a, b)][v] = 187 if a.end {88 m[dfs(root, b)][v] = 189 }90 if b.end {91 m[dfs(root, a)][v]++92 }93 if a.end && b.end {94 m[dfs(root, root)][v]++95 }96 }97 return v98 }99 dfs(root, root)100 101 size := len(idx)102 m = m[:size]103 for i, row := range m {104 m[i] = row[:size]105 }106 107 f0 := newMatrix11(size, 1)108 f0[0][0] = 1109 110 fn := m.powMul(k, f0)111 Fprint(out, fn[0][0]%mod11)112}113 114115