package main import ( "bufio" "fmt" "io" "os" "strconv" ) var iost *Iost type Iost struct { Scanner *bufio.Scanner Writer *bufio.Writer } func NewIost(fp io.Reader, wfp io.Writer) *Iost { const BufSize = 2000005 scanner := bufio.NewScanner(fp) scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, BufSize), BufSize) return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)} } func (i *Iost) Text() string { if !i.Scanner.Scan() { panic("scan failed") } return i.Scanner.Text() } func (i *Iost) Atoi(s string) int { x, _ := strconv.Atoi(s); return x } func (i *Iost) GetNextInt() int { return i.Atoi(i.Text()) } func (i *Iost) Atoi64(s string) int64 { x, _ := strconv.ParseInt(s, 10, 64); return x } func (i *Iost) GetNextInt64() int64 { return i.Atoi64(i.Text()) } func (i *Iost) Atof64(s string) float64 { x, _ := strconv.ParseFloat(s, 64); return x } func (i *Iost) GetNextFloat64() float64 { return i.Atof64(i.Text()) } func (i *Iost) Print(x ...interface{}) { fmt.Fprint(i.Writer, x...) } func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) } func (i *Iost) Println(x ...interface{}) { fmt.Fprintln(i.Writer, x...) } func isLocal() bool { return os.Getenv("NICKEL") == "BACK" } func main() { fp := os.Stdin wfp := os.Stdout if isLocal() { fp, _ = os.Open(os.Getenv("WELL_EVERYBODY_LIES_TOO_MUCH")) } iost = NewIost(fp, wfp) defer func() { iost.Writer.Flush() }() solve() } func solve() { s := iost.Text() n := len(s) ss := make([]int, n) var z int for i := 0; i < n; i++ { ss[i] = int(s[i] - '0') if ss[i] == 0 { z++ } } m := iost.GetNextInt() dp := make([][2]int, m) dp[0][0] = 1 for i := 0; i < n; i++ { dp2 := make([][2]int, m) for j := 0; j < m; j++ { for k := 0; k < 2; k++ { dp2[j][k] += dp[j][k] if dp2[j][k] >= Mod1000000007 { dp2[j][k] -= Mod1000000007 } tj := (j*10 + ss[i]) % m tk := k if ss[i] > 0 { tk = 1 } if tk == 0 { continue } dp2[tj][tk] += dp[j][k] if dp2[tj][tk] >= Mod1000000007 { dp2[tj][tk] -= Mod1000000007 } } } dp, dp2 = dp2, dp } ans := dp[0][1] + z iost.Println(ans % Mod1000000007) } // Mod constants. const ( Mod1000000007 = 1000000007 Mod998244353 = 998244353 )