Description

问[L,R]中有多少能表示k个b次幂之和.

Sol

数位DP.

当2进制时.

建出一个二叉树, \(f[i][j]\) 表示长度为 \(i\) 有 \(j\) 个1的个数.

递推式就是左右子树之和 \(f[i][j]=f[i-1][j-1]+f[i-1][j]\)

将b进制变成2进制来做.

因为发现系数只能是1,所以找到一个b进制下极大的2进制就行..我觉得我好像讲不明白..

我计算的是小于 x 满足条件的...我觉得这样比较好写,也不用特判最后的情况..

复杂度 \(O(log^2 n)\)

Code

#include <cstdio>
#include <iostream>
using namespace std; #define debug(a) cout<<#a<<"="<<a<<" "
const int N = 64;
typedef long long LL; LL l,r,k,b;
LL f[N][N]; void init() {
f[0][0]=1;
for(int i=1;i<32;i++) {
f[i][0]=1;
for(int j=1;j<=i;j++) f[i][j]=f[i-1][j]+f[i-1][j-1];
}
// for(int i=0;i<6;i++) {
// for(int j=0;j<=i;j++) cout<<f[i][j]<<" ";cout<<endl;
// }
}
LL calc(LL x) {
LL t=1,r=0,c=0;
for(;t<x;t*=b,c++);
for(;x;t/=b,c--) if(x>=t) {
if(x>=2*t) {
return r+(1<<(c+1))-1;
}else {
x%=t,r+=1<<c;
}
}
return r;
}
int DP(LL x) {
int r=0,c=k;
for(int i=31;~i;i--) {
if(x & (1LL<<i)) {
if(c>=0) r+=f[i][c];
c--;
// debug(i),debug(f[i][c])<<endl;
}
}return r;
}
int main() {
// freopen("in.in","r",stdin);
init(); cin>>l>>r>>k>>b; // debug(calc(r)),debug(calc(l-1))<<endl;
// debug(DP(calc(r))),debug(DP(calc(l-1)))<<endl; cout<<DP(calc(r+1))-DP(calc(l))<<endl;
return 0;
}

  

Ural 1057 Amount of Degrees的更多相关文章

  1. [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 ...

  2. URAL 1057 Amount of Degrees (数位dp)

    Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactly ...

  3. URAL 1057. Amount of Degrees(数位DP)

    题目链接 我看错题了...都是泪啊,不存在3*4^2这种情况...系数必须为1... #include <cstdio> #include <cstring> #include ...

  4. ural 1057 Amount of degrees 【数位dp】

    题意:求(x--y)区间转化为 c 进制 1 的个数为 k 的数的出现次数. 分析:发现其满足区间减法,所以能够求直接求0---x 的转化为 c 进制中 1 的个数为k的数的出现次数. 首先用一个数组 ...

  5. URAL 1057 Amount of Degrees (数位DP,入门)

    题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求:  17 = 24+2 ...

  6. Timus Online Judge 1057. Amount of Degrees(数位dp)

    1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...

  7. 【Ural】【1057】Amount of degrees

    数位DP 2009年刘聪<浅谈数位类统计问题> 例题一 从组合数 以及 数位DP的角度都可以做…… 首先转化成求1~n内K进制下只有0.1的数的个数: 考虑K进制下第一个为1的位,剩下的数 ...

  8. Ural Amount of Degrees(数位dp)

    传送门 Amount of Degrees Time limit: 1.0 secondMemory limit: 64 MB Description Create a code to determi ...

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

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

随机推荐

  1. zookeeper安装

    http://blog.itpub.net/27099995/viewspace-1394831/ http://blog.csdn.net/huwei2003/article/details/491 ...

  2. 匈牙利算法与KM算法

    匈牙利算法 var i,j,k,l,n,m,v,mm,ans:longint; a:..,..]of longint; p,f:..]of longint; function xyl(x,y:long ...

  3. iOS下的按钮css去除原生样式

    IOS环境下的按钮都是经过美化的,但通常我们在设计web app的时候不需要这些看上去老土的样式,所以,去除这些显得很有必要. 下面这句代码就是重置这些样式的: input[type=button]{ ...

  4. 一个五年 Android 开发者百度、阿里、聚美、映客的面试心经

    花絮 也许会有人感叹某些人的运气比较好,但是他们不曾知道对方吃过多少苦,受过多少委屈.某些时候就是需要我们用心去发现突破点,然后顺势而上,抓住机遇,那么你将会走向另外一条大道,成就另外一个全新的自我. ...

  5. [CareerCup] 6.6 Toggle Lockers 切换锁的状态

    6.6 There are 100 closed lockers in a hallway. A man begins by opening all 100 lockers. Next, he clo ...

  6. 也谈面试必备问题之 JavaScript 数组去重

    Why underscore (觉得这部分眼熟的可以直接跳到下一段了...) 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. ...

  7. APIPA(Automatic Private IP Addressing,自动专用IP寻址)

    APIPA APIPA(Automatic Private IP Addressing,自动专用IP寻址),是一个DHCP故障转移机制.当DHCP服务器出故障时, APIPA在169.254.0.1到 ...

  8. Maven_profile_使用profile配置不同环境的properties(实践)

    配置方法分为以下几个步骤: 1.配置profiles节点(pom.xml) 2.配置build节点(pom.xml)--如果不配置该节点则无法找到profile中的properties属性值,并且配置 ...

  9. ubuntu搭建shad(-_-)owscoks(影梭)

    准备步骤 apt-get updateapt-get install python-gevent python-pippip install shadowsocks 新建一个json文件内容如下,文件 ...

  10. OpenLayers的定制

    最近因为工作的需要,把主流的的一些GIS的javascript库看了一遍,主要是ArcGIS Server API for Javascript,Openlayers和Leaflet. 先说说ArcG ...