Problem solution · Go

Codeforces 1380F — Strange Addition

Codeforces 1380F — Strange Addition: 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
91 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 1380F — Strange Addition, 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

  • 91 lines of Go from the credited upstream file 1380F.go.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 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 1380F — Strange Addition · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io") const m1380 = 998244353 var s1380 []byte type seg1380 []struct {	l, r int	mat  [2][2]int64} func (t seg1380) set(o, i int) {	t[o].mat = [2][2]int64{{int64(s1380[i] + 1), 1}}	if s1380[i-1] == 1 {		t[o].mat[1][0] = int64(9 - s1380[i])	}} func (seg1380) mul(a, b [2][2]int64) (c [2][2]int64) {	for i := 0; i < 2; i++ {		for j := 0; j < 2; j++ {			for k := 0; k < 2; k++ {				c[i][j] += a[i][k] * b[k][j]			}			c[i][j] %= m1380		}	}	return} func (t seg1380) maintain(o int) { t[o].mat = t.mul(t[o<<1].mat, t[o<<1|1].mat) } func (t seg1380) build(o, l, r int) {	t[o].l, t[o].r = l, r	if l == r {		t.set(o, l)		return	}	m := (l + r) >> 1	t.build(o<<1, l, m)	t.build(o<<1|1, m+1, r)	t.maintain(o)} func (t seg1380) update(o, i int) {	if t[o].l == t[o].r {		t.set(o, i)		return	}	if i <= (t[o].l+t[o].r)>>1 {		t.update(o<<1, i)	} else {		t.update(o<<1|1, i)	}	t.maintain(o)} // github.com/EndlessCheng/codeforces-gofunc CF1380F(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var n, q, i int	var d byte	Fscan(in, &n, &q, &s1380)	for i := range s1380 {		s1380[i] &= 15	}	s1380 = append([]byte{0}, s1380...)	t := make(seg1380, 4*n)	t.build(1, 1, n)	for ; q > 0; q-- {		Fscan(in, &i, &d)		s1380[i] = d		if i < n {			t.update(1, i+1)		}		t.update(1, i)		Fprintln(out, t[1].mat[0][0])	}} //func main() { CF1380F(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 ↗