码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3652

时间:2019-08-20 22:23:46      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:tle   state   string   main   +=   printf   hdu   pid   ++   

http://acm.hdu.edu.cn/showproblem.php?pid=3652

就是个朴素的数位dp啊,但是我一直tle,毫无办法,来日再战吧。

#include<cstdio>
#include<cstring>
int dp[11][2][2][14];
int num[11];
int remain[11];
int n;
int dfs(int i,bool state,bool have,int k,bool limit) {
    if(i==0){
        if(k==0&&have)
            return 1;
        else return 0;    
    }
    if(~dp[i][state][have][k]&&!limit) return dp[i][state][have][k];
    int up=limit?num[i]:9;
    int res=0;
    for(int j=0;j<=up;j++) {
        int h=(k+j*remain[i])%13;
        res+=dfs(i-1,j==1,have||(state&&j==3),h,limit&&j==up);
    }
    if(!limit) dp[i][state][have][k]=res;
    return res;
}
int solve(int x) {
    memset(dp,-1,sizeof(dp));    
    int len=0;
    while(x) {
        len++;
        num[len]=x%10;
        x/=10;
    }
    remain[1]=1;
    for(int i=2;i<=len;i++) {
        remain[i]=remain[i-1]*10;
    }
    return dfs(len,0,0,0,1);
}
int main() {
    while(scanf("%d",&n)) {
        printf("%d\n",solve(n));
    }
    return 0;
}

 

hdu3652

标签:tle   state   string   main   +=   printf   hdu   pid   ++   

原文地址:https://www.cnblogs.com/lcezfzy/p/11385680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!