Approach
Stack-based processing
For Codeforces 986F — Oppa Funcan Style Remastered, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.
- Define what every stack entry represents.
- Pop entries once the current item resolves or invalidates them.
- Push the current item with only the information later steps need.
Code notes
- 136 lines of Go from the credited upstream file 986F.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
If each item is pushed and popped at most once, the stack work is linear.
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 "bufio"5 "container/heap"6 . "fmt"7 "io"8 "math"9)10 1112func CF986F(_r io.Reader, _w io.Writer) {13 in := bufio.NewReader(_r)14 out := bufio.NewWriter(_w)15 defer out.Flush()16 const mx = 3162277617 primes := make([]int, 0, 1951957)18 np := [mx + 1]bool{}19 for i := 2; i <= mx; i++ {20 if !np[i] {21 primes = append(primes, i)22 }23 for _, p := range primes {24 if p*i > mx {25 break26 }27 np[p*i] = true28 if i%p == 0 {29 break30 }31 }32 }33 pow := func(x, n, mod int64) int64 {34 x %= mod35 res := int64(1)36 for ; n > 0; n >>= 1 {37 if n&1 > 0 {38 res = res * x % mod39 }40 x = x * x % mod41 }42 return res43 }44 45 var T int46 var n, k int6447 Fscan(in, &T)48 type query struct {49 n int6450 i int51 }52 qs := map[int64][]query{}53 for i := 0; i < T; i++ {54 Fscan(in, &n, &k)55 qs[k] = append(qs[k], query{n, i})56 }57 ans := make([]bool, T)58 for k, qs := range qs {59 ps := []int64{}60 x := k61 for _, p := range primes {62 p := int64(p)63 if p > x {64 break65 }66 if x%p == 0 {67 for x /= p; x%p == 0; x /= p {68 }69 ps = append(ps, p)70 }71 }72 if x > 1 {73 ps = append(ps, x)74 }75 if len(ps) == 0 { 76 } else if len(ps) == 1 {77 for _, q := range qs {78 ans[q.i] = q.n%ps[0] == 079 }80 } else if len(ps) == 2 {81 x, y := ps[0], ps[1]82 for _, q := range qs {83 t := q.n % x * pow(y, x-2, x) % x84 ans[q.i] = t*y <= q.n85 }86 } else {87 dis := make([]int64, ps[0])88 for i := range dis {89 dis[i] = math.MaxInt6490 }91 dis[0] = 092 h := hp86{{}}93 for len(h) > 0 {94 top := h.pop()95 v := top.v96 if top.dis > dis[v] {97 continue98 }99 for _, p := range ps[1:] {100 w := (int64(v) + p) % ps[0]101 if newD := dis[v] + p; newD < dis[w] {102 dis[w] = newD103 h.push(pair86{int(w), newD})104 }105 }106 }107 for _, q := range qs {108 ans[q.i] = dis[q.n%ps[0]] <= q.n109 }110 }111 }112 for _, b := range ans {113 if b {114 Fprintln(out, "YES")115 } else {116 Fprintln(out, "NO")117 }118 }119}120 121122 123type pair86 struct {124 v int125 dis int64126}127type hp86 []pair86128 129func (h hp86) Len() int { return len(h) }130func (h hp86) Less(i, j int) bool { return h[i].dis < h[j].dis }131func (h hp86) Swap(i, j int) { h[i], h[j] = h[j], h[i] }132func (h *hp86) Push(v interface{}) { *h = append(*h, v.(pair86)) }133func (h *hp86) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }134func (h *hp86) push(v pair86) { heap.Push(h, v) }135func (h *hp86) pop() pair86 { return heap.Pop(h).(pair86) }136