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. python 学习第四十七天shelve模块

    shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式. 1,序列化 import shelve f=shelve.open('she ...

  2. A.Gennady and a Card Game

    http://m3.codeforces.com/contest/1097/problem/A Gennady and a Card Game time limit per test 1 second ...

  3. Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板

    原文:Elasticsearch7.X 入门学习第八课笔记-----索引模板和动态模板 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...

  4. python学习笔记(10):面向对象

    一.类和实例 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 2.对象:通过类定义的数据结构实例.对象包括两个数据成员( ...

  5. 使用vee-validate表单验证插件如何设置中文提示

    版本: vee-validate v3.x设置如下: import * as rules from 'vee-validate/dist/rules'; import zh_CN from 'vee- ...

  6. PREPARE - 创建一个准备好的查询

    SYNOPSIS PREPARE plan_name [ (datatype [, ...] ) ] AS statement DESCRIPTION 描述 PREPARE 创建一个已准备好的查询. ...

  7. ARM Cortex-M底层技术(2)—启动代码详解

    杂谈 工作了一天,脑袋比较乱.一直想把底层的知识写成一个系列,希望可以坚持下去.为什么要写底层的东西呢?首先,工作用到了这部分内容,最近和内部Flash打交道比较多,自然而然会接触到一些底层的东西:第 ...

  8. LINUX VSFTP配置及安装

    ------------------转载:亲身实践,确实好用(http://www.cnblogs.com/jack-Star/p/4089547.html) 1.VSFTP简介 VSFTP是一个基于 ...

  9. css定位选择兄弟元素,nth-of-type

    <span class="input-group-btn" the-id="num-change"> <button class=" ...

  10. 2019最新create-react-app创建的react中使用sass/scss,以及在react中使用sass/scss公共变量的方法

    Sass(英文全称:Syntactically Awesome Stylesheets)是一个最初由Hampton Catlin设计并由Natalie Weizenbaum开发的层叠样式表语言.Sas ...