-
Note: The intervals in this article are all closed intervals
-
Explanation video
00: 00 − − \small 00:00 --~00:00−- Digitaldp \ small dpd p principle explanation
02: 50 − − \small 02:50 --~02:50−− "The number of degrees" train of thought
21: 40 − − \small 21:40 --~21:40−− "The number of degrees" code explanation
36: 20 − − \small 36:20 --~36:20−− Explanation of theidea of “number game”
48: 00 − − \small 48:00 --~48:00−− "Number Game" code explanation
56: 50 − − \small 56:50 --~56:50−− "Windy number" mentality explained
65: 00 − − \small 65:00 --~65:00−− "Windy number" code explanation
83: 30 − − \small 83:30 --~83:30−− "Number Game II" Train of Thought
94: 20 − − \small 94:20 --~94:20−− "Number Game II" code explanation
104: 10 − − \small 104:10 --~104:10−− "Don’t 62" train of thought
115: 10 − − \small 115:10 --~115:10−− “Don’t 62” code explanation
127: 40 − − \small 127:40 --~127:40−− "Hate 7 to break a wife" train of thought to explain
145: 10 − − \small 145:10 --~145:10−− "Hate 7 not getting married" code explanation -
The problem solved by digital dp : find a given interval [L, R] \small [L,R][L,R ] , in line with the functionf (x) \small f(x)The number of f ( x ) .
Example:
f (x) \small f(x)f ( x ) : do not decrease the number-x \small xx must satisfy the relationship that the digits from left to right are less than or equal to. Such as123 \small 123123, 446 \small446 446
L = 1 \small L=1 L=1 , \small , , R = 19 \small R=19 R=1 9
Answer:18 \small 181 8
Explanation: Only10 \small 101 0 is not a non-decreasing number, the rest are non-decreasing numbers, so the answer is18 \small 1818 -
Digital dp skills :
1, 1,1、 d p ( L , R ) = d p ( 0 , R ) − d p ( 0 , L − 1 ) \small dp(L,R)=dp(0,R)-dp(0,L-1) dp(L,R)=dp(0,R)−dp(0,L−1 )
For[L, R] \small [L, R][L,R ] Interval problem, we generally convert it into two orderdp \small dpd p , ie find[0, R] \small [0, R][0,R ] and[0, L − 1] \small [0,L-1][0,L−1 ] Two paragraphs, and then subtract the results to get the[L, R] \small [L,R]we need[L,R ]
Example: Find the sum of the interval2, 2through the prefix sum
2 , painting tree -
template
#include<vector>
#include<iostream>
using namespace std;
const int N = 100;
int f[N][N];
int u[N];
//根据题目要求来预处理f[i][j]
void init() {
}
//计算[0,n]符合f(x)的个数
int dp(int n) {
//如果L可以取到0的话,特判-1
if (n == -1) return 0;
//特判0
if (n == 0) return 0 / 1;
//用于把每位的数字提取出来
vector<int> vt;
while (n) vt.push_back(n % 10), n /= 10;
//res记录返回值,last保留前缀的信息
int res = 0, last = 0;
for (int i = vt.size() - 1; i >= 0; i--) {
//第一种放法
for (int j = 0; j < vt[i]; j++) {
if (true) res += f[][];
}
//第二种放法就是vt[i]本身,特判vt[i]是否和法,不合法直接退出就好
if (false) break;
}
//返回答案
return res;
}
int main() {
//预处理需要的数据
init();
int l, r;
while (cin >> l >> r) {
//技巧一
cout << dp(r) - dp(l - 1) << endl;
}
return 0;
}