数位dp——hud3652

B-number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


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
 
模板与前一篇博客是一样的,只是dp数组的意义略微做了改动。
f[pos][p][is13]表示到第pos位,除以13的余数为p,是否出现过13的数字个数。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 int f[20][14][4],bit[20],n,l;
 7 int get(int a,int b){
 8     if(b==2)return 2;
 9     return a==1?1:a==3&&b==1?2:0;
10 }
11 int dfs(int pos,int lim,int p,int is13){
12     if(pos<=0&&p==0&&is13==2)return 1;
13     if(pos<=0)return 0;
14     if(!lim&&f[pos][p][is13]!=-1)return f[pos][p][is13];
15     int rng=(lim?bit[pos]:9),ret=0;
16     for(int i=0;i<=rng;i++)
17         ret+=dfs(pos-1,lim&&(i==rng),(p*10+i)%13,get(i,is13));
18     if(!lim)f[pos][p][is13]=ret;
19     return ret;
20 }
21 
22 int main(){
23     while(scanf("%d",&n)!=EOF){
24         for(l=0;n;)bit[++l]=n%10,n/=10;
25         memset(f,-1,sizeof(f));
26         printf("%d
",dfs(l,1,0,0));
27     }
28 }
原文地址:https://www.cnblogs.com/y-m-y/p/6663896.html