ural1057Amount of Degrees
2016-03-03 10:10
441 查看
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 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
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:
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 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 |
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=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1; for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar()); if (ok) x=-x; } const int maxl=33; int l,r,k,b,c[maxl][maxl],len,a[maxl],ans; void init(){ for (int i=0;i<=32;i++) c[i][0]=1; for (int i=1;i<=32;i++) for (int j=1;j<=i;j++) c[i][j]=c[i-1][j-1]+c[i-1][j]; } int calc(int n){ int t=n,len=0,res=0,ans=0; memset(a,0,sizeof(a)); while (t) a[++len]=t%b,t/=b; for (int i=len;i;i--) if (a[i]!=0&&a[i]!=1){ for (;i;i--) a[i]=1; break; } for (int i=len;i;i--) if (a[i]) ans+=c[i-1][k-res],res++; return ans; } int main(){ init(); read(l),read(r),read(k),read(b); printf("%d\n",calc(r+1)-calc(l)); return 0; }
相关文章推荐
- C#更改tabControl选项卡颜色的方法
- 更换UITextView的换行键为完成键
- ubuntu批量改文件名
- Java基础系列5:访问控制权限
- Java中hashCode的作用
- 彻底理解position与anchorPoint
- linux下C语言实现静态IP地址,网关的设置
- 加人CSDN
- java.net.URISyntaxException的简单处理方法
- python metaclass __new__ __init__ ___call__
- 北京Uber优步司机奖励政策(3月3日)
- .Net Discovery 系列之五--深入浅出.Net实时编译机制(上)
- c语言实现tree数据结构
- 借用layer让弹层不限制在iframe内部
- 很有用的PHP笔试题系列三
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- glide的基本使用以及原理
- imagePickerController 获取图片的拍照时间等信息
- 基础总结篇之二:Activity的四种launchMode
- iOS个人整理20-UITableViewCell自定义,cell高度的自适应(纯代码)