P2110: The Tale of Boss Huan Shouting Up at the Building — Solution
A Godlike Approach
After reading the solutions I felt really gutted: it doesn't even need digit DP! Let's first consider a partial solution. When \(num<10\), the answer is just \(num\). If \(num \geq 10\), the answer is \(9+\frac{x}{10}\). Let's see: consider the answer for the number \(1921\). We can consider that the \(192-\) part contains \(\lfloor \frac{1920}{10} \rfloor\) pairs of numbers whose first and last digits are the same. Then we add the answer for when there is only one digit.
We also have to consider this: if the highest digit of \(num\) is greater than the lowest digit, the answer has to be decreased by one. For example, the number \(62\) needs extra consideration, because on the surface the second digit has \(6\) pairs of numbers matching the first digit, but that's not actually so: since \(66>62\), this possibility has to be excluded.
Here's the code:
// P2110.cpp
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;
ll L, R;
ll dp[20][20][20];
ll query(ll num)
{
if (num <= 9)
return num;
ll sum = 9 + num / 10;
ll inner = num % 10;
ll highest = num;
while (highest >= 10)
highest /= 10;
if (highest > inner)
sum--;
return sum;
}
int main()
{
scanf("%lld%lld", &L, &R);
printf("%lld", query(R) - query(L - 1));
return 0;
}