hdu 5167(dfs)
2016-07-18 16:39
274 查看
Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2400 Accepted Submission(s): 610
[align=left]Problem Description[/align]
Following is the recursive definition of Fibonacci sequence:
Fi=⎧⎩⎨01Fi−1+Fi−2i = 0i = 1i > 1
Now we need to check whether a number can be expressed as the product of numbers in the Fibonacci sequence.
[align=left]Input[/align]
There is a number T shows there are T test cases below. (T≤100,000)
For each test case , the first line contains a integers n , which means the number need to be checked.
0≤n≤1,000,000,000
[align=left]Output[/align]
For each case output "Yes" or "No".
[align=left]Sample Input[/align]
3
4
17
233
[align=left]Sample Output[/align]
Yes
No
Yes
[align=left]Source[/align]
BestCoder Round #28
题意:给出一个数n, n<=10^9,问是否存在一系列斐波拉契数列中的数字使得这一系列斐波拉契数之积等于 n
题解:暴力搜索,但是要加剪枝,不然会超时,我们先从最大的斐波拉契数开始,如果能够除尽,那么下一个斐波拉契数必定不会大于当前这个数,所以可以在这里剪个枝,还是跑了700ms+
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; typedef long long LL; LL f[45]; bool flag; void init(){ f[0] = 0; f[1] = 1; for(int i=2;i<=45;i++){ f[i] = f[i-1]+f[i-2]; } } void dfs(LL ans,int step){ if(ans==1){ flag = true; return; } for(int i=3;i<=step;i++){ if(ans<f[i]) break; if(ans%f[i]==0){ if(flag) return; dfs(ans/f[i],i); } } return; } int main() { init(); int tcase; scanf("%d",&tcase); while(tcase--) { LL n; scanf("%lld",&n); if(n==0){ printf("Yes\n"); continue; } flag = false; dfs(n,45); if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }
-------------------------------------------------------------------------------------------------------------------------------------------------------
然后我把循环顺序改了,171msAC...
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; typedef long long LL; LL f[45]; bool flag; void init(){ f[0] = 0; f[1] = 1; for(int i=2;i<=45;i++){ f[i] = f[i-1]+f[i-2]; } } void dfs(LL ans,int step){ if(ans==1){ flag = true; return; } for(int i=step;i>=3;i--){ if(ans%f[i]==0){ if(flag) return; dfs(ans/f[i],i); } } return; } int main() { init(); int tcase; scanf("%d",&tcase); while(tcase--) { LL n; scanf("%lld",&n); if(n==0){ printf("Yes\n"); continue; } flag = false; dfs(n,45); if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- 浅谈MySQL中优化sql语句查询常用的30种方法
- js对字符串进行编码方法总结
- 文件名搜索分词设计
- 用flume的intercepter作数据格式转换和清洗
- 2016年上半年系统集成项目管理工程师培训感想
- [从头读历史] 第308节 星球战争 BC2599 至 BC2500(公元前26世纪)
- pb_ds(Policy-Based Data Structures)
- SSH整合之org.springframework.web.context.ContextLoaderListener not found问题
- LaTeX技巧168:如何让行间公式左对齐
- L2-011. 玩转二叉树
- ORACLE AWR报告生成过程出现多个实例记录分析
- Codeforces Round #353 (Div. 2) A - Infinite Sequence
- 学习笔记-斯坦福iOS7-第十四课:UIApplication和MapKit
- 预编译头文件之pch文件的创建及使用
- git pull
- JsonCpp使用细谈(Windows平台)
- Android 7(N)网络安全配置
- Executor相关的一些类
- MySQL两种存储引擎MyISAM于InnoDB对比
- STM32三种启动模式