题目链接:传送门

思路:考虑二进制数字的情况,可以写成一个二叉树的形式,然后考虑区间[i……j]中满足的个数=[0……j]-[0……i-1]。

所以统计树高为i,中有j个1的数的个数。

对于一个二进制数字,求出每次向右转时的左子树内的个数。

对于非二进制数字,就转换为二进制数字后再求解。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ;
int dp[maxn][maxn];
typedef long long LL;
void Init() //初始化,dp[i][j]代表高度为i的二进制树中恰好有j个1的数的个数
{
dp[][]=;
for(int i=;i<=;i++){
dp[i][]=dp[i-][];
for(int j=;j<=i;j++)
dp[i][j]=dp[i-][j]+dp[i-][j-];
}
}
int change(int x,int b) //b进制转换为2进制
{
int tot=,ans=;LL tmp=;
while(tmp*b<=x){
tmp*=b;tot++;
}
while(tmp){
if(x>=tmp){
ans+=(<<tot);
x-=tmp;
}
tmp/=b;tot--;
}
return ans;
}
int cal(int x,int k) //求二进制中k个数组成x的组合的个数
{
int tot=,ans=;
for(int i=;i>;i--){
if(x&(<<i)){ //统计当前位是否为1
tot++;
if(tot>k) break;
x=x^(<<i);
}
if((<<(i-))<=x) ans+=dp[i-][k-tot]; //统计左子树中的个数
}
if(tot+x==k) ans++; //判断端点是否由k个数组成
return ans;
}
int main(void)
{
int x,y,k,b;
Init();
while(~scanf("%d%d%d%d",&x,&y,&k,&b)){
printf("%d\n",cal(change(y,b),k)-cal(change(x-,b),k));
}
return ;
}

Amount of Degrees(数位dp)的更多相关文章

  1. Ural1057 - Amount of Degrees(数位DP)

    题目大意 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 输入:第一行包含两个整 ...

  2. [ural1057][Amount of Degrees] (数位dp+进制模型)

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

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

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

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

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

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

  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. Ural1057. Amount of Degrees 题解 数位DP

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

  8. 2018.09.07 Amount of degrees(数位dp)

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

  9. Ural Amount of Degrees(数位dp)

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

  10. 【数位DP】[LOJ10163]Amount of Degrees

    发现自己以前对数位DP其实一窍不通... 这题可以做一个很简单的转换:一个数如果在$b$进制下是一个01串,且1的个数恰好有k个,那么这个数就是合法的(刚开始没判断必定是01串,只判断了1的个数竟然有 ...

随机推荐

  1. MySQL之开启远程连接

    MySQL安装时,默认只能本地连接. mysql -u root -p mysql>use mysql; mysql>select 'host' from user where user= ...

  2. java初学网站

    http://www.jfinal.com/doc http://www.w3school.com.cn/

  3. c#: 以模态窗口显示于其它进程窗体之前

    产品之工具箱,需要工具以模态窗体,显示于主界面之上.记下代码点,以做备忘. 1.IWin32Window internal class Win32Window : IWin32Window { pub ...

  4. Linux 线程】线程同步《四》

    1.信号量 (1)概念 信号量和互斥锁(mutex)的区别:互斥锁只允许一个线程进入临界区,而信号量允许多个线程同时进入临界区. 不多做解释,要使用信号量同步,需要包含头文件semaphore.h. ...

  5. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  6. linux命令学习之:chmod

    chmod命令用来变更文件或目录的权限.在Linux系统家族里,文件或目录权限的控制分别以读取R.写入W.执行X3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目录 ...

  7. go语言处理文件上传和多个文件上传

    uploadOne.html代码如下: <!doctype html> <html lang="en"> <head> <meta cha ...

  8. mysql中如何使用一句话将一个表的数据导入到另一个表中:insert into ...select

    INSERT INTO cdcd2015(`filename`,`xmlfile`,`updatetime`,`isDel`) SELECT `filename`,`xmlfile`,`updatet ...

  9. java的几个日志框架log4j、logback、common-logging

    开发工作中每个系统都需要记录日志,常见的日志工具有log4j(用的最多),slf4j,commons-loging,以及最近比较流行的logback 以前只是在项目中用log4j,更多的是参考下配置文 ...

  10. spring boot 项目属性配置

    一.系统配置文件 1.application.properties是新建springboot项目后默认得配置文件 配置的示例如下 server.port= server.context-path=/g ...