Approach
Breadth-first search
For Codeforces 79D — Password, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 85 lines of Go from the credited upstream file 79D.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
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 "slices"8)9 1011func cf79D(in io.Reader, out io.Writer) {12 var n, k, l int13 Fscan(in, &n, &k, &l)14 x := make([]int, k)15 for i := range x {16 Fscan(in, &x[i])17 }18 t := []int{}19 for i := 0; i < k; {20 t = append(t, x[i])21 for i++; i < k && x[i] == x[i-1]+1; i++ {22 }23 t = append(t, x[i-1]+1)24 }25 x = t26 k = len(x)27 28 a := make([]int, l)29 for i := range a {30 Fscan(in, &a[i])31 }32 slices.Sort(a)33 dis := make([]int, n+2)34 bfs := func(st int) []int {35 for i := range dis {36 dis[i] = 1e937 }38 q := []int{st}39 dis[st] = 040 for len(q) > 0 {41 v := q[0]42 q = q[1:]43 for _, w := range a {44 if v > w && dis[v]+1 < dis[v-w] {45 dis[v-w] = dis[v] + 146 q = append(q, v-w)47 }48 if v+w < len(dis) && dis[v]+1 < dis[v+w] {49 dis[v+w] = dis[v] + 150 q = append(q, v+w)51 }52 }53 }54 dx := make([]int, k)55 for i, v := range x {56 dx[i] = dis[v]57 }58 return dx59 }60 dx := make([][]int, k)61 for i, v := range x {62 dx[i] = bfs(v)63 }64 65 f := make([]int, 1<<k)66 for i := 1; i < len(f); i++ {67 if bits.OnesCount(uint(i))&1 > 0 {68 continue69 }70 f[i] = 1e971 tz := bits.TrailingZeros(uint(i))72 for m := uint(i ^ 1<<tz); m > 0; m &= m - 1 {73 j := bits.TrailingZeros(m)74 f[i] = min(f[i], f[i^1<<tz^1<<j]+dx[tz][j])75 }76 }77 ans := f[1<<k-1]78 if ans == 1e9 {79 ans = -180 }81 Fprint(out, ans)82}83 8485