【YBTOJ】【HDU3652】B-number
【YBTOJ】【HDU3652】B-number
题目大意:
查询 \([1,n]\) 中有多少个数包含 \(13\) 并能被其整除。
正文:
数位 DP 板子题。设 \(f_{len,x,mod,flag,pos}\) 表示,在第 \(len\) 位时,其数为 \(x\),且当前余数是 \(mod\),是否有 \(13\) 出现,是否到顶的方案数。
代码:
const int N = 15;
inline ll Read()
{
ll x = 0, f = 1;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = -f, c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x * f;
}
ll n;
int a[N], Len;
ll f[N][N][N][2][2];
ll DP (int len, int x, int mod, bool flag, bool pos)
{
if (~f[len][x][mod][flag][pos]) return f[len][x][mod][flag][pos];
if (!len) return flag && !mod;
int m = pos? a[len]: 9;
ll ans = 0;
for (int i = 0; i <= m; i++)
ans += DP(len - 1, i, (mod * 10 + i) % 13, (x == 1 && i == 3) || flag, pos && i == m);
return f[len][x][mod][flag][pos] = ans;
}
ll Solve (ll n)
{
Len = 0;
memset (a, 0, sizeof a);
memset (f, -1, sizeof f);
for (ll m = n; m; m /= 10)
a[++Len] = m % 10;
ll ans = 0;
for (int i = 0; i <= a[Len]; i++)
ans += DP(Len - 1, i, i, 0, i == a[Len]);
return ans;
}
int main()
{
while (~scanf ("%lld", &n))
{
printf ("%lld\n", Solve (n));
}
return 0;
}