zoukankan      html  css  js  c++  java
  • [hdu3652][B-number] (数位dp)

    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
    ",dfs(len,0,0,true)); }
        return 0; }
  • 相关阅读:
    day84
    模型层之单表操作
    Django的模板层
    Django框架导读
    创建Django项目
    名称空间2.0path
    js基础之BOM和DOM
    LG5003 跳舞的线
    20191003 「HZOJ NOIP2019 Round #8」20191003模拟
    LG3092 「USACO2013NOV」No Change 状压DP
  • 原文地址:https://www.cnblogs.com/keshuqi/p/6277856.html
Copyright © 2011-2022 走看看