- 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
- 61 lines of Go from the credited upstream file 1620G.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 "math/bits"7)8 910func cf1620G(in io.Reader, out io.Writer) {11 const mod = 99824435312 var n, ans int13 var s string14 Fscan(in, &n)15 cnt := make([][26]int, n)16 for i := range cnt {17 Fscan(in, &s)18 for _, b := range s {19 cnt[i][b-'a']++20 }21 }22 23 f := make([]int32, 1<<n)24 mn := [26]int{}25 for mask := 1; mask < 1<<n; mask++ {26 for i := range 26 {27 mn[i] = 1e928 }29 for m := uint(mask); m > 0; m &= m - 1 {30 for i, c := range cnt[bits.TrailingZeros(m)][:] {31 mn[i] = min(mn[i], c)32 }33 }34 35 res := bits.OnesCount(uint(mask))%2*2 - 1 + mod36 for i := range 26 {37 res = res * (mn[i] + 1) % mod38 }39 f[mask] = int32(res)40 }41 42 for i := range n {43 for j := 0; j < 1<<n; j++ {44 j |= 1 << i45 f[j] = (f[j] + f[j^1<<i]) % mod46 }47 }48 49 for mask, v := range f {50 sum := 051 for m := uint(mask); m > 0; m &= m - 1 {52 sum += bits.TrailingZeros(m)53 }54 k := bits.OnesCount(uint(mask))55 ans ^= int(v) * k * (sum + k)56 }57 Fprint(out, ans)58}59 6061