题目链接:



(请自行百度进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真神奇。

Ural1057. Amount of Degrees 题解 数位DP的更多相关文章

  1. Timus Online Judge 1057. Amount of Degrees(数位dp)

    1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...

  2. 2018.09.07 Amount of degrees(数位dp)

    描述 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和. 例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 17 = 24+20, ...

  3. Ural Amount of Degrees(数位dp)

    传送门 Amount of Degrees Time limit: 1.0 secondMemory limit: 64 MB Description Create a code to determi ...

  4. URAL 1057 Amount of Degrees (数位dp)

    Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactly ...

  5. Amount of Degrees(数位dp)

    题目链接:传送门 思路:考虑二进制数字的情况,可以写成一个二叉树的形式,然后考虑区间[i……j]中满足的个数=[0……j]-[0……i-1]. 所以统计树高为i,中有j个1的数的个数. 对于一个二进制 ...

  6. ural 1057 Amount of degrees 【数位dp】

    题意:求(x--y)区间转化为 c 进制 1 的个数为 k 的数的出现次数. 分析:发现其满足区间减法,所以能够求直接求0---x 的转化为 c 进制中 1 的个数为k的数的出现次数. 首先用一个数组 ...

  7. URAL 1057 Amount of Degrees (数位DP,入门)

    题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求:  17 = 24+2 ...

  8. Ural1057 - Amount of Degrees(数位DP)

    题目大意 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 输入:第一行包含两个整 ...

  9. URAL1057. Amount of Degrees(DP)

    1057 简单的数位DP  刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&g ...

随机推荐

  1. 云原生生态周报 Vol. 5 | etcd性能知多少

    业界要闻 1 Azure Red Hat OpenShift已经GA.在刚刚结束的Red Hat Summit 2019上,Azure Red Hat OpenShift正式宣布GA,这是一个微软和红 ...

  2. 从 Spark 到 Kubernetes — MaxCompute 的云原生开源生态实践之路

    2019年5月14日,喜提浙江省科学技术进步一等奖的 MaxCompute 是阿里巴巴自研的 EB 级大数据计算平台.该平台依托阿里云飞天基础架构,是阿里巴巴在10年前做飞天系统的三大件之分布式计算部 ...

  3. HZOJ 数颜色

    一眼看去树套树啊,我可能是数据结构学傻了…… 是应该去学一下莫队进阶的东西了. 上面那个东西我没有打,所以这里没有代码,而且应该也不难理解吧. 这么多平衡树就算了,不过线段树还是挺好打的. 正解3: ...

  4. 30 Cool Open Source Software I Discovered in 2013

    30 Cool Open Source Software I Discovered in 2013 #1 Replicant – Fully free Android distribution Rep ...

  5. mysql数据库之mysql下载与设置

    下载和安装mysql数据库 mysql为我们提供了开源的安装在各个操作系统上的安装包,包括ios,liunx,windows. mysql的安装,启动和基础配置-------linux版本 mysql ...

  6. hdu 3234 Exclusive-OR (并查集)

    Problem - 3234 题意不难理解,就是给出一些断言,以及一些查询,回答查询或者在找到断言矛盾以后沉默不做任何事. 这题其实就是一个并查集的距离存储问题,只要记录并查集元素的相对值以及绝对值就 ...

  7. C# 标准性能测试

    经常我写一个类,作为一个工具类,小伙伴会问我这个类的性能,这时我就需要一个标准的工具进行测试. 本文告诉大家如何使用 benchmarkdotnet 做测试. 现在在 github 提交代码,如果有小 ...

  8. Java日志框架——JCL

    JCL,全称为"Jakarta Commons Logging",也可称为"Apache Commons Logging". 一.JCL原理 1.基本原理 JC ...

  9. 通过页码直接跳转 html

    <?php namespace Admin\TagLib; class BootstrapPage{ public $firstRow; // 起始行数 public $listRows; // ...

  10. Layout 实现三栏布局的几种方法

    https://github.com/ljianshu/Blog/issues/14    布局参考 https://github.com/ljianshu/Blog/issues/38  响应式那点 ...