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 ...
随机推荐
- 算法竞赛入门经典5.2 STL初步
1. 排序和检索,学会使用sort排序,以及low_bound函数 Raju and Meena love to play with Marbles. They have got a lot of m ...
- 我能考虑到的数组(老)方法就这些了(es5)
代码注释都写的很清楚了 关键字:斐波那契数组.二维数组.多维数组(矩阵)... <!DOCTYPE html> <html lang="en"> <h ...
- assert.deepEqual()
assert.deepEqual(actual, expected[, message]) 深度比较 actual 和 expected 参数,使用比较运算符(==)比较原始值. 只考虑可枚举的&qu ...
- UVa 548 树(已知其中两种遍历, 还原树)
题意: 给出后序遍历和先序遍历, 还原一棵树, 然后求出从根节点到叶子的最小路劲和. 分析: 已知后序遍历, 那么后序的最后一个节点就是根节点, 然后在中序中找到这个节点, 它的左边就是左子树, 它的 ...
- 【03】emmet系列之CSS语法
[01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 单位: 有几个常用值别 ...
- node.js 写流 createWriteStream----由浅入深
createWriteStream 写流 基于fs模块 let fs=require("fs"); createWriteStream创建一个写流 我们先创建一个2.txt要写入的 ...
- 在workbench中导入.sql文件!(导入数据库文件)
第一步,登陆mysql workbench 第二步,打开自己的数据 ,此处默认(root) 打开数据库后页面 : 第三步,新建一个schema ,随便给个名字,这里起名为test : 可以看到test ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- codeforces 691E(矩阵乘法)
E. Xor-sequences time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- codeforces 762E(cdq分治)
题意: n个电台,每个电台有三个属性xi, ri, fi.分别代表电台的坐标,电台的播报范围,以及播报的频率. 对于一对电台i, j,若min(ri, rj) >= |xi - xj|,那么他们 ...