AcWing 1082. 数位DP模板题(不降数的个数)
原题链接
中等
作者:
_empty
,
2020-08-24 20:30:59
,
所有人可见
,
阅读 598
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
const int N = 35;
int f[N][10];
void init()
{
for (int i = 0; i < 10; i++) f[1][i] = 1;
for (int i = 2; i <= N; i++)
for (int j = 0; j < 10; j++)
for (int k = 0; k < 10; k++)
if (k >= j) f[i][j] += f[i - 1][k];
}
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++)
if (j >= last) res += f[i + 1][j];
if (x >= last) last = x;
else break;
if (i == 0) res++;
}
return res;
}
int main()
{
int a, b;
init();
while (cin >> a >> b)
{
cout << dp(b) - dp(a - 1) << endl;
}
return 0;
}