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:
\]
\]
\]
解题思路:
数位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的更多相关文章
- 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 ...
- 2018.09.07 Amount of degrees(数位dp)
描述 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和. 例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 17 = 24+20, ...
- Ural Amount of Degrees(数位dp)
传送门 Amount of Degrees Time limit: 1.0 secondMemory limit: 64 MB Description Create a code to determi ...
- 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 ...
- Amount of Degrees(数位dp)
题目链接:传送门 思路:考虑二进制数字的情况,可以写成一个二叉树的形式,然后考虑区间[i……j]中满足的个数=[0……j]-[0……i-1]. 所以统计树高为i,中有j个1的数的个数. 对于一个二进制 ...
- ural 1057 Amount of degrees 【数位dp】
题意:求(x--y)区间转化为 c 进制 1 的个数为 k 的数的出现次数. 分析:发现其满足区间减法,所以能够求直接求0---x 的转化为 c 进制中 1 的个数为k的数的出现次数. 首先用一个数组 ...
- URAL 1057 Amount of Degrees (数位DP,入门)
题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求: 17 = 24+2 ...
- Ural1057 - Amount of Degrees(数位DP)
题目大意 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 输入:第一行包含两个整 ...
- URAL1057. Amount of Degrees(DP)
1057 简单的数位DP 刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&g ...
随机推荐
- @NOI模拟2017.06.30 - T3@ Right
目录 @description@ @solution@ @part - 1@ @part - 2@ @accepted code@ @details@ @description@ JOHNKRAM 和 ...
- 使用sqlyog链接多个主机的数据库
- hdu 2225 The nearest fraction (数学题)
Problem - 2225 一道简单数学题,要求求出一个分母不超过m的最接近sqrt(n)的分数. 做法就是暴力枚举,注意中间过程不能用浮点数比较,误差要求比较高. 代码如下: #include & ...
- python不得不知的几个开源项目
1.Trac Trac拥有强大的bug管理 功能,并集成了Wiki 用于文档管理.它还支持代码管理工具Subversion ,这样可以在 bug管理和Wiki中方便地参考程序源代码. Trac有着比较 ...
- HTML静态网页--表单验证和事件
1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...
- npm镜像及配置方法
npm全称Node Package Manager,是node.js的模块依赖管理工具.由于npm的源在国外,所以国内用户使用起来各种不方便.下面整理出了一部分国内优秀的npm镜像资源,国内用户可以选 ...
- CSS滤镜 :灰色 ,方便站点哀悼
html { -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); ...
- canvas.toDataURL 由于跨域报错的解决方法
关于canvas.toDataURL 由于跨域报错的解决方法 用过canvas,都知道toDataURL这个方法真好用,不仅合成图片用到它,压缩图片也用到它.但有一个问题,就是图片源不能跨域,不然会报 ...
- 1471 - Defense Lines
After the last war devastated your country, you - as the king of the land of Ardenia - decided it wa ...
- H3C DHCP中继显示及维护