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; }

 

相关文章:

  • 2022-12-23
  • 2021-11-08
  • 2021-07-31
  • 2021-08-19
  • 2022-01-30
  • 2021-11-08
猜你喜欢
  • 2021-05-28
  • 2021-05-21
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-16
相关资源
相似解决方案