HDU 3652 数位dp

B-number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5894    Accepted Submission(s): 3389


Problem Description
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
 
Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
 
Output
Print each answer in a single line.
 
Sample Input
13 100 200 1000
 
Sample Output
1 1 2 2
 
Author
wqb0039
 
Source
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 #define bug() printf("!!!!!!!")
14 #define M 100005
15 using namespace  std;
16 ll bit[100];
17 ll dp[100][5][13];
18 ll n;
19 ll functi(int pos,ll pre,int flag ,int m)
20 {
21     if(pos==0) return (pre==2&&m==0);
22     if(flag&&dp[pos][pre][m]!=-1) return dp[pos][pre][m];
23     ll x=flag ? 9 : bit[pos];
24     ll ans=0;
25     for(ll i=0;i<=x;i++)
26     {
27         if(pre==2||(pre==1&&i==3))
28             ans+=functi(pos-1,2,flag||i<x,(m*10+i)%13);
29         else if(i==1)
30             ans+=functi(pos-1,1,flag||i<x,(m*10+i)%13);
31         else
32             ans+=functi(pos-1,0,flag||i<x,(m*10+i)%13);
33     }
34     return dp[pos][pre][m]=ans;
35 }
36 ll fun(ll x)
37 {
38     memset(dp,-1,sizeof(dp));
39     memset(bit,0,sizeof(bit));
40     int len=0;
41     while(x)
42     {
43         bit[++len]=x%10;
44         x/=10;
45     }
46     return functi(len,0,0,0);
47 }
48 int main()
49 {
50     while(scanf("%I64d",&n)!=EOF)
51     {
52         printf("%I64d
",fun(n));
53     }
54     return 0;
55 }
原文地址:https://www.cnblogs.com/hsd-/p/6597266.html