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 ...
随机推荐
- css3制作动画性能问题
这篇文章主要讲的是怎样制作流畅动画,特别是针对移动端.在这里我首先介绍制作动画的几种方法的优缺点:接着会着重介绍用css3制作动画的注意事项. 1.用canvas.css3.jquery制作动画 Ca ...
- vlc 网页插件的 使用与控制 API
下面开始使用教程: html文档结构: <object class="vlc" type='application/x-vlc-plugin' events='True' w ...
- lrj 9.4.1 最长上升子序列 LIS
p275 d(i)是以Ai为结尾的最长上升子序列的长度 <算法竞赛入门经典-训练指南>p62 问题6 提供了一种优化到 O(nlogn)的方法. 文本中用g(i)表示d值为i的最小状态编号 ...
- python开启GPU加速
看了好多教程都提到了使用 os.environ["CUDA_VISIBLE_DEVICES"] = "1" 或者export CUDA_VISIBLE_DEVI ...
- 洛谷P3366 【模板】最小生成树 题解
题目链接:https://www.luogu.org/problem/P3366 最小生成树模板题. Kruskal算法 算法思想:给边按边权从小到大排序,然后遍历每一条边,如果边上的两个点不在同一个 ...
- 2004年NOIP普及组复赛题解
题目涉及算法: 不高兴的津津:入门题: 花生采摘:贪心: FBI树:递归.DP求区间和: 火星人:模拟. 不高兴的津津 题目链接: 简单枚举. 遍历一遍,找到 \(a[i] + b[i]\) 最大的那 ...
- Vue 路由的嵌套使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python图表绘制Matplotlib
引入 import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# 导入相关模块 使用 # 图表窗口1 → plt.sho ...
- java什么叫面向对象?
面向对象(Object-Oriented,简称OO)就是一种常见的程序结构设计方法. 面向对象思想的基础是将相关的数据和方法放在一起,组合成一种新的复合数据类型,然后使用新创建的复合数据类型作为项目的 ...
- python基础五之字典
python数据的可变性 通过数据的可变性,可将数据分为可变数据类型和不可变数据类型. 可变数据类型:list,dict (不可哈希) 不可变数据类型:元祖,bool,int,str (可哈希) py ...