Approach
Sorting and greedy selection
For Codeforces 2114F — Small Operations, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 54 lines of Go from the credited upstream file 2114F.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
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 "slices"7)8 910func calc14(x, k int) int {11 ds := []int{}12 for d := 1; d*d <= x; d++ {13 if x%d == 0 {14 ds = append(ds, d)15 if d*d < x {16 ds = append(ds, x/d)17 }18 }19 }20 slices.Sort(ds)21 n := len(ds)22 f := make([]int, n)23 for i := 1; i < n; i++ {24 f[i] = 1e925 for j := i - 1; j >= 0 && ds[i]/ds[j] <= k; j-- {26 if ds[i]%ds[j] == 0 {27 f[i] = min(f[i], f[j]+1)28 }29 }30 }31 return f[n-1]32}33 34func cf2114F(in io.Reader, out io.Writer) {35 gcd := func(a, b int) int {36 for a != 0 {37 a, b = b%a, a38 }39 return b40 }41 var T, x, y, k int42 for Fscan(in, &T); T > 0; T-- {43 Fscan(in, &x, &y, &k)44 g := gcd(x, y)45 ans := calc14(x/g, k) + calc14(y/g, k)46 if ans >= 1e9 {47 ans = -148 }49 Fprintln(out, ans)50 }51}52 5354