topic link : [Kuangbin take you to fly] topic XV digital DP G-b-number
Test instructions
The number of numbers containing 13 and divisible by 13 in the range of the 1~n.
Ideas
First, understand such a formula: A%m = = ((b%m) *c+d)%m;
The correctness of the formula is obvious, it is not proved.
Then the judgment number can be divided into 13 divisible into one one to deal with.
Of course, we only need to store the value after the remainder.
DFS (len, num, mod, flag)
MoD records The value of the number to 13 after taking the remainder
Len indicates the current number of digits
num==0 does not contain 13 and the previous one is not 1
Pre==1 does not contain 13 and the previous one is 1
pre==2 containing
flag Indicates whether the value can be arbitrarily evaluated (judging range).
So, the memory search can be solved.
Code
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>using namespace STD;#define LL Long Long#define MODLL dp[ -][3][ -];intdis[ -]; LL DFS (intLenintTypeintMoDBOOLFlag) {if(Len <0)returnType = =2&& MoD = =0;if(!flag && dp[len][type][mod]!=-1)returnDP[LEN][TYPE][MOD];intEnd = Flag?dis[len]:9;intAns =0; for(intI=0; i<=end; i++) {if(Type = =2|| (Type = =1&& i = =3)) ans + = DFS (len-1,2, (mod*Ten+i)%mod, flag&&i==end);ElseAns + = DFS (len-1, i==1?1:0, (mod*Ten+i)%mod, flag&&i==end); }if(!flag) Dp[len][type][mod] = ans;returnAns;} ll solve (ll N) {intLen =0; while(n) {dis[len++] = n%Ten; N/=Ten; }returnDFS (len-1,0,0,1);}intMain () {intNmemset(DP,-1,sizeof(DP)); while(Cin>>n)cout<<solve (n) <<endl;return 0;}
HDU 3652 B-number (Digital dp&-memory search)