数位dp——hud3652

Posted 羊毛羊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数位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\n",dfs(l,1,0,0));
27     }
28 }

 

以上是关于数位dp——hud3652的主要内容,如果未能解决你的问题,请参考以下文章

数位dp进阶(hdu2089,3652)

HDU 3652 数位dp

hdu3652 数位dp

HDU-3652(数位dp)

hdu3652 数位dp记忆化搜索

hdu-3652 B-number 数位DP