- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 95 lines of Go from the credited upstream file 1033D.go.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
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 . "fmt"6 "io"7 "math"8)9 1011func CF1033D(_r io.Reader, _w io.Writer) {12 const mod int64 = 99824435313 sqrt := func(a int64) int64 {14 r := int64(math.Round(math.Sqrt(float64(a))))15 if r*r == a {16 return r17 }18 return -119 }20 cbrt := func(a int64) int64 {21 r := int64(math.Round(math.Cbrt(float64(a))))22 if r*r*r == a {23 return r24 }25 return -126 }27 gcd := func(a, b int64) int64 {28 for a != 0 {29 a, b = b%a, a30 }31 return b32 }33 34 in := bufio.NewReader(_r)35 var n int36 Fscan(in, &n)37 a := make([]int64, 0, n)38 vis := make([]bool, 0, n)39 cntv := map[int64]int{}40 cnte := map[int64]int{}41 for ; n > 0; n-- {42 var v int6443 Fscan(in, &v)44 ok := true45 if r := cbrt(v); r != -1 {46 cnte[r] += 347 } else if r := sqrt(v); r != -1 {48 if r4 := sqrt(r); r4 != -1 {49 cnte[r4] += 450 } else {51 cnte[r] += 252 }53 } else {54 ok = false55 }56 cntv[v]++57 if cntv[v] == 1 {58 a = append(a, v)59 vis = append(vis, ok)60 }61 }62 63 ans := int64(1)64 for i, x := range a {65 if !vis[i] {66 for j, y := range a {67 if j == i {68 continue69 }70 if g := gcd(x, y); g > 1 {71 cnte[g] += cntv[x]72 cnte[x/g] += cntv[x]73 if !vis[j] {74 cnte[g] += cntv[y]75 cnte[y/g] += cntv[y]76 vis[j] = true77 }78 vis[i] = true79 break80 }81 }82 }83 if !vis[i] {84 c := cntv[x] + 185 ans = ans * int64(c*c) % mod86 }87 }88 for _, e := range cnte {89 ans = ans * int64(e+1) % mod90 }91 Fprintln(_w, ans)92}93 9495