- 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
- 56 lines of Go from the credited upstream file 1279D.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)8 910func CF1279D(_r io.Reader, _w io.Writer) {11 in := bufio.NewScanner(_r)12 in.Split(bufio.ScanWords)13 r := func() (x int) {14 in.Scan()15 for _, b := range in.Bytes() {16 x = x*10 + int(b&15)17 }18 return19 }20 const mod int64 = 99824435321 pow := func(x int64) int64 {22 x %= mod23 res := int64(1)24 for n := mod - 2; n > 0; n >>= 1 {25 if n&1 == 1 {26 res = res * x % mod27 }28 x = x * x % mod29 }30 return res31 }32 div := func(a, b int64) int64 { return a * pow(b) % mod }33 34 n := r()35 cnts := [1e6 + 1]int{}36 ps := make([][]int, n)37 for i := range ps {38 ps[i] = make([]int, r())39 for j := range ps[i] {40 ps[i][j] = r()41 cnts[ps[i][j]]++42 }43 }44 ans := int64(0)45 for _, p := range ps {46 s := 047 for _, v := range p {48 s += cnts[v]49 }50 ans = (ans + div(int64(s), int64(len(p)))) % mod51 }52 Fprint(_w, div(ans, int64(n)*int64(n)))53}54 5556