- 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
- 53 lines of Go from the credited upstream file 1738C.go.
- The implementation visibly relies on 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 CF1738C(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 15 dp := [101][101][2][2]int8{}16 for i := range dp {17 for j := range dp[i] {18 dp[i][j] = [2][2]int8{{-1, -1}, {-1, -1}}19 }20 }21 var f func(c0, c1, s, who int8) int822 f = func(c0, c1, s, who int8) (res int8) {23 if c0 == 0 && c1 == 0 {24 return s ^ who ^ 125 }26 dv := &dp[c0][c1][s][who]27 if *dv != -1 {28 return *dv29 }30 defer func() { *dv = res }()31 if c0 > 0 && f(c0-1, c1, s, who^1) == 0 || c1 > 0 && f(c0, c1-1, s^who^1, who^1) == 0 {32 return 133 }34 return35 }36 37 var T, n, v int38 for Fscan(in, &T); T > 0; T-- {39 cnt := [2]int8{}40 for Fscan(in, &n); n > 0; n-- {41 Fscan(in, &v)42 cnt[v&1]++43 }44 if f(cnt[0], cnt[1], 0, 0) == 1 {45 Fprintln(out, "Alice")46 } else {47 Fprintln(out, "Bob")48 }49 }50}51 5253