• hdu3652 B-number[数位dp]


    B-number

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6020    Accepted Submission(s): 3473


    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
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    const int N=20;
    ll n,f[N][13][3];int T,bits[N];
    ll dfs(int pos,int mod,int st,bool lim){
        if(!pos) return st==2&&!mod;
        ll &res=f[pos][mod][st],ans=0;
        if(!lim&&(~res)) return res;
        int up=!lim?9:bits[pos];
        for(int i=up;~i;i--){
            int p=(mod*10+i)%13;
            if(st==2||(st==1&&i==3))
                ans+=dfs(pos-1,p,2,lim&&i==bits[pos]);
            else if(i==1)
                ans+=dfs(pos-1,p,1,lim&&i==bits[pos]);
            else 
                ans+=dfs(pos-1,p,0,lim&&i==bits[pos]);
        }
        if(!lim) res=ans;
        return ans;
    }
    ll solve(ll x){
        int len=0;
        for(;x;x/=10) bits[++len]=x%10;
        return dfs(len,0,0,1);
    }
    int main(){
        memset(f,-1,sizeof f);
        while(~scanf("%I64d",&n))
            printf("%I64d
    ",solve(n));
        return 0;
    }
     
  • 相关阅读:
    k8s应用持久化存储和StorageClass(10)
    K8s控制器Statefulset(11)
    LeetCode面试题 08.11. 硬币(完全背包)
    打怪兽问题
    二叉树两个节点的最近公共祖先问题
    复制带随机指针的链表
    二叉树中最大的二叉搜索子树的大小
    零钱兑换问题
    Netty 学习(九):解码源码说明
    二叉树的直径和最大距离问题
  • 原文地址:https://www.cnblogs.com/shenben/p/6707665.html
Copyright © 2020-2023  润新知