Amount of Degrees

Time limit: 1.0 second
Memory limit: 64 MB

Description

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 = 24+20,
18 = 24+21,
20 = 24+22.

Input

The first line of input contains integers X and Y, separated with a space (1 ≤ X ≤ Y ≤ 231−1). The next two lines contain integers K and B (1 ≤ K ≤ 20; 2 ≤ B ≤ 10).

Output

Output should contain a single integer — the amount of integers, lying between X and Y, being a sum of exactly K different integer degrees of B.

Sample

input output
15 20
2
2
3

解题思路

题意:

思路:

#include<iostream>
#include<string>
using namespace std;
int f[32][32]; int change(int x, int b)
{
string s;
do
{
s = char('0' + x % b) + s;
x /= b;
}
while (x > 0);
for (int i = 0; i < s.size(); ++i)
if (s[i] > '1')
{
for (int j = i; j < s.size(); ++j) s[j] = '1';
break;
}
x = 0;
for (int i = 0; i < s.size(); ++i)
x = x | ((s[s.size() - i - 1] - '0') << i); //或运算,在此相当于加法
return x;
} void init()//预处理f
{
f[0][0] = 1;
for (int i = 1; i <= 31; ++i)
{
f[i][0] = f[i - 1][0];
for (int j = 1; j <= i; ++j) f[i][j] = f[i - 1][j] + f[i - 1][j - 1];
}
} int calc(int x, int k) //统计[0..x]内二进制表示含k个1的数的个数
{
int tot = 0, ans = 0; //tot记录当前路径上已有的1的数量,ans表示答案
for (int i = 31; i > 0; --i)
{
if (x & (1 << i)) //该位上是否为1
{
++tot;
if (tot > k) break;
x = x ^ (1 << i); //将这一位置0
}
if ((1 << (i - 1)) <= x)
{
ans += f[i - 1][k - tot];
}
}
if (tot + x == k) ++ans;
return ans;
} int main()
{
int x, y, k, b;
cin >> x >> y >> k >> b;
x = change(x, b);
y = change(y, b);
init();
cout << calc(y, k) - calc(x - 1, k) << endl;
return 0;
}

  

Ural Amount of Degrees(数位dp)的更多相关文章

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

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

  2. URAL 1057. Amount of Degrees(数位DP)

    题目链接 我看错题了...都是泪啊,不存在3*4^2这种情况...系数必须为1... #include <cstdio> #include <cstring> #include ...

  3. [ural1057][Amount of Degrees] (数位dp+进制模型)

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

  4. ural 1057Amount of Degrees ——数位DP

    link:http://acm.timus.ru/problem.aspx?space=1&num=1057 论文: 浅谈数位类统计问题  刘聪 #include <iostream&g ...

  5. [ACM] ural 1057 Amount of degrees (数位统计)

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

  6. 基础数位DP小结

    HDU 3555 Bomb dp[i][0] 表示含 i 位数的方案总和. sp[i][0] 表示对于位数为len 的 num 在区间[ 10^(i-1) , num/(10^(len-i)) ] 内 ...

  7. Ural1057. Amount of Degrees 题解 数位DP

    题目链接: (请自行百度进Ural然后查看题号为1057的那道题目囧~) 题目大意: Create a code to determine the amount of integers, lying ...

  8. 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 ...

  9. 2018.09.07 Amount of degrees(数位dp)

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

随机推荐

  1. Djaon 基于 Ajax 的 json 数据传输

    关键参数:contentType:"application/json" data:JSON.stringify({ json数据 }) 再views.py中,使用request.d ...

  2. VUE组件嵌套

    vue中组件嵌套烦分为两种,分别是全局注册组件和局部注册组件 基本步骤: 1.在components 下创建一个新的.vue结尾的文件,文件首字母最好是大写,基于规范复制代码 2.分别写出结构层< ...

  3. JavaScript深入之从原型到原型链(转载)

    构造函数创建对象 我们先使用构造函数创建一个对象: function Person() { } var person = new Person(); person.name = 'Kevin'; co ...

  4. centos7操作防火墙

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  5. SQL Server 2008将数据库数据导出到脚本

    1.在要到处的数据库上右键 2.选择“任务” 3.选择“生成脚本” 4.选定要导出的数据库 5.在“编写数据的脚本”处选择“True” 6.接下来选定要导出的表,然后选择“完成”

  6. Linux性能优化从入门到实战:02 CPU篇:平均负载

    每次发现系统变慢时,我们通常做的第一件事,就是执行 top 或 uptime 命令: $ uptime 22:22:17 up 2 days, 20:14, 1 user, load average: ...

  7. Linux下C语言实现ATM取款机,消息队列版本

    链接:https://pan.baidu.com/s/1oBavXBuZul7ZAEBL1eYfRA 提取码:ffhg Mybank ATM取款机实验,消息队列实现本实验用的是Centos71.在服务 ...

  8. ps:新建Photoshop图像

    从现在起我们开始正式地接触Photoshop,为了保证大家的快捷键设置与教程内容一致.请确认Photoshop的快捷键设置是默认值.可从菜单[编辑 键盘快捷键]打开快捷键设置,在组选项里面选择“Pho ...

  9. Cobaltstrike系列教程(一)-简介与安装

    0x001-Cobaltstrike简介 Cobalt Strike是一款美国Red Team开发的渗透测试神器,常被业界人称为CS.这款神器许多大佬们都已经玩的很6,我一个菜鸡玩的略有心得,因此写一 ...

  10. 基于 Flink 的实时数仓生产实践

    数据仓库的建设是“数据智能”必不可少的一环,也是大规模数据应用中必然面临的挑战.在智能商业中,数据的结果代表了用户反馈.获取数据的及时性尤为重要.快速获取数据反馈能够帮助公司更快地做出决策,更好地进行 ...