Question:
Evaluate the number of integers between 1 and N that contain 13 and can be divisible by 13.
Ideas:
Digit DP. Note that the remainder must be recorded in the condition of division, that is, the modulus.
# Include <cstdio> # include <cstring> using namespace STD; int DP [12] [15] [2] [12]; int bit [12]; // pos: number of digits processed num: modulo 13 remainder T: existing 13 E: Current tail flag: whether to enter the Upper Limit Range int DFS (INT POs, int num, bool T, int e, bool flag) {If (Pos =-1) // The return T &&(! Num); // determine whether there are still 13 Conditions for Division. If yes, 1 If (! Flag & DP [POS] [num] [T] [e]! =-1) // you have not reached the upper limit and have already calculated it. You can directly enter the return DP [POS] [num] [T] [E]; int end = flag? Bit [POS]: 9; // The maximum value of this bit. If the previous number does not reach the upper limit, this bit can be obtained to the maximum value of 9. otherwise, it is the int ans = 0 of N; For (INT I = 0; I <= end; I ++) {// The next DP, the remainder can be so accumulated (?), Whether there are 13 or just 13, ending number, whether this bit is still upper limit ans + = DFS (pos-1, (Num * 10 + I) % 13, T | (E = 1 & I = 3), I, flag & (I = END);} If (! Flag) DP [POS] [num] [T] [e] = ans; // if it is not the upper limit, this bit can be used for general and memory-based search. return ans;} int solve (int n) {int Pos = 0; while (n) {bit [POS ++] = n % 10; N/= 10 ;} return DFS (pos-1, 0, 0, 0, 1); // The final flag is only initialized to 1 to ensure that subsequent results are not affected. explanation .. still do not need to explain} int main () {int N; memset (DP,-1, sizeof (DP); While (scanf ("% d", & N) = 1) {printf ("% d \ n", solve (N ));}}