Approach
Sorting and greedy selection
For Codeforces 2131G — Wafu!, 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
- 67 lines of Go from the credited upstream file 2131G.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 cf2131G(in io.Reader, out io.Writer) {11 const mod = 1_000_000_00712 pow := func(x, n int) int {13 res := 114 for ; n > 0; n /= 2 {15 if n%2 > 0 {16 res = res * x % mod17 }18 x = x * x % mod19 }20 return res21 }22 var T, n, k int23 for Fscan(in, &T); T > 0; T-- {24 Fscan(in, &n, &k)25 a := make([]int, n)26 for i := range a {27 Fscan(in, &a[i])28 }29 slices.Sort(a)30 31 cnt := map[int]int{}32 var del func(int)33 del = func(v int) {34 k--35 cnt[v]++36 if v < 31 && k >= 1<<(v-1)-1 {37 k -= 1<<(v-1) - 138 for i := 1; i < v; i++ {39 cnt[i] += 1 << (v - 1 - i)40 }41 return42 }43 for i := 1; i < v && k > 0; i++ {44 del(i)45 }46 }47 for _, v := range a {48 if v > 30 || 1<<(v-1) >= k {49 del(v)50 break51 }52 k -= 1 << (v - 1)53 cnt[v]++54 for i := 1; i < v; i++ {55 cnt[i] += 1 << (v - 1 - i)56 }57 }58 ans := 159 for v, c := range cnt {60 ans = ans * pow(v, c) % mod61 }62 Fprintln(out, ans)63 }64}65 6667