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 ...
随机推荐
- SAX解析XML-例子
1.要解析的xml <?xml version="1.0" encoding="UTF-8"?> <employees> <emp ...
- 常用的四种设计模式 PHP代码
// 工厂模式 interface Iuser { public function getUserName(); } class UserFactory { static public functio ...
- Uva 1103 古代象形符号(dfs求连通块, floodfill, 进制转换)
题意: 给定一个H行W列的字符矩阵(H<200, W < 50), 输入的是一个十六进制字符, 代表一行四个相邻的二进制, 1代表像素, 0代表没有像素. 然后要求判断输入的是以下哪些图形 ...
- 原生js获取复选框的值
obj = document.getElementsByName("dk_tj"); var longtxt = ""; for (k in obj) { ...
- STM32F407 正点原子 资料网址记录
网络资源 资源下载: http://www.openedv.com/thread-13912-1-1.html (注意下载资料的版本!非常推荐腾讯视频,因为可以在线免费倍速播放.课件ppt可以单独下载 ...
- 九度oj 题目1054:字符串内排序
题目1054:字符串内排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:10985 解决:5869 题目描述: 输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串 ...
- 自己打断点走的struts流程&拦截器工作原理
①. 请求发送给 StrutsPrepareAndExecuteFilter ②. StrutsPrepareAndExecuteFilter 判定该请求是否是一个 Struts2 请 求(Actio ...
- hosts.allow和hosts.deny文件
之前想通过外部主机访问自己主机上的VMWare虚拟机,使用了VMWare的NAT端口映射,经过一番尝试,算是成功了,总结一下. VMWare NAT端口映射就可以将虚拟机上的服务映射成自己主机上的端口 ...
- 不同app之间activity的跳转
关键在于指定activity的action属性 <activity android:name=".HardActivity"> <intent-filter> ...
- JSP服务器响应
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/server-response.html: 当一个Web服务器响应浏览器的HTTP请求时,响应通常包括一个 ...