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的个数竟然有 ...
随机推荐
- Codeforces Beta Round #25 (Div. 2 Only)
Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...
- jquery 事件委托(利用冒泡)
将事件绑定在父元素上,格式$(父元素).on("事件名称","子元素选择器",function(方法体){}) <!DOCTYPE html> &l ...
- f5售后查询
登录: https://secure.f5.com/validate/validate.jsp http://boochem.blog.51cto.com/628505/633907
- 如何从应用直接跳转AppStore 电话 短信 邮件
//如何从应用直接跳转AppStore [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"应用程序的下载链接& ...
- java_11接口
1接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成.这样将功能的定义 ...
- 两个App之间的跳转 并传值
两个App之间的传值最主要的是方法是 Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.a ...
- SSH异常“Failed to start OpenSSH Server daemon”
[root@bogon yum]# systemctl status sshd.service● sshd.service - OpenSSH server daemon Loaded: load ...
- android studio 3.0 集成ijkplayer
一.ijkplayer编译过程略,有兴趣的朋友可以再研究,以下以编译好的版本讲解. 将ijkplayer相关的so及aar文件复制到app下的libs目录,为支持多版本的手机使用,将所有的so文件都复 ...
- Oracle_PL/SQL(10) 定时器job
定时器job1.定义 定时器指在特定的时间执行特定的操作. 可以多次执行.说明:特定的操作:指一个完成特定功能的存储过程.多次执行:指可以每分钟.每小时.每天.每周.每月.每季度.每年等周期性的运行. ...
- [中英对照]Introduction to DPDK: Architecture and Principles | DPDK概论: 体系结构与实现原理
[中英对照]Introduction to DPDK: Architecture and Principles | DPDK概论: 体系结构与实现原理 Introduction to DPDK: ...