AcWing 1084. 数字游戏 II
原题链接
中等
作者:
wjie
,
2020-07-16 19:03:58
,
所有人可见
,
阅读 523
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int N = 11;
int f[N][10][101];
int l, r, p;
int mod(int a, int b)
{
return (a%b+b)%b;
}
void init()
{
memset(f, 0, sizeof(f));
for (int i = 0; i <= 9; ++i) f[1][i][i%p] = 1;
for (int i = 2; i < N; ++i)
{
for (int j = 0; j <= 9; ++j)
{
for (int k = 0; k < p; ++k)
{
for (int r = 0; r <= 9; ++r)
{
f[i][j][k] += f[i-1][r][mod(k-j, p)];
}
}
}
}
}
int dp(int n)
{
if (!n) return 1;
vector<int> nums;
while (n) nums.push_back(n%10), n /= 10;
int res = 0;
int last = 0;
for (int i = nums.size()-1; i >= 0; --i)
{
int x = nums[i];
for (int j = 0; j < x; ++j)
{
res += f[i+1][j][mod(-last, p)];
}
last += x;
if (!i && last%p == 0) res++;
}
return res;
}
int main()
{
while (~scanf("%d%d%d", &l, &r, &p))
{
init();
printf("%d\n", dp(r) - dp(l-1));
}
return 0;
}