"Hdoj 3652" B-number
To an integer n find <=n integer can be divisible by 13 and contains 13 of the Digital DP memory!!
One into the memory deep like the sea ... I don't want to use recursion anymore ... It's really good to find that if the condition is satisfied, record the unchecked high (that is, the number of random picks) and keep searching for recursion.
The code is as follows:
#include <iostream>#include <cstdio>#include <cstring>using namespace Std;intdp[Ten][ -][3];intdigit[Ten];/*HS = 0 No 131 No 13 12 appears 13mod indicates the result after the high-level take-off after the completion of the MoD = = 0 said that the name can be divisible by 13 to take the remainder *intDfsint POS,intMoDintHs,bool high) {if(POS== -1)returnHS = =2&&!mod;if(!high && ~dp[POS][MOD][HS])returndp[POS][MOD][HS];intI,en,ans =0, NHS,NMD; En = high? digit[POS]:9; for(i =0; I <= en; ++i) {NMD = (mod*10+i)%13; NHS = HS;if(NHS = =1&& i = =3) NHS =2;Else if(NHS! =2) NHS = (i = =1)?1:0; Ans + = DFS (POS-1, Nmd,nhs,high && i = = en); }if(!high) dp[POS][MOD][HS] = ans;returnAns;}intSolve (int x){intLen =0; while(x) {digit[len++] =x%10;x/=Ten; }returnDFS (len-1,0,0,1);}intMain () {memset (dp,-1, sizeof (DP));intN while(~SCANF ("%d", &n))printf("%d\ n", Solve (n));return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Hdoj 3652" B-number