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. 【149】ArcGIS Desktop 10.0 & Engine 10.0 安装及破解

    写在前面:可能会出现按照此方法无法破解的情况,那请确保您有将 ArcGIS 10.0 已经完全卸载干净,直接通过控制面板进行卸载的时候并不能将其卸载干净,需要进行更深层次的卸载,包括删除注册表,各种文 ...

  2. CentOS下网卡启动、配置等ifcfg-eth0教程

    步骤1.配置/etc/sysconfig/network-scripts/ifcfg-eth0 里的文件. CentOS6.4 下的ifcfg-eth0的配置详情: [root@Jeffery]# v ...

  3. UVaLive 6834 Shopping (贪心)

    题意:给定 n 个商店,然后有 m个限制,去 c 之前必须先去d,问你从0到n+1,最短路程是多少. 析:我们我们要到c,必须要先到d,那么举个例子,2 5, 3 7,如果我们先到5再到2,再到7再到 ...

  4. Rabbitmq笔记一

    几个基本概念 Producer 生产者,发送消息的一方,图中左侧的client. Consumer 消费者,接收消息的一方,图中后侧的client. Broker 消息中间件的服务节点,一般一个Rab ...

  5. [转]MVC之 自定义过滤器(Filter)

    本文转自:http://www.cnblogs.com/kissdodog/archive/2013/01/21/2869298.html 一.自定义Filter 自定义Filter需要继承Actio ...

  6. C#模拟百度登录并到指定网站评论回帖(三)

    上次说到怎么获取BAIDUID,这个相信很多人都能够拿到就不多说了,今天一连说两个,获取token和raskey 2.利用以上获得的cookie直接访问页面 https://passport.baid ...

  7. Spring Boot (33) 分布式锁

    上一篇中使用的Guava Cache,如果在集群中就不可以用了,需要借助Redis.Zookeeper之类的中间件实现分布式锁. 导入依赖 在pom.xml中需要添加的依赖包:stater-web.s ...

  8. WordPress强制跳转https教程

    在互联网火热的今天,安全问题显得越来越重要,为了用户信息安全,很多热门网站都启用了https 有小伙伴就问:我启用了https,为什么访问的时候显示的还是http呢? 其实,有时候并不是因为我们ssl ...

  9. sublime 自定义快捷键

    [ { "keys": ["alt+space"], "command": "auto_complete" }, // ...

  10. JavaScript(八)日期对象

    Date对象 1.创建方式 var now = new Date(); //现在返回的直接就是 当前的时间 不需要进行换算了   返回格式  (星期 月 日 年 时 分 秒 时区) 2.日期的格式化方 ...