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. 安装rabbitmq集群

    一.安装 erlang.rabbitmq 在10.0.0.45.10.0.0.57.10.0.0.58三个节点上安装,然后开启 RabbitMQ 监控插件 以下在root用户操作 1./etc/hos ...

  2. C基础

    一.关于整型数据 1.整型常量:十进制数前面可以加+.-号,但是不能有前缀0 八进制数:必须以前缀0开头,不是O.不能加负号(-),否则不能识别. 十六进制数:前缀必须为0x或者0X.不能加负号(-) ...

  3. jersey构建rest服务返回json数据

    1.  eclipse 创建 dynamic web project 2.  将jersey相关jar包放到libs目录下 3. web.xml 增加 jersey 相关内容 <?xml ver ...

  4. PHP代码为什么不能直接保存HTML文件——&gt;PHP生成静态页面教程

    1.server会依据文件的后缀名去进行解析,假设是HTML文件则server不会进行语法解析.而是直接输出到浏览器. 2.假设一个页面中所有都是HTML代码而没有须要解析的PHP语法,则没有必要保存 ...

  5. Delphi TFindDialog TReplaceDialog对话框在Memo中的使用

    Delphi TFindDialog TReplaceDialog对话框的使用 下载地址1: http://download.csdn.net/detail/teststudio/6408383   ...

  6. UVA 10465 Homer Simpson(dp + 完全背包)

    Problem C: Homer Simpson Time Limit: 3 seconds Memory Limit: 32 MB Homer Simpson, a very smart guy, ...

  7. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  8. [转] Android LocalService与RemoteService理解

    前段时间被别人问到相关的问题,没有回答对,发现自己原来理解的有偏差,最近看了下,写了个小Demo实验了下,现在将其记录下来,以后千万别犯同样的错误就好了. 一.LocalService(本地服务) 不 ...

  9. iOS UIKit:viewController之定义(2)

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  10. 第一篇:python高级之函数

    python高级之函数   python高级之函数 本节内容 函数的介绍 函数的创建 函数参数及返回值 LEGB作用域 特殊函数 函数式编程 1.函数的介绍 为什么要有函数?因为在平时写代码时,如果没 ...