- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 66 lines of Go from the credited upstream file 1334E.go.
- The implementation visibly relies on sequence storage, cached states.
- No explicit loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
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)8 910func CF1334E(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 const mod = 99824435315 gcd := func(a, b int64) int64 {16 for a != 0 {17 a, b = b%a, a18 }19 return b20 }21 22 var n, q, v, w int6423 Fscan(in, &n, &q)24 ps := []int64{}25 x := n26 if x&1 == 0 {27 ps = append(ps, 2)28 x /= x & -x29 }30 for i := int64(3); i*i <= x; i += 2 {31 if x%i == 0 {32 for x /= i; x%i == 0; x /= i {33 }34 ps = append(ps, i)35 }36 }37 if x > 1 {38 ps = append(ps, x)39 }40 41 dp := map[int64]int{}42 var f func(n int64) int43 f = func(n int64) (res int) {44 if n == 1 {45 return 146 }47 if v, ok := dp[n]; ok {48 return v49 }50 for _, p := range ps {51 if n%p == 0 {52 res = (res + f(n/p)) % mod53 }54 }55 dp[n] = res56 return57 }58 for ; q > 0; q-- {59 Fscan(in, &v, &w)60 g := gcd(v, w)61 Fprintln(out, int64(f(v/g))*int64(f(w/g))%mod)62 }63}64 6566