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(ref 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; dp[tar] = new int[][][][](2, 3, 2, 800); foreach (b; 0..2) { int digit = b ? 10 : N[a]-'0'+1; foreach (c; 0..3) { foreach (d; 0..2) { if (M - a < 6) { foreach (e; 0..P) { foreach (f; 0..digit) { dp[tar][b||(f