数位dp题,关键是用树的思维去考虑。

对于一个数字X,要是能表示成K个B的不同次幂,等价于X在B进制下有且只有K个位上面的数字为一,其他位上的数字都为0。

具体读者可以去参考,国家集训队李聪的论文,里面的介绍都很详细。

#include<stdio.h>
#include<string.h>
#define LL long long
LL c[60][60];
int K,b,a[40];
void init()//预处理组合数
{
int i,j;
for(i=0;i<40;i++)c[i][0]=c[i][i]=1;
for(i=1;i<40;i++)
for(j=1;j<i;j++)
c[i][j]=c[i-1][j]+c[i-1][j-1];
}
int change(int s)
{
int i,j,len;
memset(a,0,sizeof(a));
for(i=0;s;i++){
a[i]=s%b;
s/=b;
}
len=i;
for(i=len;i>=0;i--){//对于非二进制的处理
if(a[i]>1){
for(j=i;j>=0;j--)a[j]=1;
break;
}
}
return len;
}
int solve(int s)
{
int ans=0;
int i,j;
int len=change(s);
int tot=0;
for(j=len+1;j>0;j--)
{
if(a[j]){
tot++;
if(tot>K)break;
}
if(a[j-1])ans+=c[j-1][K-tot];
}
if(a[0]+tot==K)ans++;//考虑本身
return ans;
}
int main()
{
int i,j,x,y;
init();
while(scanf("%d%d",&x,&y)!=-1)
{
scanf("%d%d",&K,&b);
printf("%d\n",solve(y)-solve(x-1));
}
return 0;
}

ural 1057(数位dp)的更多相关文章

  1. URAL 1057 数位dp

    题目传送门http://acm.timus.ru/problem.aspx?space=1&num=1057 最近在学习数位dp,具体姿势可以参照这篇论文:http://wenku.baidu ...

  2. [转]数位dp小记

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

  3. 数位DP 计划

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

  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. struts2 + jquery 开发环境下的ajax构建方法(action写法 + struts.xml配置 + js调用代码)

    1.action写法 public class RegisterAction extends ActionSupport { private InputStream inputStream; /** ...

  2. struts2中#,$,%的用法以及el,ognl表达式的用法

    OGNL, JSTL, STRUTS2标签中符号#,$,%的用法示例 取Session中的值 <c:out value="${sessionScope.user.userId}&quo ...

  3. Tex介绍

    TeX, LaTeX, XeTeX, PdfTeX等等 TeX 是一种宏语言.就像别的语言有库一样,TeX 语言有宏的集合.这些宏,就是用这个语言写出来的东西,供大家共用的.TeX 有个最基本的宏集合 ...

  4. PADS无模命令总结

    1.PADS2007无模命令与快捷键 <x.y>表示坐标.<s>表示文体.<n>表示数字. 1.[C]显示平面的焊盘和热焊盘(Thermal). 2.[D]显示当前 ...

  5. silverlight datagrid绑定匿名类

    原文 http://www.cnblogs.com/luweis/archive/2011/10/21/2220587.html 刚开始遇到的一个问题是这样的,我有一个datagrid,根据不同的条件 ...

  6. Make Hadoop 1.2.1 run, my first try

    经历两天努力,8月25日下午2点40分,终于让hadoop1.2.1跑起来. 用的是<Hadoop实战第2版>(陆嘉恒)里面的WordCount例子,虽然书是2013年出的,但用的例子还是 ...

  7. javascript抽象工厂模式

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  8. kafka学习(一)-背景及架构设计

    概念和术语 消息,全称为Message,是指在生产者.服务端和消费者之间传输数据. 消息代理:全称为Message Broker,通俗来讲就是指该MQ的服务端或者说服务器. 消息生产者:全称为Mess ...

  9. Android之单击返回键两次退出

    private static Boolean isExit = false;        private static Boolean hasTask = false;        Timer t ...

  10. 初识Maven

    今天开始学习怎样使用maven,听起来挺神奇的东西,我们来一步一步的加以剖析. Maven的一些具体的论文的东西,网上很多博客介绍,这里我就不逐一介绍,下面我们从安装maven开始讲解: (1)Mav ...