题目链接:



(请自行百度进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. 宝塔linux

    宝塔linux linux 定时任务管理

  2. RequestMapping中produces属性作用

    注解RequestMapping中produces属性可以设置返回数据的类型以及编码,可以是json或者xml: @RequestMapping(value="/xxx",prod ...

  3. 洛谷 1602 Sramoc问题

    Description 话说员工们整理好了筷子之后,就准备将快餐送出了,但是一看订单,都傻眼了:订单上没有留电话号码,只写了一个sramoc(k,m)函数,这什么东西?什么意思?于是餐厅找来了资深顾问 ...

  4. 2018-9-28-WPF-自定义-TextBoxView-的-Margin-大小

    title author date CreateTime categories WPF 自定义 TextBoxView 的 Margin 大小 lindexi 2018-09-28 17:16:17 ...

  5. sdk uncaught third Error Cannot assign to read only property 'constructor' of object '#<V>' (小程序)

    sdk uncaught third Error Cannot assign to read only property 'constructor' of object '#<V>' 在a ...

  6. Python--day37--多进程

    1,创建多进程(父进程和子进程) import os import time #多进程都要导入multiprocessing from multiprocessing import Process d ...

  7. H3C 常用的IPv6地址类型及格式

  8. H3C保存当前配置--用户图示(console)以上

    <H3C>save         //此种保存只默认保存为Startup.cfg ,系统默认是加载此文件 The current configuration will be writte ...

  9. javascript修改css样式表

    //创建var sheet=document.createElement('style');document.bodt.appendChild(sheet);sheet.styleSheet.cssT ...

  10. Linux 内核提交和控制一个 urb

    当驱动有数据发送到 USB 设备(如同在驱动的 write 函数中发生的), 一个 urb 必须被 分配来传送数据到设备. urb = usb_alloc_urb(0, GFP_KERNEL); if ...