- 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
- 57 lines of Go from the credited upstream file 1185G1.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 . "fmt"5 "io"6 "math/bits"7)8 910func CF1185G1(in io.Reader, out io.Writer) {11 const mod int = 1e9 + 712 var n, t, ans int13 Fscan(in, &n, &t)14 a := make([]int, n)15 tp := make([]int, n)16 for i := range a {17 Fscan(in, &a[i], &tp[i])18 }19 20 dp := make([][]int, 1<<n)21 for i := range dp {22 dp[i] = make([]int, n)23 }24 for i := range a {25 dp[1<<i][i] = 126 }27 for s, dr := range dp {28 for ss := uint(s); ss > 0; ss &= ss - 1 {29 i := bits.TrailingZeros(ss)30 for t, lb := len(dp)-1^s, 0; t > 0; t ^= lb {31 lb = t & -t32 j := bits.TrailingZeros(uint(lb))33 if tp[j] != tp[i] {34 dp[s|lb][j] = (dp[s|lb][j] + dr[i]) % mod35 }36 }37 }38 }39 sum := make([]int, 1<<n)40 for i, v := range a {41 bit := 1 << i42 for mask := 0; mask < bit; mask++ {43 sum[bit|mask] = sum[mask] + v44 }45 }46 for i, s := range sum {47 if s == t {48 for ss := uint(i); ss > 0; ss &= ss - 1 {49 ans = (ans + dp[i][bits.TrailingZeros(ss)]) % mod50 }51 }52 }53 Fprint(out, ans)54}55 5657