Problem solution · Go

Codeforces 145C — Lucky Subsequence

Codeforces 145C — Lucky Subsequence: 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
90 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 145C — Lucky Subsequence, 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

  • 90 lines of Go from the credited upstream file 145C.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 145C — Lucky Subsequence · GoGo
Use this to learn the idea, then write your own version.
package main import (	. "fmt"	"io"	"slices"	"strconv"	"strings") // https://github.com/EndlessChengconst mod45 = 1_000_000_007 type comb45 struct{ _f, _invF []int } func (c *comb45) _grow(mx int) {	pow := func(x, n int) int {		res := 1		for ; n > 0; n /= 2 {			if n%2 > 0 {				res = res * x % mod45			}			x = x * x % mod45		}		return res	}	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 % mod45	}	c._invF = slices.Grow(c._invF, mx+1)[:mx+1]	c._invF[mx] = pow(c._f[mx], mod45-2)	for i := mx; i > n; i-- {		c._invF[i-1] = c._invF[i] * i % mod45	}} func (c *comb45) f(n int) int {	if n >= len(c._f) {		c._grow(n * 2)	}	return c._f[n]} func (c *comb45) invF(n int) int {	if n >= len(c._f) {		c._grow(n * 2)	}	return c._invF[n]} func (c *comb45) c(n, k int) int {	if k < 0 || k > n {		return 0	}	return c.f(n) * c.invF(k) % mod45 * c.invF(n-k) % mod45} func cf145C(in io.Reader, out io.Writer) {	var n, k, tot, ans int	var s string	Fscan(in, &n, &k)	cnt := map[int]int{}	for range n {		Fscan(in, &s)		if strings.Count(s, "4")+strings.Count(s, "7") == len(s) {			v, _ := strconv.Atoi(s)			cnt[v]++			tot++		}	} 	f := make([]int, min(tot, k)+1)	f[0] = 1	for _, c := range cnt {		for j := len(f) - 1; j > 0; j-- {			f[j] = (f[j] + f[j-1]*c) % mod45		}	} 	cm := &comb45{[]int{1}, []int{1}}	for i, v := range f {		ans = (ans + v*cm.c(n-tot, k-i)) % mod45	}	Fprint(out, ans)} //func main() { cf145C(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 ↗