- 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
- 93 lines of Go from the credited upstream file 1107D.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/bits"8)9 1011func CF1107D(_r io.Reader, out io.Writer) {12 in := bufio.NewReader(_r)13 const w = bits.UintSize14 var n, ans int15 var s string16 Fscan(in, &n)17 a := make([][]uint, n)18 for i := range a {19 a[i] = make([]uint, (n+w-1)/w)20 Fscan(in, &s)21 for j, b := range s {22 if b < 'A' {23 b -= '0'24 } else {25 b -= 'A' - 1026 }27 a[i][j*4/w] |= uint(bits.Reverse8(uint8(b))>>4) << (j % (w / 4) * 4)28 }29 }30 31 f := func(d int) bool {32 for i, r := range a {33 for j := 0; j < n; j += d {34 st, end := j, j+d-135 if a[i-i%d][j/w]>>(j%w)&1 == 0 {36 i := st / w37 if i == end/w {38 if r[i]>>(st%w)&(1<<(end-st+1)-1) != 0 {39 return false40 }41 continue42 }43 if r[i]>>(st%w) != 0 {44 return false45 }46 for i++; i < end/w; i++ {47 if r[i] != 0 {48 return false49 }50 }51 if r[end/w]&(1<<(end%w+1)-1) != 0 {52 return false53 }54 } else {55 i := st / w56 if i == end/w {57 if r[i]>>(st%w)&(1<<(end-st+1)-1) != 1<<(end-st+1)-1 {58 return false59 }60 continue61 }62 if ^(r[i] | (1<<(st%w) - 1)) != 0 {63 return false64 }65 for i++; i < end/w; i++ {66 if ^r[i] != 0 {67 return false68 }69 }70 if r[end/w]&(1<<(end%w+1)-1) != 1<<(end%w+1)-1 {71 return false72 }73 }74 }75 }76 return true77 }78 for d := 1; d*d <= n; d++ {79 if n%d == 0 {80 if d*d < n && f(n/d) {81 ans = n / d82 break83 }84 if f(d) {85 ans = d86 }87 }88 }89 Fprint(out, ans)90}91 9293