题目传送门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. Kali Linux 下载、引导、安装

    下载卡莉 Linux 官方镜像: https://www.kali.org/downloads/ 官方虚拟机镜像: https://www.offensive-security.com/kali-li ...

  2. Python之购物车

    Python之购物车 msg_list = [ ['iphone',8888], ['coffe',38], ['book',90], ['Tesla',100000], ['RR',10000000 ...

  3. 集训第六周 数学概念与方法 概率 数论 最大公约数 G题

    Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...

  4. CodeForcesGym 100212E Long Dominoes

    Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. ...

  5. [luoguP1136] 迎接仪式(DP)

    传送门 每个字母只有两种选择,变成另一个或者不变. 所以f[i][j][k]表示前i个字母有j个j变成z,有k个z变成j 只需要比较j==k时的答案就行 #include <cstdio> ...

  6. 南邮CTF密码学,mixed_base64

    # -*- coding:utf-8 -*- from base64 import * flag = open("code.txt").readline() # 读取密文 for ...

  7. Linux rz的使用

    RZ是Linux提供的上传的命令,基于XMODEM/YMODEM/ZMODEM协议. 让我们来测试一下参数吧: 先准备一个文件,就叫test.txt吧,内容如下: one line rz -+  如果 ...

  8. JavaScript判断页面是否已经加载完毕

    做页面时经常会遇到当前页面加载完成后,执行某些初始化工作.这时候就要知道如何判断页面(包括IFRAME)已经加载完成,代码如下: < script language = "javasc ...

  9. BootStrap3栅格系统与布局

    栅格系统与布局 Use our powerful mobile-first flexbox grid to build layouts of all shapes and sizes thanks t ...

  10. 恢复表数据的办法(delete删除可恢复,truncate不可恢复)

    select * from table_name as of timestamp to_timestamp('2018-12-20 00:00:00', 'yyyy-mm-dd hh24:mi:ss' ...