hdu4734(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734
思路:记忆化搜索。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int Pow[];
int dp[][];
int digit[];
int a,b; int Get_f(int n)
{
int pos=,res=;
while(n){
digit[pos++]=n%;
n/=;
}
for(int i=;i<pos;i++)res+=digit[i]*Pow[i];
// cout<<res<<"**"<<endl;
return res;
} int dfs(int pos,int res,int doing)
{
if(res<)return ;
if(pos==-)return ;
if(!doing&&dp[pos][res]!=-)return dp[pos][res];
int End=doing?digit[pos]:;
int ans=;
for(int i=;i<=End;i++){
ans+=dfs(pos-,res-i*Pow[pos],doing&&i==End);
}
if(!doing){
dp[pos][res]=ans;
}
return ans;
} int Solve(int res,int n)
{
int pos=;
while(n){
digit[pos++]=n%;
n/=;
}
return dfs(pos-,res,);
} int main()
{
memset(dp,-,sizeof(dp));
Pow[]=;
for(int i=;i<=;i++)Pow[i]=Pow[i-]<<;
int _case,t=;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&a,&b);
printf("Case #%d: ",t++);
printf("%d\n",Solve(Get_f(a),b));
}
return ;
}
hdu4734(记忆化搜索)的更多相关文章
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【BZOJ-3895】取石子 记忆化搜索 + 博弈
3895: 取石子 Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 263 Solved: 127[Submit][Status][Discuss] D ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- loj 1044(dp+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26764 思路:dp[pos]表示0-pos这段字符串最少分割的回文 ...
- DP(记忆化搜索) + AC自动机 LA 4126 Password Suspects
题目传送门 题意:训练指南P250 分析:DFS记忆化搜索,范围或者说是图是已知的字串构成的自动机图,那么用 | (1 << i)表示包含第i个字串,如果长度为len,且st == (1 ...
- HDU1978 记忆化搜索
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- bzoj4562: [Haoi2016]食物链--记忆化搜索
这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...
- 数位dp/记忆化搜索
一.引例 #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an ...
随机推荐
- docker sshd image problem, session required pam_loginuid.so, cann't login
在使用sshd docker 镜像时, 发现一个比较诡异的问题, 有些启动的容器可以连接, 有些不能. 例如 : 启动2个容器(这两个容器都有问题) : [root@localhost ~]# d ...
- spring.xml从外部文件引入数据库配置信息
<!-- 分散配置 --> <context:property-placeholder location="classpath:jdbc.properties" ...
- List、Set、Map常见集合遍历总结
Java中的集合有三大类,List.Set.Map,都处于java.util包中,List.Set和Map都是接口,不能被实例化,它们的各自的实现类可以被实例化.List的实现类主要有ArrayLis ...
- script的延迟执行
看这段代码: <body> <script src="deffer.js"></script> content </body> de ...
- SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关
SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using Syst ...
- javascript 相等运算符
相等运算符 JavaScript提供两个相等运算符:==和===. 简单说,它们的区别是相等运算符(==)比较两个值是否相等,严格相等运算符(===)比较它们是否为“同一个值”.如果两个值不是同一类型 ...
- [na]tcp的可靠性
- Windows 7 Path环境变量255限制的解决办法,SUBST
C:\Users\xxx>subst /? Associates a path with a drive letter. SUBST [drive1: [drive2:]path] SUBST ...
- Ubuntu环境下使用npm安装node模块时报错的处理方法
错误信息: npm ERR : node: not found : npm ERR! not ok code 0 解决方案: sudo apt-get install nodejs-legacy 也可 ...
- 新建一个用户,让他只能看到某一个视图(View),如何设置
新建一个用户,让他只能看到某一个视图(View),怎么设置? 新建一个用户,让他只能看到某一个视图(View),怎么设置? 如果做不到“只能看到指定视图”,最好能做到“对指定表或视图只有查询的权限”. ...