B-number[数位DP]

14 篇文章 0 订阅
4 篇文章 0 订阅

题意: 问一个区间内的数字满足能被13整除,且含有子序列13的数字的个数。
采用数位DP, 记录状态dp[pos][sum][statue]表示当前为pos位,前面求和mod13的值为sum,statue状态下的答案。
其中statue==0表示上一位不是1,statue==1表示上一位是1, statue==2表示前面已经出现过13序列了。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int dig[12];
LL dp[15][15][3];// 0- not 1, 1- is 1, 2 - contain 13
LL dfs(int pos, int presum, int statue, int limit)
{
    if(pos < 1) return statue==2&&presum%13==0;
    if(!limit && dp[pos][presum][statue] != -1)
        return dp[pos][presum][statue];
    int n = limit ? dig[pos] : 9;
    LL res = 0;
    for(int i = 0; i <= n; ++i)
    {
        int nowsum = (presum*10 + i)%13;
        int nowstatue = statue;
        if(statue == 0 && i == 1)
            nowstatue = 1;
        if(statue == 1 && i != 1)
            nowstatue = 0;
        if(statue == 1 && i == 3)
            nowstatue = 2;
        res += dfs(pos-1, nowsum, nowstatue, limit&&i==n);
    }
    if(!limit) dp[pos][presum][statue] = res;
    return res;
}
LL call(LL x)
{
    int len = 0;
    while(x)
    {
        dig[++len] = x%10;
        x /= 10;
    }
    return dfs(len, 0, 0, 1);
}
int main()
{
    ios::sync_with_stdio(false);
    memset(dp, -1, sizeof(dp));
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif // ONLION_JUDGE
    LL x;
    while(cin >> x)
    {
        cout << call(x) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值