ural 1057(数位dp)
数位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)的更多相关文章
- URAL 1057 数位dp
题目传送门http://acm.timus.ru/problem.aspx?space=1&num=1057 最近在学习数位dp,具体姿势可以参照这篇论文:http://wenku.baidu ...
- [转]数位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 ...
- 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 ...
随机推荐
- javascript 动态创建表格
<html> <head> <script> function createTable(rows,lines){ this.rows=rows; this.line ...
- codeforces 552 E. Vanya and Brackets 表达式求值
题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...
- Python环境变量配置问题
安装Python2.7后,在环境变量中加入路径方法如下: 1,设置:右键单击计算机-->属性-->高级系统设置-->环境变量-->Path-->编辑Path-->在 ...
- [原创]抢先DriverStudio夺取机器控制权(上篇)
原文链接:抢先DriverStudio夺取机器控制权 废话不谈,言归正传!大家都知道,装了DriverStudio软件(我使用的是v3.2版)的系统在启动时会显示其配置画面,(如图0所示) 图 0 这 ...
- (C)学生成绩管理系统
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h&g ...
- Apache commons (Java常用工具包)简介
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表,详细信息访问http://jakarta.apache.org/commons/index.html Be ...
- ARM Cortex-M3内核的巨大优势
ARM Cortex-M3相比于ARM其他系列微控制器,具有以下优势或特点: 1. 三级流水线+分支预测 ARM Cortex-M3与ARM7内核一样,采用适合于微控制器应用的三级流水线,但增加了分支 ...
- Sprite Kit编程指南中文版下载
下载地址:http://download.csdn.net/detail/xin814/6032573 关于Sprite Kit 重要提示: 这是API或开发技术的一个初版文档.虽然本文档的技术准确 ...
- 理解session机制
理解session机制 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序需要为某个客户端的请求创建一个session的时候,服务器首 ...
- Codeforces Round #262 (Div. 2) B
题目: B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes inp ...