Description

Create a code to determine the amount of integers, lying in the set [ XY] and being a sum of exactly K different integer degrees ofB.
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.

Input

The first line of input contains integers X and Y, separated with a space (1 ≤  X ≤  Y ≤ 2 31−1). The next two lines contain integers Kand 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

input output
15 20
2
2
3

Source

Problem Source: Rybinsk State Avia Academy 
 
题解:

所求的数为互不相等的幂之和,亦即其B进制表示的各位数字都只能是0和1。 因此,我们只需讨论二进制的情况,其他进制都可以转化为二进制求解。

设f[i,j]代表i位二进制数中恰好有j个1的数的个数。其实这个就是组合数。

最后的问题就是如何处理非二进制。 对于询问n,我们需要求出不超过n的最大B进制表示只含0、1的数:找到n的左起第一位非0、1的数位,将它变为1,并将右面所有数位设为1。 将得到的B进制表示视为二进制进行询问即可。 如n = (10204)9进制 n = (10111)2进制

code:

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxl=;
int l,r,k,b,c[maxl][maxl],len,a[maxl],ans;
void init(){
for (int i=;i<=;i++) c[i][]=;
for (int i=;i<=;i++) for (int j=;j<=i;j++) c[i][j]=c[i-][j-]+c[i-][j];
}
int calc(int n){
int t=n,len=,res=,ans=;
memset(a,,sizeof(a));
while (t) a[++len]=t%b,t/=b;
for (int i=len;i;i--) if (a[i]!=&&a[i]!=){
for (;i;i--) a[i]=;
break;
}
for (int i=len;i;i--) if (a[i]) ans+=c[i-][k-res],res++;
return ans;
}
int main(){
init();
read(l),read(r),read(k),read(b);
printf("%d\n",calc(r+)-calc(l));
return ;
}

ural1057Amount of Degrees的更多相关文章

  1. [TimusOJ1057]Amount of Degrees

    [TimusOJ1057]Amount of Degrees 试题描述 Create a code to determine the amount of integers, lying in the ...

  2. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  3. <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies

    本题链接:http://poj.org/problem?id=2139 Description:     The cows have been making movies lately, so the ...

  4. Economics degrees

    Economics degrees Name game"> 经济学学位"> 名称痕戏 Luring students with a new label 新瓶旧酒吸引学生 ...

  5. 一本通1585【例 1】Amount of Degrees

    1585: [例 1]Amount of Degrees 时间限制: 1000 ms         内存限制: 524288 KB 题目描述 原题来自:NEERC 2000 Central Subr ...

  6. opencv: Rotate image by 90, 180 or 270 degrees

    opencv2: void rotate_cw(const cv::Mat& image, cv::Mat& dest, int degrees) { ) { : dest = ima ...

  7. POJ2139--Six Degrees of Cowvin Bacon(最简单Floyd)

    The cows have been making movies lately, so they are ready to play a variant of the famous game &quo ...

  8. 树形DP ---- Codeforces Global Round 2 F. Niyaz and Small Degrees引发的一场血案

    Aspirations:没有结果,没有成绩,acm是否有意义?它最大的意义就是让我培养快速理解和应用一个个未知知识点的能力. ————————————————————————————————————— ...

  9. POJ 2139 Six Degrees of Cowvin Bacon (floyd)

    Six Degrees of Cowvin Bacon Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Ja ...

随机推荐

  1. PHP——四种基本排序算法

    分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序. $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序 思路分析 ...

  2. textarea文本域

    textarea中换行问题 一旦用nl2br函数处理会多个\n导致十分难看 也无法形成文本框默认效果 前台的html如果用<br/>处理也会给后台的编辑造成困难 所以在编辑的时候这样处理 ...

  3. HDU 4288 线段树+离散化

    题意: n个操作 在[1, 100000]  的区间上add 或del数( 必不会重复添加或删除不存在的数) sum 求出整个集合中 (下标%5 == 3 位置) 的数   的和 注意数据类型要64位 ...

  4. centos 正确 安装 jdk

    1>先找下centos下java默认安装的情况 whereis java which java (java运行路径) echo $JAVA_HOME echo $PATH 2> 查看并卸载 ...

  5. [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)

    Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. Hibernate的游离态与持久态转换

    在Hibernate中,一个PO可能经过长时间的操作,session已过时关闭,此时PO已经是一个游离态的对象,这时要转换为持久战态,有下面几种方法: 1.session.saveOrUpdate(o ...

  7. Java theory and practice: Thread pools and work queues--reference

    Why thread pools? Many server applications, such as Web servers, database servers, file servers, or ...

  8. iOS--日历事件的获取和添加

    日历添加事件 EKEventStore* eventStore = [[EKEventStore alloc] init];//获取日历类 EKEvent *event = [EKEvent even ...

  9. ios-点击屏幕,隐藏键盘

    ios-点击屏幕,隐藏键盘 - (void)getFirstRegist{ //结束键盘编辑 __weak typeof(self)weakSelf = self; UITapGestureRecog ...

  10. css考核点整理(八)-在什么情况下通过img引入图片,什么情况用背景图引入?背景属性有哪些

    在什么情况下通过img引入图片,什么情况用背景图引入?背景属性有哪些