Discription

Create a code to determine the amount of integers, lying in the set [XY] 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 = 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 integersK 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.

Example

input output
15 20
2
2
3

Solution

中文大意:给定你一个区间[X,Y],问区间内可以表示为k个b的次方和的数有多少

可以套用进制模型,k个b的次方的和,转化为b进制后即含有k个1的b进制数(当然,其余为0,因为正好是幂的和,所以只可能包含了0或1)

先考虑简单的二进制情况,按照套路,我们可以对数的个数预处理一下

设f[i][j]表示i长度的二进制数正好含有j个1的个数

f[i][j]=f[i-1][j-1]+f[i-1][j]

处理b进制中大于1的情况:把求出的b进制串从高到低枚举,第一个大于1的位视为1,之后也全部视为1

之后把b进制视为2进制求解即可(满足区间减法,相减一下就行,当然因为算的时候都没有考虑这个限制数本身,所以用Ans(Y+1)-Ans(x)才能求出正解)

#include<stdio.h>
int f[][],n,m,_k,_b,len,zt[];
void bin_P() {
for(int i=; i<=; i++) {
f[i][]=,f[i][]=i;
for(int j=; j<=i; j++)
f[i][j]=f[i-][j-]+f[i-][j]; } }
int getans(int x) {
for(len=; x; x/=_b)
zt[++len]=x%_b;
int res=,q=_k;
for(int i=len; i; i--)
if(zt[i]) {
if(zt[i]>)
return res+f[i-][q-]+f[i-][q];
else res+=f[i-][q],q--;
if(q<)return res; }
return res; }
int main() {
bin_P();
scanf("%d%d%d%d",&n,&m,&_k,&_b);
printf("%d\n",getans(m+)-getans(n));
return ; }

[ural1057][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

    题目链接: (请自行百度进Ural然后查看题号为1057的那道题目囧~) 题目大意: Create a code to determine the amount of integers, lying ...

  4. URAL1057. Amount of Degrees(DP)

    1057 简单的数位DP  刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&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. Balanced Numbers (数位dp+三进制)

    SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...

  7. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

    Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...

  8. ural1057 Amount of Degrees

    链接 这题有一点小坑点 就是AX^B  A只能为0或者1  ,剩下的就比较好做的了. #include <iostream> #include<cstdio> #include ...

  9. ural 1057Amount of Degrees ——数位DP

    link:http://acm.timus.ru/problem.aspx?space=1&num=1057 论文: 浅谈数位类统计问题  刘聪 #include <iostream&g ...

随机推荐

  1. RDA UMF进程 & UMF_IR.C 遥控处理

    SIS架构图: SW Structure APP Event Flow :消息分发流程 UMF进程: int umf_main(int argc, char* argv[]) { umf_Init() ...

  2. Centos7 配置防火墙 firewall

    一.firewall 1.从CentOS7开始,默认使用firewall来配置防火墙,没有安装iptables(旧版默认安装). 2.firewall的配置文件是以xml的格式,存储在 /usr/li ...

  3. 如何在BCGControlBar工程的工具栏里面新增下拉列表控件

    通常情况下,工具栏里面都是一些按钮和图片,很少可以看到下拉列表控件,但是在某些应用场合,也需要用到下拉列表控件.今天在这里就简单讲解下如何在工具栏里添加下拉列表控件.   添加的过程也比较简单,在CM ...

  4. E20171106-hm

    pulldown   adj. 折叠式的; pulldown menu 下拉菜单

  5. visual studio使用dos命令在生成项目时复制文件到指定目录

    本人使用软件:vs2015 拷贝“项目1”的 bin目录 下, 项目配置的名称(“Release”,“Debug”)目录下,所有内容到“项目2”输出目录(存在直接覆盖): xcopy $(Soluti ...

  6. bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害【最小割】

    枚举建图.jpg 一开始建的图挂了,于是枚举了几种建图方式-- 因为要删点,所以拆点,连接(i,i',1),对于原来图上的边(u,v),连接(u',v,inf),(v',u,inf),然后连接(s,i ...

  7. 使用nginx和tomcat配置反向代理和动静分离

    背景 本人主修的编程语言是Java语言,因此最开始接触的Web开发也是JSP技术.使用Java开发的Web应用需要部署在专门的服务器程序上运行,比如Tomcat.但是一般很少会有人将Tomcat作为用 ...

  8. DotnetCore(1)尝鲜构建Web应用

    在上篇文章中DotnetCore环境安装完成后,现在我们来尝试构建Web应用. 新建文件夹NetCoreWebDemo,并cd进入NetCoreWebDemo文件夹 同时Ctrl+shift按下快捷键 ...

  9. hdu5922Minimum’s Revenge

    Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  10. 国内使用pip / pip with GFW / pip 镜像

    sudo pip install -i https://pypi.doubanio.com/simple/ YOUR_PACKAGE_NAME --trusted-host pypi.doubanio ...