Ural1057 - Amount of Degrees(数位DP)
题目大意
求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整
数次幂之和。例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意:
输入:第一行包含两个整数X和Y。接下来两行包含整数K和B。
输出:只包含一个整数,表示满足条件的数的个数。
数据规模:1 ≤ X ≤ Y ≤ 2^31
−1,1 ≤ K ≤ 20, 2 ≤ B ≤ 10。
题解
《浅谈数位类统计问题》的论问题~~~~~人生第一道数位DP,哈哈~~~O(∩_∩)O~~代码纯属抄袭~~~~
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
#define MAXN 35
int f[MAXN][MAXN];
void init()
{
f[0][0]=1;
for(int i=1;i<32;i++)
{
f[i][0]=f[i-1][0];
for(int j=1;j<=i;j++)
f[i][j]=f[i-1][j]+f[i-1][j-1];
}
}
int change(int x,int b)
{
int p[MAXN];
int ans=0;
while(x)
{
p[ans++]=x%b;
x/=b;
}
reverse(p,p+ans);
for(int i=0;i<ans;i++)
if(p[i]>1)
{
for(int j=i;j<ans;j++)
p[j]=1;
break;
}
for(int i=0;i<ans;i++)
x=x|(p[ans-i-1]<<i);
return x;
}
int cal(int x,int k)
{
int tot=0,ans=0;
for(int i=31;i>0;i--)
{
if(x&(1<<i))
{
tot++;
if(tot>k) break;
x=x^(1<<i);
}
if((1<<(i-1))<=x)
ans+=f[i-1][k-tot];
}
if(tot+x==k) ans++;
return ans;
}
int main()
{
init();
int x,y,k,b;
while(cin>>x>>y>>k>>b)
{
int X=change(x,b);
int Y=change(y,b);
cout<<cal(Y,k)-cal(X-1,k)<<endl;
}
return 0;
}
Ural1057 - Amount of Degrees(数位DP)的更多相关文章
- [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 ...
- Ural1057. Amount of Degrees 题解 数位DP
题目链接: (请自行百度进Ural然后查看题号为1057的那道题目囧~) 题目大意: Create a code to determine the amount of integers, lying ...
- URAL1057. Amount of Degrees(DP)
1057 简单的数位DP 刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&g ...
- [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 ...
- ural1057 Amount of Degrees
链接 这题有一点小坑点 就是AX^B A只能为0或者1 ,剩下的就比较好做的了. #include <iostream> #include<cstdio> #include ...
- ural 1057Amount of Degrees ——数位DP
link:http://acm.timus.ru/problem.aspx?space=1&num=1057 论文: 浅谈数位类统计问题 刘聪 #include <iostream&g ...
- ural1057 Amount of degrees 位数统计
#include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...
- 数位dp总结
由简单到稍微难点. 从网上搜了10到数位dp的题目,有几道还是很难想到的,前几道基本都是模板题,供入门用. 点开即可看题解. hdu3555 Bomb hdu3652 B-number hdu2089 ...
随机推荐
- 【原创】一起学C++ 之 字符串 ---------C++ primer plus(第6版)
C++ Primer Plus 第6版 字符串:是存储在内存的连续字节中的一系列字符. C++处理字符串的方式有2种: 一.来自C语言.常被称为C-风格字符串(C-Style-string) 1)从字 ...
- .net entity framework 泛型 更新与增加记录
static public bool SaveOrUpdate<T>(T entity) where T: class { bool result = false; using (wech ...
- C# - 参数数组Params
参数数组可以使用个数不定的参数调用函数,此时用params关键字定义它们. 限制:必须是函数定义中的最后一个参数:必须是同一类型的. 优点:不用在调用代码中传入数组,指定参数个数不受限制,可以不指定该 ...
- leetcode-110:判断平衡二叉树 Java
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [JavaScript] 用html5 js实现浏览器全屏
项目中需要将后台浏览器的窗口全屏,也就是我们点击一个按钮要实现按F11全屏的 效果. 在HTML5中,W3C制定了关于全屏的API,就可以实现全屏幕的效果,也可以 让页面中的图片,视频等全屏目前只有g ...
- Hadoop集群(第5期)_Hadoop安装配置
1.1 Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS,Hadoop Distributed Filesystem)和Map ...
- BZOJ 3971 Матрёшка 解题报告
很自然想到区间 DP. 设 $Dp[i][j]$ 表示把区间 $[i, j]$ 内的套娃合并成一个所需要的代价,那么有: $Dp[i][i] = 0$ $Dp[i][j] = min\{Dp[i][k ...
- Welcome
唔.你好! 这里是 Evensgn 的笔记本. 我是 SD 省的一名高中 OIer,从初中就接触了 OI ,然而水平一直是弱弱哒. Evensgn 是我常用的 ID. 不忘初心,方能始终. E-mai ...
- http://blog.csdn.net/zhang_xinxiu/article/details/38655311
一.Activiti下载及简介 1.1.Activiti下载 官网下载地址:http://activiti.org/download.html Note:下载时不一定要使用最新版本的,最 ...
- Android EditText的常用技巧
1. 设定 EditText 的滚动条.对齐方式.行数.和提示 (hint) 及其颜色 在布局文件,比如 main.xml 中,增加 < EditText android:id = ...