"Hdoj 3652" B-number

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.