- 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
- 65 lines of Go from the credited upstream file 431C.go.
- The implementation visibly relies on sequence storage, 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 Sol431C(reader io.Reader, writer io.Writer) {11 in := bufio.NewReader(reader)12 out := bufio.NewWriter(writer)13 defer out.Flush()14 15 const mod = 1e9 + 716 var n, k, d int17 Fscan(in, &n, &k, &d)18 dp := make([][2]int, n)19 for i := range dp {20 dp[i][0] = -121 dp[i][1] = -122 }23 var f func(int, int) int24 f = func(sum int, contain int) int {25 if sum == n {26 return 127 }28 if contain == 0 && sum+d > n {29 return 030 }31 if v := dp[sum][contain]; v != -1 {32 return v33 }34 ans := 035 if contain == 0 {36 for i := 1; i < d; i++ {37 if sum+i > n {38 break39 }40 ans = (ans + f(sum+i, 0)) % mod41 }42 for i := d; i <= k; i++ {43 if sum+i > n {44 break45 }46 ans = (ans + f(sum+i, 1)) % mod47 }48 } else {49 for i := 1; i <= k; i++ {50 if sum+i > n {51 break52 }53 ans = (ans + f(sum+i, 1)) % mod54 }55 }56 dp[sum][contain] = ans57 return ans58 }59 Fprint(out, f(0, 0))60}61 62636465