Skip to content

Commit d6cc6bd

Browse files
committed
[2100][1A] CF1593F 二染色 搜索
1 parent 89fe00c commit d6cc6bd

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

main/1500-1599/1593F.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
. "fmt"
5+
"io"
6+
)
7+
8+
// github.com/EndlessCheng/codeforces-go
9+
func CF1593F(in io.Reader, out io.Writer) {
10+
abs := func(x int) int {
11+
if x < 0 {
12+
return -x
13+
}
14+
return x
15+
}
16+
17+
var T, n, ma, mb int
18+
var s string
19+
for Fscan(in, &T); T > 0; T-- {
20+
Fscan(in, &n, &ma, &mb, &s)
21+
minD, ans := n, "-1"
22+
t := make([]byte, n)
23+
vis := [40][40][40][41]bool{}
24+
var f func(i, x, y, c int)
25+
f = func(i, x, y, c int) {
26+
if i == n {
27+
if x == 0 && y == 0 && abs(n-c*2) < minD {
28+
minD = abs(n - c*2)
29+
ans = string(t)
30+
}
31+
return
32+
}
33+
if vis[i][x][y][c] {
34+
return
35+
}
36+
vis[i][x][y][c] = true
37+
t[i] = 'R'
38+
f(i+1, (x*10+int(s[i]&15))%ma, y, c+1)
39+
t[i] = 'B'
40+
f(i+1, x, (y*10+int(s[i]&15))%mb, c)
41+
}
42+
f(0, 0, 0, 0)
43+
Fprintln(out, ans)
44+
}
45+
}
46+
47+
//func main() { CF1593F(os.Stdin, os.Stdout) }

main/1500-1599/1593F_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/1593/F
9+
// https://codeforces.com/problemset/status/1593/problem/F
10+
func TestCF1593F(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
4
15+
5 3 13
16+
02165
17+
4 2 1
18+
1357
19+
8 1 1
20+
12345678
21+
2 7 9
22+
90
23+
outputCopy
24+
RBRBR
25+
-1
26+
BBRRRRBB
27+
BR`
28+
testutil.AssertEqualCase(t, rawText, 0, CF1593F)
29+
}

0 commit comments

Comments
 (0)