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. 10 个常用的 Linux 命令?

    pwd 显示工作路径ls 查看目录中的文件 cd /home 进入 '/ home' 目录'cd .. 返回上一级目录cd ../.. 返回上两级目录mkdir dir1 创建一个叫做 'dir1' ...

  2. java 回调的原理与实现

    回调函数,顾名思义,用于回调的函数.回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用(回调)时机. 回调原本应该是一个非常简 ...

  3. python时间测量

    使用自定义装饰器测量时间 def test_time(func): def inner(*args, **kw): t1 = datetime.datetime.now() print('开始时间:' ...

  4. FTP服务器原理及配置

    控制连接 21端口  用于发送ftp命令 数据连接 20端口  用于上传下载数据 数据连接的建立类型: 1主动模式: 服务器主动发起的数据连接 首先由客户端的21 端口建立ftp控制连接 当需要传输数 ...

  5. mysql 注入绕过小特性

    1. 注释 Select /*多行(单行)注释*/ version(); Select version(); #单行注释 Select version(); -- 单行注释 (两划线之后必须有空格) ...

  6. php 调用远程url

    // ; Whether to allow the treatment of URLs (like http:// or ftp://) as files. // ; http://php.net/a ...

  7. HTML5 音频播放

    代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. 01.helloworld--标签

    """参考网站:http://python.cocos2d.org/doc/programming_guide/index.html""" ...

  9. Qt 样式对于QPushbutton 增加 hover press release效果

    按钮的三种状态,未被选中,选中(划过),点击时候的效果 使用setStyleSheet即QSS样式实现. QPushButton *MyBtn = new QPushButton(this); MyB ...

  10. voc数据集坐标,coco数据集坐标

    voc,如上图 x1 ,y1 ,x4, y4    bbox的坐标格式是,x,y的最大最小值,也就是box的左上角和右下角的坐标 coco x,y,w,h       box左上角的坐标以及宽.高 图 ...