题目传送门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的更多相关文章

  1. [转]数位dp小记

    转载自:http://blog.csdn.net/guognib/article/details/25472879 参考: http://www.cnblogs.com/jffifa/archive/ ...

  2. 数位DP 计划

    通常的数位dp可以写成如下形式: [cpp] view plain copy int dfs(int i, int s, bool e) { if (i==-1) return s==target_s ...

  3. ural 1057(数位dp)

    数位dp题,关键是用树的思维去考虑. 对于一个数字X,要是能表示成K个B的不同次幂,等价于X在B进制下有且只有K个位上面的数字为一,其他位上的数字都为0. 具体读者可以去参考,国家集训队李聪的论文,里 ...

  4. 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 ...

  5. [DP]数位DP总结

     数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step   http://blog.csdn.net/dslovemz/article/details/ ...

  6. 数位DP问题整理(一)

    第一题:Amount of degrees (ural 1057) 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1057 题意:[x,y ...

  7. 【学时总结】 ◆学时·IV◆ 数位DP

    [学时·IV] 数位DP ■基本策略■ 说白了就是超时和不超时的区别 :) 有一些特别的题与数位有关,但是用一般的枚举算法会超时.这时候就有人提出了--我们可以用动态规划!通过数字前一位和后一位之间的 ...

  8. 以前刷过的数位dp

    TOJ1688: Round Numbers Description The cows, as you know, have no fingers or thumbs and thus are una ...

  9. Ural1057. Amount of Degrees 题解 数位DP

    题目链接: (请自行百度进Ural然后查看题号为1057的那道题目囧~) 题目大意: Create a code to determine the amount of integers, lying ...

随机推荐

  1. Tomcat:Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

    可能原因一: 在本地tomcat启动正常并且访问正常的项目放在服务器上tomcat报以上错误. 本地tomcat为7.0.68,服务器上为7.0.86 错误原因:服务器tomcat版本过高. 解决办法 ...

  2. IO之DataStream数据流举例

    import java.io.*; public class TestDataStream { public static void main(String[] args) { ByteArrayOu ...

  3. jmeter 接口测试

    web接口测试工具: 手工测试的话可以用postman ,自动化测试多是用到 Jmeter(开源).soupUI(开源&商业版). 下面将对前一篇Postman做接口测试中的接口用Jmeter ...

  4. codeforces365A

    #include<stdio.h> #include<string.h>//刚做codeforces上的比赛题我都没看懂啊啊啊啊啊啊 int main() { int n,m, ...

  5. 刺激(codevs 1958)

    题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺激(exitement,由于刺激过度导致拼写都缺了个字母),喜欢忽高忽低的感觉.现在S拿到了一张地图,试图制定一个最长路径 ...

  6. iOS 数据库操作崩溃提示“ int rc = sqlite3_step([_statement statement]);”或者提示“ rc = sqlite3_step(pStmt);”

    数据库崩溃崩溃提示“ int rc = sqlite3_step([_statement statement]);”或者提示“ rc      = sqlite3_step(pStmt);”的时候,可 ...

  7. HTML5学习之语义化标签

    一.为什么HTML5要引入新语义标签 在HTML5出现之前,我们一般采用DIV+CSS布局我们的页面.但是这样的布局方式不仅使我们的文档结构不够清晰,而且不利于搜索引擎爬虫对我们页面的爬取.为了解决上 ...

  8. [bzoj1874][BeiJing2009 WinterCamp]取石子游戏_博弈论

    取石子游戏 bzoj-1874 BeiJing2009 WinterCamp 题目大意:题目链接. 注释:略. 想法: 我们通过$SG$函数的定义来更新$SG$的转移. 如果是寻求第一步的话我们只需要 ...

  9. how to read openstack code : wsgi

    要读懂本篇,你至少得写过一个python的web程序,并且把它部署到web服务器上过. 什么是wsgi 假设你写了一个python的web程序,并部署到了nginx上,那么一个http request ...

  10. 【nginx】【转】正向代理与反向代理的区别[

    转自: http://blog.csdn.net/m13666368773/article/details/8060481 正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单 ...