Approach
Sorting and greedy selection
For Codeforces 1269B — Modulo Equality, 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
- 72 lines of Go from the credited upstream file 1269B.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 cf1269B(in io.Reader, out io.Writer) {11 var n, m int12 Fscan(in, &n, &m)13 a := make([]int, n)14 for i := range a {15 Fscan(in, &a[i])16 }17 slices.Sort(a)18 a0 := a[0]19 for i := range n - 1 {20 a[i] = a[i+1] - a[i]21 }22 23 pi := make([]int, n-1)24 match := 025 for i := 1; i < n-1; i++ {26 v := a[i]27 for match > 0 && a[match] != v {28 match = pi[match-1]29 }30 if a[match] == v {31 match++32 }33 pi[i] = match34 }35 36 b := make([]int, n*2)37 for i := range n {38 Fscan(in, &b[i])39 }40 if n == 1 {41 Fprint(out, (b[0]-a0+m)%m)42 return43 }44 slices.Sort(b[:n])45 for i, v := range b[:n] {46 b[n+i] = v + m47 }48 oriB := slices.Clone(b)49 for i := range n*2 - 1 {50 b[i] = b[i+1] - b[i]51 }52 53 ans := m54 match = 055 for i, v := range b[:n*2-1] {56 for match > 0 && a[match] != v {57 match = pi[match-1]58 }59 if a[match] == v {60 match++61 }62 if match == n-1 {63 res := oriB[i-(n-1)+1] - a064 ans = min(ans, (res%m+m)%m)65 match = pi[match-1]66 }67 }68 Fprint(out, ans)69}70 7172