zoukankan      html  css  js  c++  java
  • Ural1057. Amount of Degrees 题解 数位DP

    题目链接:

    (请自行百度进Ural然后查看题号为1057的那道题目囧~)

    题目大意:
    Create a code to determine the amount of integers, lying in the set \([X;Y]\) and being a sum of exactly \(K\) different integer degrees of B.
    Example. Let \(X=15, Y=20, K=2, B=2\) . By this example 3 numbers are the sum of exactly two integer degrees of number 2:

    \[17 = 2^4+2^0, \]

    \[18 = 2^4+2^1, \]

    \[20 = 2^4+2^2. \]

    解题思路:
    数位DP。
    建立一个函数 dfs(int pos, int k, bool limit) ,其中:

    • pos 表示当前所处的数位;
    • k 表示当前还剩几个位置需要填数;
    • limit 表示当前是否仍处于闲置状态。

    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int K, B, f[33][22], a[33];
    void init() {
        memset(f, -1, sizeof(f));
    }
    int dfs(int pos, int k, bool limit) {
        if (k == 0) return 1;
        if (pos < 0) return 0;
        if (!limit && f[pos][k] != -1) return f[pos][k];
        int up = limit ? a[pos] : 9;
        int tmp = 0;
        for (int i = 0; i <= up && i <= 1; i ++) {
            tmp += dfs(pos-1, k-i, limit && i==up);
        }
        if (!limit) f[pos][k] = tmp;
        return tmp;
    }
    int get_num(int x) {
        int pos = 0;
        while (x) {
            a[pos++] = x % B;
            x /= B;
        }
        return dfs(pos-1, K, true);
    }
    int X, Y;
    int main() {
        init();
        cin >> X >> Y >> K >> B;
        cout << get_num(Y) - get_num(X-1) << endl;
        return 0;
    }
    

    今天晚上睡早了导致大半夜睡不着了,起来刷一道题目再睡,数位DP真神奇。

  • 相关阅读:
    HDU 1686 Oulipo(kmp)
    openstack介绍以及流程
    openstack组件介绍
    linux之sort
    linux-ls命令
    CSRF-跨域访问保护
    WEB聊天
    python之路-Django进阶
    python之路-Django
    python之路-jQuery
  • 原文地址:https://www.cnblogs.com/quanjun/p/11968577.html
Copyright © 2011-2022 走看看