zoukankan      html  css  js  c++  java
  • HDOJ 3652 Bnumber (数位DP)

    题意:求1-n中有多少个数含字符串”13"且能被13整除。(1 <= n <= 1000000000).

    练习数位DP的dfs写法。

    View Code
    #include <stdio.h>
    #include <string.h>
    #define N 10
    int dp[N][13][3],digit[N];
    int dfs(int pos,int r,int s,int f)
    {
        if(pos==-1) return r==0&&s==2;
        if(!f&&dp[pos][r][s]!=-1)   return dp[pos][r][s];
        int max=f?digit[pos]:9;
        int ret=0;
        for(int i=0;i<=max;i++)
        {
            int nr=(r*10+i)%13;
            int ns=s;
            if(s==0&&i==1)  ns=1;
            else if(s==1&&i!=1) ns=0;
            if(s==1&&i==3)  ns=2;
            ret+=dfs(pos-1,nr,ns,f&&i==max);
        }
        if(!f)  dp[pos][r][s]=ret;
        return ret;
    }
    int cal(int x)
    {
        int pos=0;
        while(x)
        {
            digit[pos++]=x%10;
            x/=10;
        }
        return dfs(pos-1,0,0,1);
    }
    int main()
    {
        int x;
        memset(dp,-1,sizeof(dp));
        while(~scanf("%d",&x))
        {
            printf("%d\n",cal(x));
        }
        return 0;
    }
  • 相关阅读:
    分布式 and 集群
    时间复杂度



    线性表 & 散列表
    栈 & 队列
    数组 & 链表
    数据结构&算法
    Docket 容器引擎
  • 原文地址:https://www.cnblogs.com/algorithms/p/2667634.html
Copyright © 2011-2022 走看看