zoukankan      html  css  js  c++  java
  • 数位dp小结

    思路:逐位处理,在依次遍历十进制的每一位数字的基础上不断更新状态,从而求解dp[i][s]表示第i位,状态为s的数字的个数。

    例题一:传送门

    思路:求和,然后对N取余。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    typedef long long LL;
    LL A,B,N,a[120],dp[50][120];
    LL dfs(LL pos,LL stu,bool done)
    {
        if(pos==-1) return stu%N==0?1:0;
        if(!done&&~dp[pos][stu]) return dp[pos][stu];
        LL tp,len=(done?a[pos]:9),i,ans=0;
        for(i=0;i<=len;i++){
            tp=(stu+i)%N;
            ans+=dfs(pos-1,tp,done&&(i==len));
        }
        if(!done) dp[pos][stu]=ans;
        return ans;
    }
    LL solve(LL x)
    {
        memset(dp,-1,sizeof(dp));
        LL pos=0;
        while(x){
            a[pos++]=x%10;x/=10;
        }
        return dfs(pos-1,0,true);
    }
    int main(void)
    {
        while(~scanf("%lld%lld%lld",&A,&B,&N)){
            printf("%lld
    ",solve(B)-solve(A-1));
        }
        return 0;
    }
    View Code

    例题二(hdu-2089):传送门

    思路:判断62(前一个状态和后一个状态),判断4。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int dp[120][50],a[50],n,m;
    int dfs(int pos,int stu,bool done)
    {
        if(pos==-1) return 1;
        if(!done&&~dp[pos][stu]) return dp[pos][stu];
        int tp,len=(done?a[pos]:9),i,ans=0;
        for(i=0;i<=len;i++){
            if(i==4||(stu==6&&i==2)) continue;
            ans+=dfs(pos-1,i,done&&(len==i));
        }
        if(!done) dp[pos][stu]=ans;
        return ans;
    }
    int solve(int x)
    {
        memset(dp,-1,sizeof(dp));
        int pos=0;
        while(x){
            a[pos++]=x%10;
            x/=10;
        }
        return dfs(pos-1,0,true);
    }
    int main(void)
    {
        while(~scanf("%d%d",&n,&m)&&(n+m)){
            if(n>m){
                int tp=n;n=m;m=tp;
            }
            printf("%d
    ",solve(m)-solve(n-1));
        }
        return 0;
    }
    View Code
  • 相关阅读:
    如何将自己编写的软件放在真机上运行
    多台Mac电脑使用一个apple开发者账号
    Xcode5最初级的教程
    利用NSIS软件制作C#安装包
    C#针对DataTable进行分页方法
    Json 的日期格式转换成DateTime
    FI & RI
    Rename a TFS Project Collection
    char、varchar、nchar、nvarchar的区别
    Migration from TF Service to TF Server with the TFS Integration Platform
  • 原文地址:https://www.cnblogs.com/2018zxy/p/10332043.html
Copyright © 2011-2022 走看看