- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 65 lines of Go from the credited upstream file 893E.go.
- The implementation keeps its working state in language-native values and containers.
- 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.
Use this to learn the idea, then write your own version.
1package main2 3import (4 "bufio"5 . "fmt"6 "io"7)8 910func CF893E(_r io.Reader, _w io.Writer) {11 in := bufio.NewReader(_r)12 out := bufio.NewWriter(_w)13 defer out.Flush()14 const mx = 1 << 2015 lpf := [mx + 1]int{1: 1}16 for i := 2; i <= mx; i++ {17 if lpf[i] == 0 {18 for j := i; j <= mx; j += i {19 if lpf[j] == 0 {20 lpf[j] = i21 }22 }23 }24 }25 const mod int64 = 1e9 + 726 F := [mx + 1]int64{1}27 for i := 1; i <= mx; i++ {28 F[i] = F[i-1] * int64(i) % mod29 }30 pow := func(x, n int64) int64 {31 res := int64(1)32 for ; n > 0; n >>= 1 {33 if n&1 == 1 {34 res = res * x % mod35 }36 x = x * x % mod37 }38 return res39 }40 invF := [...]int64{mx: pow(F[mx], mod-2)}41 for i := mx; i > 0; i-- {42 invF[i-1] = invF[i] * int64(i) % mod43 }44 C := func(n, k int) int64 { return F[n] * invF[k] % mod * invF[n-k] % mod }45 pow2 := [mx + 1]int64{1}46 for i := 1; i <= mx; i++ {47 pow2[i] = pow2[i-1] << 1 % mod48 }49 50 var t, n, m int51 for Fscan(in, &t); t > 0; t-- {52 ans := int64(1)53 Fscan(in, &n, &m)54 for n > 1 {55 p, e := lpf[n], 156 for n /= p; lpf[n] == p; n /= p {57 e++58 }59 ans = ans * C(e+m-1, e) % mod60 }61 Fprintln(out, ans*pow2[m-1]%mod)62 }63}64 65