HDU 3652 B-number

B-number

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


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
 
解题:数位dp
 
dp[i][j][k]表示前i位 余数为j 状态为k
k取0表示不包含13
1表示i+1位取了1
2表示包含13
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 15;
 4 int dp[maxn][maxn][3],b[maxn],n;
 5 int dfs(int p,int st,int mod,bool flag) {
 6     if(!p) return st == 2 && mod == 0;
 7     if(flag && dp[p][mod][st] != -1) return dp[p][mod][st];
 8     int ret = 0,u = flag?9:b[p];
 9     for(int i = 0; i <= u; ++i) {
10         int tmp = (mod*10 + i)%13;
11         if(st == 2 || st == 1 && i == 3) ret += dfs(p-1,2,tmp,flag||(i < u));
12         else if(i == 1) ret += dfs(p-1,1,tmp,flag||(i < u));
13         else ret += dfs(p-1,0,tmp,flag||(i < u));
14     }
15     if(flag) dp[p][mod][st] = ret;
16     return ret;
17 }
18 int solve(int x) {
19     int len = 0;
20     while(x) {
21         b[++len] = x%10;
22         x /= 10;
23     }
24     return dfs(len,0,0,0);
25 }
26 int main() {
27     memset(dp,-1,sizeof dp);
28     while(~scanf("%d",&n)) printf("%d
",solve(n));
29     return 0;
30 }
View Code
 
原文地址:https://www.cnblogs.com/crackpotisback/p/4782238.html