Approach
Depth-first search
For Codeforces 914C — Travelling Salesman and Special Numbers, 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
- 69 lines of Go from the credited upstream file 914C.go.
- The implementation visibly relies on sequence storage, cached states.
- 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 "math/bits"7)8 910func cf914C(in io.Reader, out io.Writer) {11 const mod = 1_000_000_00712 var s string13 var k int14 Fscan(in, &s, &k)15 if k == 0 {16 Fprint(out, 1)17 return18 }19 if k == 1 {20 Fprint(out, len(s)-1)21 return22 }23 24 n := len(s)25 memo := make([][]int, n)26 for i := range memo {27 memo[i] = make([]int, n+1)28 for j := range memo[i] {29 memo[i][j] = -130 }31 }32 var dfs func(int, int, bool) int33 dfs = func(i, left1 int, isLimit bool) (res int) {34 if left1 == 0 {35 return 136 }37 if i == n {38 return39 }40 if !isLimit {41 p := &memo[i][left1]42 if *p >= 0 {43 return *p44 }45 defer func() { *p = res }()46 }47 up := 148 if isLimit {49 up = int(s[i] - '0')50 }51 for d := 0; d <= up; d++ {52 res += dfs(i+1, left1-d, isLimit && d == up)53 }54 return res % mod55 }56 57 ans := 058 f := make([]int, n+1)59 for i := 1; i <= n; i++ {60 f[i] = f[bits.OnesCount(uint(i))] + 161 if f[i] == k {62 ans += dfs(0, i, true)63 }64 }65 Fprint(out, ans%mod)66}67 6869