- 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
- 75 lines of Go from the credited upstream file 2176F.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 . "fmt"5 "io"6)7 89func cf2176F(in io.Reader, out io.Writer) {10 const mod = 99824435311 pow := func(x, n int) int {12 res := 113 for ; n > 0; n /= 2 {14 if n%2 > 0 {15 res = res * x % mod16 }17 x = x * x % mod18 }19 return res20 }21 22 const mx = 20000123 omega := [mx]int{}24 for i := 2; i < mx; i++ {25 if omega[i] == 0 {26 for j := i; j < mx; j += i {27 omega[j]++28 }29 }30 }31 32 var T, n, k int33 for Fscan(in, &T); T > 0; T-- {34 Fscan(in, &n, &k)35 cnt := make([]int, n+1)36 for range n {37 var v int38 Fscan(in, &v)39 cnt[v]++40 }41 42 const u = 743 res := [u * 2]int{}44 f := make([][u * 2]int, n+1)45 for v := n; v > 0; v-- {46 cntOmega := [u]int{}47 for w := v; w <= n; w += v {48 cntOmega[omega[w]] += cnt[w]49 for j := range u * 2 {50 f[v][j] -= f[w][j]51 }52 }53 for i := range u {54 for j := range i {55 f[v][i+j] += cntOmega[i] * cntOmega[j]56 }57 f[v][i*2] += cntOmega[i] * (cntOmega[i] - 1) / 258 }59 for i := omega[v]; i < u*2; i++ {60 res[i-omega[v]] += f[v][i]61 }62 }63 64 ans := 065 for i, v := range res {66 if v != 0 {67 ans = (ans + v%mod*pow(i, k)) % mod68 }69 }70 Fprintln(out, ans)71 }72}73 7475