- 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
- 72 lines of Go from the credited upstream file 1140E.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 CF1140E(_r io.Reader, out io.Writer) {11 in := bufio.NewReader(_r)12 const mod = 99824435313 pow := func(x int64, n int) (res int64) {14 res = 115 for ; n > 0; n >>= 1 {16 if n&1 > 0 {17 res = res * x % mod18 }19 x = x * x % mod20 }21 return22 }23 24 var n, v int25 var k int6426 Fscan(in, &n, &k)27 dp := make([][2]int64, n)28 dp[0][0] = 129 for i := 1; i < n; i++ {30 dp[i][0] = (dp[i-1][0]*(k-2) + dp[i-1][1]) % mod31 dp[i][1] = dp[i-1][0] * (k - 1) % mod32 }33 34 a := [2][]int{}35 for ; n > 0; n-- {36 Fscan(in, &v)37 a[n&1] = append(a[n&1], v)38 }39 40 f := func(a []int) int64 {41 ans := int64(1)42 for i, n := 0, len(a); i < n; i++ {43 if a[i] > 0 {44 if i > 0 && a[i] == a[i-1] {45 return 046 }47 continue48 }49 st := i50 for ; i < n && a[i] < 0; i++ {51 }52 if st == 0 {53 if i == n {54 ans = ans * pow(k-1, n-1) % mod * k % mod55 } else {56 ans = ans * pow(k-1, i) % mod57 }58 } else if i == n {59 ans = ans * pow(k-1, n-st) % mod60 } else if a[st-1] != a[i] {61 ans = ans * dp[i-st][0] % mod62 } else {63 ans = ans * dp[i-st][1] % mod64 }65 }66 return ans67 }68 Fprint(out, f(a[0])*f(a[1])%mod)69}70 7172