题目链接:传送门

思路:考虑二进制数字的情况,可以写成一个二叉树的形式,然后考虑区间[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. 【nginx】配置详解

    nginx作为web server,是最常用的网络服务器.来看下它的配置文件. 实验1:最基础的配置(可访问静态资源) 包括listen,server_name,root,index,access_l ...

  2. redis.conf配置详解(转)

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位,# 通常的格式就是 1k 5gb 4m 等酱紫:## 1k => 1000 bytes# 1kb =&g ...

  3. [z]cxf生成javaclient

    \apache-cxf-3.1.6\bin>wsdl2java -client http://192.168.129.224:8000/PricingEngine?wsdl

  4. Vmware黑屏解决方法

    此原因是显卡性能差,显示选项开启了3D加速导致的,具体修改步骤: 英文路径:VM->Settings->Hardware->Display 在右面的内容栏中将 Accelerate ...

  5. ubuntu远程桌面、VNC(转载)

    VNC 远程桌面 配置/使用 for xfce 本贴主要目的为说明如何在windows系统下远程控制xfce桌面 Ubuntu 的默认GNOME桌面可以在系统设置中直接打开远程桌面,然后用Window ...

  6. python subprocess 小例子

    #服务端import socketimport osimport subprocessphone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ...

  7. C#百度图片识别API调用返回数据包解析

    百度图片识别api接口 public static JObject GeneralBasic(string apikey,string secretkey,string path) { var cli ...

  8. LOADRUNNER重装经验

    1 装了LR的机器最好不要装UFT,两个工具共用一个配置文件,容易冲突. 2 LR11录制脚本时,支持的浏览器有:IE8及以下版本.FIREFOX,用哪个浏览器录制就要在操作系统中将其设置为默认浏览器 ...

  9. Java与go哪个更适合后端开发呢?哪个更适合新手呢?

    Java语言目前在后端开发领域有广泛的应用,尤其是大型互联网平台往往选择Java作为主要的后端编程语言.同时,Java自身的生态比较健全,也有大量的成功案例,所以采用Java做后端编程语言是一个风险比 ...

  10. CODE[VS]2494 Vani和Cl2捉迷藏

    原题链接 这里有一个结论:最多能选取的藏身点个数等于最小路径可重复点覆盖的路径总数. 所以我们可以先传递闭包,然后求最小路径点覆盖即可. #include<cstdio> #include ...