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 ...
随机推荐
- 学习meta标签http-equiv属性
meta标签http-equiv属性的使用:meta标签http-equiv属性的使用
- HTML标题title滚动
上代码: <script type="text/javascript"> var msg = document.title; msg = "…" + ...
- h5 的canvas绘制基本图形
文章地址:https://www.cnblogs.com/sandraryan/ canvas是一个标签,可用于绘制复杂图形,渲染效果比普通DOM快 某些低版本浏览器不支持 canvas 使用原生几乎 ...
- 2019-3-8-win10-uwp-渲染原理-DirectComposition-渲染
title author date CreateTime categories win10 uwp 渲染原理 DirectComposition 渲染 lindexi 2019-03-08 09:18 ...
- 使用java实现CNN的实战
使用java实现CNN的实战 1.要实现CNN,其中包括 卷积.池化(下采样).分类器.优化方法.分类器.反向传播 2.可以使用一个三维数组来表示一张图片(通道.行.列) 3.卷积,卷积的方式有三种: ...
- 软件自动化测试 selenium IDE + Firebug + python脚本
按顺序步骤来 一.安装软件 1.1.1 webDriver(就是selenium IDE) 解析:本来这两个东西就合成一个了,但是更新到后来,安装的时候又独立安装的. 安装 Python ...
- Python--day47--mysql索引种类
- 2019-8-24-win10-uwp-读取文本GBK错误
title author date CreateTime categories win10 uwp 读取文本GBK错误 lindexi 2019-8-24 16:2:27 +0800 2018-2-1 ...
- java异常处理格式
异常处理的5个关键字 try ,catch, finally throw, throws 我的总结: 捕获异常:先捕获小异常再捕获大异常. 程序是调出来的,不是写出来的:多测试是程序员的必修课. ...
- H3C配置Hybrid端口