Skip to content

Commit fd2d67a

Browse files
committed
[2400] CF626F 计数 DP
1 parent a921e0e commit fd2d67a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

main/600-699/626F.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
"sort"
8+
)
9+
10+
// https://space.bilibili.com/206214
11+
func CF626F(_r io.Reader, out io.Writer) {
12+
in := bufio.NewReader(_r)
13+
const mod = 1_000_000_007
14+
var n, k int
15+
Fscan(in, &n, &k)
16+
a := make([]int, n, n+1)
17+
for i := range a {
18+
Fscan(in, &a[i])
19+
}
20+
sort.Ints(a)
21+
a = append(a, 0) // 避免越界
22+
23+
memo := [200][101][1001]int{}
24+
for i := range memo {
25+
for j := range memo[i] {
26+
for k := range memo[i][j] {
27+
memo[i][j][k] = -1
28+
}
29+
}
30+
}
31+
var dfs func(int, int, int) int64
32+
dfs = func(i, groups, leftK int) (res int64) {
33+
if leftK < 0 || groups > i+1 { // groups > i+1 说明剩余数字不够组成最小值
34+
return
35+
}
36+
if i < 0 {
37+
if groups == 0 {
38+
return 1
39+
}
40+
return
41+
}
42+
p := &memo[i][groups][leftK]
43+
if *p != -1 {
44+
return int64(*p)
45+
}
46+
leftK -= (a[i+1] - a[i]) * groups
47+
res = dfs(i-1, groups+1, leftK) // a[i] 作为最大值
48+
res += dfs(i-1, groups, leftK) * int64(groups+1) // 不参与最大最小:从 groups 中选一个组 这里 +1 是只有一个数的组的方案数
49+
if groups > 0 {
50+
res += dfs(i-1, groups-1, leftK) * int64(groups) // a[i] 作为最小值:从 groups 中选一个组
51+
}
52+
res %= mod
53+
*p = int(res) // 记忆化
54+
return
55+
}
56+
Fprint(out, dfs(n-1, 0, k))
57+
}
58+
59+
//func main() { CF626F(os.Stdin, os.Stdout) }

main/600-699/626F_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/contest/626/problem/F
9+
// https://codeforces.com/problemset/status/626/problem/F
10+
func TestCF626F(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
3 2
15+
2 4 5
16+
outputCopy
17+
3
18+
inputCopy
19+
4 3
20+
7 8 9 10
21+
outputCopy
22+
13
23+
inputCopy
24+
4 0
25+
5 10 20 21
26+
outputCopy
27+
1
28+
inputCopy
29+
20 1000
30+
50 50 100 100 150 150 200 200 250 250 300 300 350 350 400 400 450 450 500 500
31+
outputCopy
32+
97456952`
33+
testutil.AssertEqualCase(t, rawText, 0, CF626F)
34+
}

0 commit comments

Comments
 (0)