What is digital dp ? Digital dp is a special kind of dynamic programming. The memorandum method (dfs+record) is often used to deal with the problem of digital dp. The digit dp is a violent enumeration of each digit of the integer. In the process of enumeration, use dp[i][j] to record the possibility of enumeration, so as to avoid repetition
Problems that can be solved by digital dp Digital dp has a single type of question, and generally speaking, digital dp is used for solving 求取区间[l,r]中满足某个条件P(i)的数字的个数,l和r很大,一般为数十亿. Basically seeing the above conditions means that the question is a digital dp
The basic idea of digital dp The templates of digital dp are fixed, all the questions can use the same set of templates, just change the number. For convenience, we often use the memorandum method to deal with digital dp. The processing steps are as follows
Divide the interval [l,r]into [1,l-1]and [1,r]. [l,r]At this time , the number of numbers satisfying the condition in the solution is the solution[0,r]-[0,l-1]
Enumerate each bit of the number starting from the highest bit i, and use depth-first traversal to list all the numbers in [0,r]and and record the number of numbers that meet the conditions[0,l-1]
Memory optimization, violent enumeration of each number in 2, some combinations are repeated during the enumeration process, we can use dp to store these repeated combinations.
Use 23 to calculate [1,r]and [1,l-1]directly subtract to get the final result
Digital dp frequently asked questions
Digital dp generally has a large data space and cannot use an array to store the state. Generally, 嵌套哈希表the storage state is usedmap<int,map<int,int>>
When enumerating r and l-1, initialize the state before enumerating
Core code The core code of digital dp is dfs. dfs is universal, and the same set of dfs can be used for each digital dp problem, just change the parameters. The state of digital dp is generally all parameters in dfs except bool type. The specific situation and specific analysis, but the state is definitely a parameter in dfs.