import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; immutable int MOD = 10^^9+7; int solve(string N, int P, bool less) { auto M = N.length; auto dp = new int[][][][][](2, 2, 3, 2, 800); // 桁, N未満か, mod3, 3がつくか, mod P dp[0][0][0][0][0] = 1; foreach (a; 0..M) { int cur = a % 2; int tar = 1 - cur; foreach (b; 0..2) foreach (c; 0..3) foreach (d; 0..2) foreach (e; 0..P) dp[tar][b][c][d][e] = 0; foreach (b; 0..2) { int digit = b ? 10 : N[a]-'0'+1; foreach (c; 0..3) { foreach (d; 0..2) { if (M - a < 10) { foreach (e; 0..P) { foreach (f; 0..digit) { dp[tar][b||(f