HDU 3652 B-number (Digital dp&-memory search)

Source: Internet
Author: User

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)

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.