package main import . "fmt" const M = 1e9+7 func main() { var a int var b string Scan(&a,&b) x := len(b)-1 ans := 0 ap := 1 for p := 0; p < x; p++ { aap := ap*a%M cnt := (aap + M-1) + M-ap + 1 ans += p*cnt%M ans %= M ap = aap } bm := 0 for _, ch := range b { bm = bm*a%M + int(ch - '0') bm %= M } ans += x*(bm + M-ap + 1)%M ans %= M Println(ans) } /* 考察 x = floor(log_A(B)) = strlen(B) - 1 p = log_A(A^p) p+1 = log_A(A^(p+1)) A^p <= v < A^(p+1) p = floor(log_A(v)) pとなる値vは (A^(p+1) - 1) - A^p + 1 個 xとなる値は B - A^x + 1 個 Σ[p=1...x](A^(p+1)-1 - A^p + 1) + (B - A^x + 1) mod 10^9+7 が答え xは最大でも10^5 1 <= p < x まで A^p mod 10^9+7 をA倍していく動的計画法で求めて B mod 10^9+7 をBの上位桁から動的計画法で求めて */