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. 动画可以暂停animation-play-state

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. Fiddler用法整理

    目 录 1 Fiddler的基本介绍 1.1 下载安装 1.2 适用平台 2 Fiddler的工作原理 3 同类工具 4 捕获非IE浏览器的会话 5 捕获不同请求的设置方法 5.1 Web HTTPS ...

  3. Python 特性?

    1.Python 是强语言类型还是弱语言类型? Python 是强类型的动态脚本语言.强类型:不允许不同类型相加.动态:不使用显示数据类型声明,且确定一个变量的类型是在第一次给它赋值的时候.脚本语言: ...

  4. 重磅 | Elasticsearch7.X学习路线图

    原文:重磅 | Elasticsearch7.X学习路线图 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.c ...

  5. Codeforces Round #425 (Div. 2) - B

    题目链接:http://codeforces.com/contest/832/problem/B 题意:给定一个好字母集合(只有小写字母,除了这些外其余都是坏字母集合),给定一个匹配模式串, 模式串只 ...

  6. 快速排序的python实现

    def quick_sort(array, left, right): if left < right: base_index = division(array, left, right) qu ...

  7. sublime下载emmet

    Emmet是一款Web前端开发工具Sublime非常有用的插件,使用仿CSS选择器的语法来生成代码,大大提高了HTML和CSS代码编写的速度.只需按住Tab键即可把一个简写展开成HTML和CSS的代码 ...

  8. 微信 ios img图片不显示问题

    使用div标签,将图片作为background .

  9. BZOJ 1233 干草堆 (单调队列优化DP)

    $ BZOJ~1233~~ $ 干草堆: (题目特殊性质) $ solution: $ 很妙的一道题目,开始看了一眼觉得是个傻逼贪心,从后往前当前层能多短就多短,尽量节省花费.但是这是DP专题,怎么会 ...

  10. 前端每日实战:43# 视频演示如何用纯 CSS 绘制一个充满动感的 Vue logo

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/zaqKPx 可交互视频教程 此视频 ...