URAL 1057 数位dp
题目传送门http://acm.timus.ru/problem.aspx?space=1&num=1057
最近在学习数位dp,具体姿势可以参照这篇论文:http://wenku.baidu.com/view/d2414ffe04a1b0717fd5dda8.html?re=view
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 40;
int f[maxn][maxn];
int X,Y,K,B;
void init() {
memset(f,0,sizeof(f));
f[0][0] = 1;
for(int i = 1;i <= 31;++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 calc(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 to_binary(int n,int b) {
int ret = 0,d[40],i,j,m=0;
while(n>0) {
d[m++] = n % b;
n /= b;
}
for(i = m-1;i >= 0;--i) {
if(d[i] > 1) {
for(j = i;j >= 0;--j)
ret |= (1<<j);
break;
}
else {
ret |= (d[i]<<i);
}
}
return ret;
}
int main()
{
init();
while(~scanf("%d%d%d%d",&X,&Y,&K,&B)) {
printf("%d\n",calc(to_binary(Y,B),K) - calc(to_binary(X-1,B),K));
}
return 0;
}
URAL 1057 数位dp的更多相关文章
- [转]数位dp小记
转载自:http://blog.csdn.net/guognib/article/details/25472879 参考: http://www.cnblogs.com/jffifa/archive/ ...
- 数位DP 计划
通常的数位dp可以写成如下形式: [cpp] view plain copy int dfs(int i, int s, bool e) { if (i==-1) return s==target_s ...
- ural 1057(数位dp)
数位dp题,关键是用树的思维去考虑. 对于一个数字X,要是能表示成K个B的不同次幂,等价于X在B进制下有且只有K个位上面的数字为一,其他位上的数字都为0. 具体读者可以去参考,国家集训队李聪的论文,里 ...
- 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 ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- 数位DP问题整理(一)
第一题:Amount of degrees (ural 1057) 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1057 题意:[x,y ...
- 【学时总结】 ◆学时·IV◆ 数位DP
[学时·IV] 数位DP ■基本策略■ 说白了就是超时和不超时的区别 :) 有一些特别的题与数位有关,但是用一般的枚举算法会超时.这时候就有人提出了--我们可以用动态规划!通过数字前一位和后一位之间的 ...
- 以前刷过的数位dp
TOJ1688: Round Numbers Description The cows, as you know, have no fingers or thumbs and thus are una ...
- Ural1057. Amount of Degrees 题解 数位DP
题目链接: (请自行百度进Ural然后查看题号为1057的那道题目囧~) 题目大意: Create a code to determine the amount of integers, lying ...
随机推荐
- python3.x Day5 面向对象
类:类是指:对具有相同属性的事物的抽象.蓝图.原型.在类中定义了这些事物都具备的属性和共同的方法. 对象:一个对象就是一个类实例化以后的实例,一个类必须经过实例化后才能在程序中被使用,一个类可以实例化 ...
- Python:os模块 time模块
1.os os.popen('cmd').read() 执行某个系统命令,:输出命令结果 os.getcwd() 获取当前操作目录 os.makedirs(r"C:\a\b\c" ...
- add list of symbols in latex
* add list of symbols -- latex 1. createa new tex file named "toc.tex' with the following conte ...
- Jquery validate自定义验证
http://www.runoob.com/jquery/jquery-plugin-validate.html addMethod(name,method,message)方法 参数 name 是添 ...
- c:foreach 标签 varStatus的使用
<c:forEach items="${MedicalDoctoList }" var="medicalDoctor" varStatus="s ...
- 洛谷 4933 洛谷10月月赛II T2 大师
[题解] f[i][j]表示最后一个数为h[i],公差为j的等差数列的个数.n方枚举最后一个数和倒数第二个数转移即可.注意公差可能为负数,需要移动为正数再作为下标. #include<cstdi ...
- 九度oj 题目1202:排序
题目1202:排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:28802 解决:9408 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n ...
- [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)
传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...
- Spring Cloud(7):Zuul自定义过滤器和接口限流
上文讲到了Zuul的基本使用: https://www.cnblogs.com/xuyiqing/p/10884860.html 自定义Zuul过滤器: package org.dreamtech.a ...
- java.lang.NoClassDefFoundError: Could not initialize class异常处理
借鉴:http://blog.csdn.net/sleepdancer/article/details/9207425 static { InputStream in = XXX.class.getR ...