HDU - 3652 B-number

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;
    }

转载于:https://www.cnblogs.com/Tony-Double-Sky/p/9774709.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值