Problem solution · Go

Codeforces 498C — Array and Operations

Codeforces 498C — Array and Operations: a Go solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

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

Breadth-first search

For Codeforces 498C — Array and Operations, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 120 lines of Go from the credited upstream file 498C.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

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 498C — Array and Operations · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io") // https://github.com/EndlessChengfunc cf498C(in io.Reader, out io.Writer) {	var n, m, v int	Fscan(in, &n, &m)	type pair struct{ p, e int }	ps := make([][]pair, n)	sum := make([]int, n+1)	for i := range ps {		Fscan(in, &v)		for p := 2; p*p <= v; p++ {			if v%p > 0 {				continue			}			e := 1			for v /= p; v%p == 0; v /= p {				e++			}			ps[i] = append(ps[i], pair{p, e})		}		if v > 1 {			ps[i] = append(ps[i], pair{v, 1})		}		sum[i+1] = sum[i] + len(ps[i])	} 	st := sum[n]	end := st + 1	type nb struct{ to, rid, cap int }	g := make([][]nb, end+1)	addEdge := func(from, to, cap int) {		g[from] = append(g[from], nb{to, len(g[to]), cap})		g[to] = append(g[to], nb{from, len(g[from]) - 1, 0})	} 	for i, ps := range ps {		if i%2 == 0 {			for j, p := range ps {				addEdge(st, sum[i]+j, p.e)			}		} else {			for j, p := range ps {				addEdge(sum[i]+j, end, p.e)			}		}	} 	for range m {		var a, b int		Fscan(in, &a, &b)		a--		b--		if a%2 > 0 {			a, b = b, a		}		for i, p := range ps[a] {			for j, q := range ps[b] {				if p.p == q.p {					addEdge(sum[a]+i, sum[b]+j, 1e9)				}			}		}	} 	d := make([]int, len(g))	bfs := func() bool {		clear(d)		d[st] = 1		q := []int{st}		for len(q) > 0 {			v := q[0]			q = q[1:]			for _, e := range g[v] {				if w := e.to; e.cap > 0 && d[w] == 0 {					d[w] = d[v] + 1					q = append(q, w)				}			}		}		return d[end] > 0	}	iter := make([]int, len(g))	var dfs func(int, int) int	dfs = func(v, totalFlow int) (curFlow int) {		if v == end {			return totalFlow		}		for ; iter[v] < len(g[v]); iter[v]++ {			e := &g[v][iter[v]]			if w := e.to; e.cap > 0 && d[w] > d[v] {				f := dfs(w, min(totalFlow-curFlow, e.cap))				if f == 0 {					continue				}				e.cap -= f				g[w][e.rid].cap += f				curFlow += f				if curFlow == totalFlow {					break				}			}		}		return	}	maxFlow := 0	for bfs() {		clear(iter)		maxFlow += dfs(st, 1e9)	}	Fprint(out, maxFlow)} //func main() { cf498C(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 ↗