Approach
Sorting and greedy selection
For Codeforces 912E — Prime Gift, 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
- 57 lines of Go from the credited upstream file 912E.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 "sort"8)9 1011func cf912E(in io.Reader, out io.Writer) {12 var n, k int13 Fscan(in, &n)14 p := make([]int, n)15 for i := range p {16 Fscan(in, &p[i])17 }18 Fscan(in, &k)19 20 gen := func(st int) (a []int) {21 var f func(int, int)22 f = func(i, m int) {23 if i >= len(p) {24 a = append(a, m)25 return26 }27 f(i+2, m)28 if m <= 1e18/p[i] {29 f(i, m*p[i])30 }31 }32 f(st, 1)33 slices.Sort(a)34 return35 }36 a := gen(0)37 b := gen(1)38 39 ans := sort.Search(1e18, func(mx int) bool {40 k := k41 j := len(b) - 142 for _, v := range a {43 for j >= 0 && b[j] > mx/v {44 j--45 }46 k -= j + 147 if k <= 0 {48 return true49 }50 }51 return false52 })53 Fprint(out, ans)54}55 5657