Codeforces Beta Round #91 (Div. 2 Only)深度优先
2016-01-14 14:48
218 查看
C. Lucky Sum
对这道题我想了很多方法,最直观的方法是打表法,把要用的数直接打表存起来,然后直接查找就好。算法思路是对的,但是提交的时候一直提示不是时间超时就是占用内存太大,最后看到了别人的代码竟然采用了深度优先搜索的办法给解决了。我对深度优先里的递归一直理解的都不怎么好,先整理下来有时间再看吧。代码如下:
对这道题我想了很多方法,最直观的方法是打表法,把要用的数直接打表存起来,然后直接查找就好。算法思路是对的,但是提交的时候一直提示不是时间超时就是占用内存太大,最后看到了别人的代码竟然采用了深度优先搜索的办法给解决了。我对深度优先里的递归一直理解的都不怎么好,先整理下来有时间再看吧。代码如下:
//the code is not written by myself #include <iostream> #include<algorithm> #define LL long long using namespace std; const int maxn = 20000; LL d[maxn]; int tot = 1; void dfs(LL num, int cur) { d[tot++] = num; if (cur > 11) return; dfs(num * 10 + 4, cur + 1); dfs(num * 10 + 7, cur + 1); } LL sum(LL x) { if (x == 0) return 0; LL temp = 0; for (int i = 1; i < tot; i++) { if (x >= d[i]) temp += d[i] * (d[i] - d[i - 1]); else { temp += d[i] * (x - d[i - 1]); break; } } return temp; } int main() { dfs(4, 0); dfs(7, 0); sort(d + 1, d + tot); LL lt, rt; cin >> lt >> rt; cout << sum(rt) - sum(lt - 1) << endl; return 0; }
相关文章推荐
- 【LeetCode】Implement strStr()
- visual studio svn插件安装
- C语言中的函数指针
- 设计模式(4)-对象创建型模式-Prototype模式
- bash登录式shell(完全切换)与非登陆式shell(不完全切换)区别
- opencv MAT数据操作
- spice
- Scala编程之分数的四则运算
- caffe框架翻译-理解(转载)
- 线程同步问题,volatile关键字和synchronized关键字
- Contacts(CNGroup)
- Android:Field can be converted to a local varible.
- Android字符串资源及其格式化
- 【总结】RPC框架Dubbo深入分析
- 禁止屏幕旋转
- spring aop 3 Spring AOP中JoinPoint的表达式定义描述
- Git am 合并 patch 时的冲突处理
- 在启动hadoop的时候,总是有警告Warning: $HADOOP_HOME is deprecated
- 如何导入Eclipse自定义的快捷键设置
- Android:Field can be converted to a local varible.