Skip to content

Commit

Permalink
[1900][1A] CF708B 构造
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Feb 23, 2023
1 parent 8b2bb59 commit 19ae9dd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
52 changes: 52 additions & 0 deletions main/700-799/708B.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
. "fmt"
"io"
"math"
"strings"
)

// https://space.bilibili.com/206214
func CF708B(in io.Reader, out io.Writer) {
var c00, c01, c10, c11 int
Fscan(in, &c00, &c01, &c10, &c11)
f := func(c int) int {
x := (1 + int(math.Sqrt(float64(1+8*int64(c))))) / 2
if x*(x-1)/2 == c {
return x
}
return -1
}
c0, c1 := f(c00), f(c11)
if c0 < 0 || c1 < 0 {
Fprint(out, "Impossible")
return
}

if c01 == 0 && c10 == 0 {
if c00 > 0 && c11 > 0 {
Fprint(out, "Impossible")
} else if c11 == 0 {
Fprint(out, strings.Repeat("0", c0))
} else {
Fprint(out, strings.Repeat("1", c1))
}
return
}

if c01+c10 != c0*c1 {
Fprint(out, "Impossible")
return
}
left1, right0 := c10/c0, c10%c0
ans := strings.Repeat("1", left1) + strings.Repeat("0", c0-right0)
if right0 > 0 {
ans += "1" + strings.Repeat("0", right0)
c1--
}
ans += strings.Repeat("1", c1-left1)
Fprint(out, ans)
}

//func main() { CF708B(os.Stdin, os.Stdout) }
26 changes: 26 additions & 0 deletions main/700-799/708B_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"github.com/EndlessCheng/codeforces-go/main/testutil"
"testing"
)

// https://codeforces.com/problemset/problem/708/B
// https://codeforces.com/problemset/status/708/problem/B
func TestCF708B(t *testing.T) {
// just copy from website
rawText := `
inputCopy
1 2 3 4
outputCopy
Impossible
inputCopy
1 2 2 1
outputCopy
0110
inputCopy
3 8 4 6
outputCopy
1001011`
testutil.AssertEqualCase(t, rawText, 0, CF708B)
}

0 comments on commit 19ae9dd

Please sign in to comment.