|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + . "fmt" |
| 5 | + "io" |
| 6 | + "math/bits" |
| 7 | +) |
| 8 | + |
| 9 | +// https://space.bilibili.com/206214 |
| 10 | +func p7961(in io.Reader, out io.Writer) { |
| 11 | + const mod = 998244353 |
| 12 | + pow := func(x, n int) int { |
| 13 | + res := 1 |
| 14 | + for ; n > 0; n /= 2 { |
| 15 | + if n%2 > 0 { |
| 16 | + res = res * x % mod |
| 17 | + } |
| 18 | + x = x * x % mod |
| 19 | + } |
| 20 | + return res |
| 21 | + } |
| 22 | + |
| 23 | + const mx = 31 |
| 24 | + var fac, invF [mx]int |
| 25 | + fac[0] = 1 |
| 26 | + for i := 1; i < mx; i++ { |
| 27 | + fac[i] = fac[i-1] * i % mod |
| 28 | + } |
| 29 | + invF[mx-1] = pow(fac[mx-1], mod-2) |
| 30 | + for i := mx - 1; i > 0; i-- { |
| 31 | + invF[i-1] = invF[i] * i % mod |
| 32 | + } |
| 33 | + |
| 34 | + var n, m, k, v int |
| 35 | + Fscan(in, &m, &n, &k) |
| 36 | + n++ |
| 37 | + powV := make([][]int, n) |
| 38 | + for i := range powV { |
| 39 | + Fscan(in, &v) |
| 40 | + powV[i] = make([]int, m+1) |
| 41 | + powV[i][0] = 1 |
| 42 | + for j := 1; j <= m; j++ { |
| 43 | + powV[i][j] = powV[i][j-1] * v % mod |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + memo := make([][][][]int, n) |
| 48 | + for i := range memo { |
| 49 | + memo[i] = make([][][]int, m+1) |
| 50 | + for j := range memo[i] { |
| 51 | + memo[i][j] = make([][]int, m/2+1) |
| 52 | + for p := range memo[i][j] { |
| 53 | + memo[i][j][p] = make([]int, k+1) |
| 54 | + for q := range memo[i][j][p] { |
| 55 | + memo[i][j][p][q] = -1 |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + var dfs func(int, int, int, int) int |
| 61 | + dfs = func(i, leftM, x, leftK int) (res int) { |
| 62 | + if i == n { |
| 63 | + if leftM == 0 && bits.OnesCount(uint(x)) <= leftK { |
| 64 | + return 1 |
| 65 | + } |
| 66 | + return |
| 67 | + } |
| 68 | + p := &memo[i][leftM][x][leftK] |
| 69 | + if *p != -1 { |
| 70 | + return *p |
| 71 | + } |
| 72 | + for j := 0; j <= leftM; j++ { |
| 73 | + bit := (x + j) & 1 |
| 74 | + if bit <= leftK { |
| 75 | + r := dfs(i+1, leftM-j, (x+j)>>1, leftK-bit) |
| 76 | + res = (res + r*powV[i][j]%mod*invF[j]) % mod |
| 77 | + } |
| 78 | + } |
| 79 | + *p = res |
| 80 | + return |
| 81 | + } |
| 82 | + Fprint(out, dfs(0, m, 0, k)*fac[m]%mod) |
| 83 | +} |
| 84 | + |
| 85 | +//func main() { p7961(bufio.NewReader(os.Stdin), os.Stdout) } |
0 commit comments