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

HDU - 3652 B-number

时间:2018-10-11 22:00:33      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:include   name   line   lld   ace   --   string   ==   etc   

B-number

找出 \([1, X]\) 中满足:

  1. 子串含 "13"
  2. 能被 \(13\) 整除的数
    的个数

错误日志: \(B == 1\) 时也可以转移到 \(1\) (没有分析清楚转移)


Solution

数位\(dp\)
满足两个性质: 能被 \(13\) 整除 , 子串含 \(13\)
状态设计: \(dp[maxn][0/1/2][0 - 12]\) 两个状态,
状态一
\(0 -->\) 无特殊性质
\(1 -->\) 数以 \(1\) 结尾
\(2 -->\) 数中含 \("13"\) 子串
状态二
次数对 \(13\) 取模的结果

状态二比较好转移, 状态一转移如下:

LL cmd = 0;
if((B == 0 || B == 1) && i == 1)cmd = 1;
else if(B == 1 && i == 3)cmd = 2;
else if(B == 2)cmd = 2;

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(LL i = (x);i <= (y);i++)
using namespace std;
LL RD(){
    LL out = 0,flag = 1;char c = getchar();
    while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
const LL maxn = 19;
LL num[maxn];
LL dp[maxn][3][maxn];//数位,是否含13,除13余数多少
LL DP(LL Index, LL B, LL mod, bool limit){
    if(Index == 0)return ((B == 2) && (mod == 0));
    if(!limit && dp[Index][B][mod] != -1)return dp[Index][B][mod];
    LL ans = 0, up = limit ? num[Index] : 9;
    REP(i, 0, up){
        LL cmd = 0;
        if((B == 0 || B == 1) && i == 1)cmd = 1;
        else if(B == 1 && i == 3)cmd = 2;
        else if(B == 2)cmd = 2;
        ans += DP(Index - 1, cmd, ((mod * 10 + i) % 13 + 13) % 13, limit && i == num[Index]);
        }
    if(!limit)dp[Index][B][mod] = ans;
    return ans;
    }
LL solve(LL x){
    LL len = 0;
    while(x){
        num[++len] = x % 10;
        x /= 10;
        }
    return DP(len, 0, 0, 1);
    }
int main(){
    memset(dp, -1, sizeof(dp));
    LL x;
    while(scanf("%lld", &x) != EOF){
        printf("%lld\n", solve(x));
        }
    return 0;
    }

HDU - 3652 B-number

标签:include   name   line   lld   ace   --   string   ==   etc   

原文地址:https://www.cnblogs.com/Tony-Double-Sky/p/9774709.html

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