• HDU


    链接:

    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
    ", Solve(n));
        }
    
        return 0;
    }
    
    
  • 相关阅读:
    写给理工科人看的乐理(一)声学基础
    魔方最少记忆还原法
    甲乙两人互猜数字(鬼谷子问题)的逻辑推理与算法建模
    模板元编程实现素数判定
    UVa OJ 194
    UVa OJ 175
    UVa OJ 197
    UVa OJ 180
    UVa OJ 140
    判断input或者div.span等标签是否存在
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12000193.html
Copyright © 2020-2023  润新知