Problem solution · Go

Codeforces 1608D — Dominoes

Codeforces 1608D — Dominoes: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
105 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 1608D — Dominoes, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 105 lines of Go from the credited upstream file 1608D.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 1608D — Dominoes · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"slices") // https://github.com/EndlessChengconst mod08 = 998244353 func pow08(x, n int) (res int) {	res = 1	for ; n > 0; n /= 2 {		if n%2 > 0 {			res = res * x % mod08		}		x = x * x % mod08	}	return} type comb08 struct{ _f, _invF []int } func newComb08(mx int) *comb08 {	c := &comb08{[]int{1}, []int{1}}	c._grow(mx)	return c} func (c *comb08) _grow(mx int) {	n := len(c._f)	c._f = slices.Grow(c._f, mx+1)[:mx+1]	for i := n; i <= mx; i++ {		c._f[i] = c._f[i-1] * i % mod08	}	c._invF = slices.Grow(c._invF, mx+1)[:mx+1]	c._invF[mx] = pow08(c._f[mx], mod08-2)	for i := mx; i > n; i-- {		c._invF[i-1] = c._invF[i] * i % mod08	}} func (c *comb08) f(n int) int {	if n >= len(c._f) {		c._grow(n * 2)	}	return c._f[n]} func (c *comb08) invF(n int) int {	if n >= len(c._f) {		c._grow(n * 2)	}	return c._invF[n]} func (c *comb08) c(n, k int) int {	if k < 0 || k > n {		return 0	}	return c.f(n) * c.invF(k) % mod08 * c.invF(n-k) % mod08} func cf1608D(in io.Reader, out io.Writer) {	cm := newComb08(0)	var n, q, w int	var s string	bad, allBW, allWB := 1, 1, 1	Fscan(in, &n)	for range n {		Fscan(in, &s)		u, v := s[0], s[1]		if u == 'W' {			w++		} else if u == '?' {			q++		}		if v == 'W' {			w++		} else if v == '?' {			q++		} 		// 如果没有 BB 和 WW,那么同时包含 BW 和 WB 的情况非法(减去只包含 BW 和只包含 WB 的情况)		if u == '?' && v == '?' {			bad = bad * 2 % mod08 // 可以变成 BW 也可以变成 WB		} else if u == v {			// 有 BB 和 WW,可以让同时包含 BW 和 WB 的情况合法			// c(q, n-w) 保证了在有 BB 的情况下,一定有 WW			bad = 0		}		if u == 'W' || v == 'B' {			allBW = 0		}		if u == 'B' || v == 'W' {			allWB = 0		}	} 	Fprint(out, (cm.c(q, n-w)-bad+allBW+allWB+mod08)%mod08)} //func main() { cf1608D(bufio.NewReader(os.Stdin), os.Stdout) } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗