AcWing 1082. 数字游戏
原题链接
中等
作者:
wjie
,
2020-07-16 12:37:44
,
所有人可见
,
阅读 560
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 15;
int f[N][10];
void init()
{
for (int i = 0; i <= 9; ++i) f[1][i] = 1;
for (int i = 2; i < N; ++i)
{
for (int j = 0; j <= 9; ++j)
{
for (int k = j; k <= 9; ++k)
{
f[i][j] += f[i-1][k];
}
}
}
}
int dp(int x)
{
if (!x) return 1;
vector<int> nums;
while (x) nums.push_back(x%10), x /= 10;
int res = 0;
int last = 0;
for (int i = nums.size()-1; i >= 0; --i)
{
int x = nums[i];
for (int j = last; j < x; ++j) res += f[i+1][j];
if (last > x) break;
last = x;
if (!i) res++;
}
return res;
}
int main()
{
int l, r;
init();
while (~scanf("%d%d", &l, &r))
{
printf("%d\n", dp(r)-dp(l-1));
}
return 0;
}