您的位置:首页 > 其它

[hdu3652][B-number] (数位dp)

2017-01-12 13:34 323 查看

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


Solution

学了用记忆化搜索解数位dp题,很直观

状态为f[position][modnum][13?][limited],也就是当前到了数字第几位,模数为多少,是否已经包含了13,当前位可以填的数字是从0~9还是从0~输入的数字的当前位

#include <stdio.h>
#include <memory.h>
int n,len,a[11],f[11][14][3];
int dfs(int x,int md,int cov,bool lim) {
if(!x)
return !md && cov==2;
if(!lim && ~f[x][md][cov])
return f[x][md][cov];
int res=0,num=lim?a[x]:9,modo,cav;
for(int i=0; i<=num; i++) {
modo=(md*10+i)%13;
cav=cov;
if(!cov && i==1)
cav=1;
if(cov==1 && i^1)
cav=i==3?2:0;
res+=dfs(x-1,modo,cav,lim && i==num); }
if(!lim)f[x][md][cov]=res;
return res; }
int main() {
while(~scanf("%d",&n)) {
memset(f,-1,sizeof f);
for(len=0; n; n/=10)
a[++len]=n%10;
printf("%d\n",dfs(len,0,0,true)); }
return 0; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: