Amount of Degrees(数位dp)
题目链接:传送门
思路:考虑二进制数字的情况,可以写成一个二叉树的形式,然后考虑区间[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)的更多相关文章
- Ural1057 - Amount of Degrees(数位DP)
题目大意 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和.例如,设X=15,Y=20,K=2,B=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 ...
- URAL 1057. Amount of Degrees(数位DP)
题目链接 我看错题了...都是泪啊,不存在3*4^2这种情况...系数必须为1... #include <cstdio> #include <cstring> #include ...
- [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 ...
- ural 1057Amount of Degrees ——数位DP
link:http://acm.timus.ru/problem.aspx?space=1&num=1057 论文: 浅谈数位类统计问题 刘聪 #include <iostream&g ...
- 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 ...
- Ural1057. Amount of Degrees 题解 数位DP
题目链接: (请自行百度进Ural然后查看题号为1057的那道题目囧~) 题目大意: Create a code to determine the amount of integers, lying ...
- 2018.09.07 Amount of degrees(数位dp)
描述 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和. 例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 17 = 24+20, ...
- Ural Amount of Degrees(数位dp)
传送门 Amount of Degrees Time limit: 1.0 secondMemory limit: 64 MB Description Create a code to determi ...
- 【数位DP】[LOJ10163]Amount of Degrees
发现自己以前对数位DP其实一窍不通... 这题可以做一个很简单的转换:一个数如果在$b$进制下是一个01串,且1的个数恰好有k个,那么这个数就是合法的(刚开始没判断必定是01串,只判断了1的个数竟然有 ...
随机推荐
- jakson
Java下常见的Json类库有Gson.JSON-lib和Jackson等,Jackson相对来说比较高效,在项目中主要使用Jackson进行JSON和Java对象转换,下面给出一些Jackson的J ...
- 22 【python】入门指南:函数
#!/bin/python def test_func(): return "test_func" a = test_func() print(a) 输出结果: test_func ...
- jsp页面\n换行替换
1,js替换 var text= $(this).text(); var reg = new RegExp("\\n","g");//g,表示全部替换. tex ...
- mysql 5.6 datetime 保存精确到秒
mysql中的CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 设置默认值 now(3) datetime 长度 3 保存精确到秒
- Redis 与Spring-data-redis 整合后封装的工具类
此工具类方法是使用了redis 与spring 整合后,才可以使用的工具类,将 spring-data-redis 一些我们开发中常用的方法进行了封装,方便我们日常开发中进行调用: package c ...
- java 知识汇总
一.springboot cloud 1.maven 配置 parent:org.springframework.boot:sping-boot-starter-parent dependencies ...
- Linux驱动之建立一个hello模块
目标:在开发板上执行insmod hello.ko能在控制台打印出hello init:接着执行rmmod会在控制台打印出hello exit 建立一个hello模块的步骤如下: 1.建立一个hell ...
- Java并发-ConcurrentModificationException原因源码分析与解决办法
一.异常原因与异常源码分析 对集合(List.Set.Map)迭代时对其进行修改就会出现java.util.ConcurrentModificationException异常.这里以ArrayList ...
- Spring Boot 2.0(一):Spring Boot 2.0尝鲜-动态 Banner
Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜 Spring Boot 更换 Banner 我们先来回顾一下在 Spring Bo ...
- C&Cpp.CallGraph
1. CodeViz http://www.skynet.ie/~mel/projects/codeviz/ 2. http://my.oschina.net/zmlblog/blog/186308