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. 嵌入式Linux驱动学习之路(十一)按键驱动-中断机制

    轮询方式: 和led驱动不同的是在配置IO引脚的时候,把LED的输出引脚换成输入,在read函数中向外发送io的状态.必须由应用程序不断的来查询当前IO口的状态来判断. 中断方式: /* file n ...

  2. POJO和VO的区别

    网上说  POJO对应DAO层中的数据库,POJO重的成员变量对于表中的每个字段. VO  为POJO的分装,与视图层交互.

  3. IIS7.5使用web.config设置伪静态的二种方法(转)

    近几天公司里开发的项目有几个运行在IIS7.5上,由于全站采用的是伪静态,因此从网上找到两两种方法来实现.这两种方法各有优势:第一种比较灵活,只要把文件拷到根目录下,即可直接显示所有伪静态页面(适用于 ...

  4. 攻克Spring

    http://www.cnblogs.com/dream-to-pku/p/5655247.html

  5. Dump中查看dictionary信息的方法

    In order to dump the contents of a dictionary, you need to start with either the MethodTable or the ...

  6. 关于 MonoDevelop on Linux 单步调试问题的解决

    在 MonoDevelop 中默认是关闭对外部程序集(.dll)的调试,可通过如下步骤来解决这个问题. 通过菜单[Edit]-[Preferences]-[Debugger]进入到调试器的设置页,把“ ...

  7. 图片轮播图插件的使用 unslider!!!

    1.百度图片轮播图unslider,第一个就会出现jquery unslider轮播图,点击进去,下载网站提供的文件,解压,内部有我们需要使用的各种js,图片等. 2.在自己的eclipse或者int ...

  8. python 列表生成式

    r = [x*x for x in range(10)] r list类型

  9. Scrapy的中Css 选择器

    //通过 名为 video_part_lists 的Class 中下面的 li 标签 liList = response.css('.video_part_lists li') for li in l ...

  10. 【USACO 2.3】Money Systems(dp)

    v种货币,求有多少种组成和为n. dp[i][j]表示前i种货币价格为j有多少种方案,dp[i][j]+=dp[i-1][j-c]. http://train.usaco.org/usacoprob2 ...