Problem solution · Go

Codeforces 1580B — Mathematics Curriculum

Codeforces 1580B — Mathematics Curriculum: a Go solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Depth-first search
Source
EndlessCheng Codeforces Go
Length
74 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

Depth-first search

For Codeforces 1580B — Mathematics Curriculum, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 74 lines of Go from the credited upstream file 1580B.go.
  • The implementation visibly relies on sequence storage, cached states.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 1580B — Mathematics Curriculum · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") // https://github.com/EndlessChengfunc cf1580B(in io.Reader, out io.Writer) {	var n, m, k, mod int	Fscan(in, &n, &m, &k, &mod)	const mx = 100	F := [mx]int{1}	for i := 1; i < mx; i++ {		F[i] = F[i-1] * i % mod	}	C := [mx][mx]int{}	for i := 0; i < mx; i++ {		C[i][0] = 1		for j := 1; j <= i; j++ {			C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod		}	} 	memo := make([][][]int, m)	for i := range memo {		memo[i] = make([][]int, n+1)		for j := range memo[i] {			memo[i][j] = make([]int, k+1)			for p := range memo[i][j] {				memo[i][j][p] = -1			}		}	}	var dfs func(int, int, int) int	dfs = func(dep, size, need int) int {		if dep < 0 {			if need > 0 {				return 0			}			return F[size] // 随便排		}		if size == 0 {			return 1		} 		p := &memo[dep][size][need]		if *p >= 0 {			return *p		} 		if dep == 0 { // 这是我们要找的			need--		} 		res := 0		for leftSz := range size {			for leftNeed := max(need-(size-1-leftSz), 0); leftNeed <= min(leftSz, need); leftNeed++ {				leftRes := dfs(dep-1, leftSz, leftNeed)				if leftRes == 0 { // 剪枝,右子树不递归					continue				}				rightRes := dfs(dep-1, size-1-leftSz, need-leftNeed)				res = (res + C[size-1][leftSz]*leftRes%mod*rightRes) % mod			}		}		*p = res		return res	}	Fprint(out, dfs(m-1, n, k))} //func main() { cf1580B(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 ↗