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

HDU - 3652 - B-number(数位DP)

时间:2019-12-07 01:12:49      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:uri   lld   cpp   str   href   判断   clu   bool   print   

链接:

https://vjudge.net/problem/HDU-3652

题意:

A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.

思路:

数位DP,记录前面的余数,和前一位的数,和是否已经存在13.四维DP。
如果不记录前一位的值,可能出现无法判断13是否存在。

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;

LL F[20][20][2][10];
LL dig[20];
LL a, b, n;

LL Dfs(int pos, int pre, int num, int ok, bool lim)
{
    if (pos == -1)
    {
        if (pre == 0 && ok)
            return 1;
        return 0;
    }
    if (!lim && F[pos][pre][ok][num] != -1)
        return F[pos][pre][ok][num];
    int up = lim ? dig[pos] : 9;
    LL ans = 0;
    for (int i = 0;i <= up;i++)
        ans += Dfs(pos-1, ((pre*10)%13+i)%13, i, ok ? ok : (num == 1 && i == 3), lim && i == up);
    if (!lim)
        F[pos][pre][ok][num] = ans;
    return ans;
}

LL Solve(LL x)
{
    int p = 0;
    while(x)
    {
        dig[p++] = x%10;
        x /= 10;
    }
    return Dfs(p-1, 0, -1, 0, 1);
}

int main()
{
    // freopen("test.in", "r", stdin);
    memset(F, -1, sizeof(F));
    while(~scanf("%lld", &n))
    {
        printf("%lld\n", Solve(n));
    }

    return 0;
}

HDU - 3652 - B-number(数位DP)

标签:uri   lld   cpp   str   href   判断   clu   bool   print   

原文地址:https://www.cnblogs.com/YDDDD/p/12000193.html

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