24-Fibonacci(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=5167
Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3388 Accepted Submission(s): 886
Now we need to check whether a number can be expressed as the product of numbers in the Fibonacci sequence.
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
4
17
233
No
Yes
#include <iostream>
using namespace std;
int a[100];
int ct, flag; void init(){
a[0] = 0;
a[1] = 1;
ct += 2;
for(int i = 2; a[i - 1] <= 1000000000; i++){ //条件写为a[i]<=100000000000就崩了,注意是先i++,再判断条件的,所以会死循环
a[i] = a[i - 1] + a[i - 2];
ct++;
}
}
void dfs(int n, int k){
if(n == 1){
flag = 1;
return ;
}
for(int i = k; i >= 3; i--){
if(a[i] > n){
continue;
}
else if(n % a[i] == 0){
dfs(n / a[i], i); //注意:不是k-1而是i
}
if(flag) //剪纸
return ;
}
}
int main(){
std::ios::sync_with_stdio(false);
int t, n;
init();
cin >> t;
while(t--){
cin >> n;
if(n == 0 || n == 1){ //要特判
cout << "Yes" << endl;
}
else{
flag = 0;
dfs(n, ct - 1);
if(flag){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
}
}
return 0;
}
24-Fibonacci(dfs+剪枝)的更多相关文章
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
随机推荐
- LeetCode OJ:Count and Say(数数)
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- BEC translation exercise 1
U.S. oil drillers have made major efficiency improvements with a speed that has repeatedly surprised ...
- BEC listen and translation exercise 13
The old lady sits on a mobile chair every morning. He got a large fortune when his father died, but ...
- c++引用和指针的实现
引用和指针有什么区别?引用在进程中是否会分配内存? C++ primer中说: 引用就是对象的另一个名字. C++ primer中说: 指针用于指向对象,保存的是另一个对象的地址. 从字面意义上,感觉 ...
- CodeForces - 827A:String Reconstruction (基础并查集)
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- [转]Angular移除不必要的$watch之性能优化
双向绑定是Angular的核心概念之一,它给我们带来了思维方式的转变:不再是DOM驱动,而是以Model为核心,在View中写上声明式标签.然后,Angular就会在后台默默的同步View的变化到Mo ...
- unix下网络编程之I/O复用(二)
select函数 该函数允许进程指示内核等待多个事件中的任何一个发生,并仅在有一个或是多个事件发生或经历一段指定的时间后才唤醒它.我们调用select告知内核对哪些描述字(就读.写或异常条件)感兴趣以 ...
- java bean Format注解用法
@NumberFormat(style=Style.NUMBER) private int number; @DateTimeFormat(pattern="yyyy-MM-dd&qu ...
- MSDN 学习网站
-MSDN中文网站http://msdn.microsoft.com/zh-cn/-MSDN Webcast 中文网络广播http://msdn.microsoft.com/zh-cn/dd79616 ...
- 什么是VBA,他有什么作用
目录 什么是VBA,它有什么作用 VBA在哪里存放的?怎么运行的 什么是宏?宏和VBA有什么关系? 录制一个宏 编写第一个宏 一.什么是VBA,它有什么作用 VBA是一种编程语言,它依托于Office ...