HDU 5167 Fibonacci (DFS + Fib数列)
2015-02-01 21:47
295 查看
Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 780 Accepted Submission(s): 209
Problem DescriptionFollowing 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.
Input
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
Output
For each case output "Yes" or "No".
Sample Input
3 4 17 233
Sample Output
Yes No Yes
Source
BestCoder Round #28
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5167
题目大意:给个数字判断它能不能由1000000000内的Fibonacci数列的一些项相乘得到
题目分析:打表到范围内的Fib数列为第46项,然后找n的因子,DFS搜一下就可以了,特判一下0
#include <cstdio> int fib[50], fac[50], cnt; bool flag; void cal() //获取Fib数列 { fib[0] = 0; fib[1] = 1; for(int i = 2; i < 47; i++) fib[i] = fib[i - 1] + fib[i - 2]; } bool DFS(int n, int now) { if(n == 1 || flag) //最后商为1则说明可以由某些项的乘积得到 flag = true; for(int i = now; i < cnt; i++) if(n % fac[i] == 0 && DFS(n / fac[i], i)) return true; return false; } int main() { cal(); int T, n; scanf("%d", &T); while(T--) { flag = false; cnt = 0; scanf("%d", &n); if(n == 0) { printf("Yes\n"); continue; } for(int i = 3; i < 47; i++) if(n % fib[i] == 0) fac[cnt++] = fib[i]; //找n的fib因子 DFS(n, 0); if(flag) printf("Yes\n"); else printf("No\n"); } }
相关文章推荐
- 【DFS】hdu 5167 Fibonacci
- hdu 5167 Fibonacci(DFS,剪枝,斐波那契)
- hdu 5167 Fibonacci(dfs)
- hdu 5167 Fibonacci(DFS)
- HDU 5167 Fibonacci(DFS暴力搜索)
- hdu 5167 Fibonacci(dfs)
- 【Fibonacci 数列前四位&后四位】hdu 3117
- 多校第十场 HDU 3936 FIB Query(fibonacci 数列的性质 ,及Ologn 矩阵加速乘算法)
- hdu 5167(dfs)
- 数论(fabonacci数列) hdu-1568-Fibonacci
- hdu 4099 Revenge of Fibonacci 字典树+斐波拉契数列数列
- 数论(fabonacci数列) hdu-1568-Fibonacci
- HDU - 5166 - Missing number && 5167 - Fibonacci
- HDU_5167_Fibonacci
- HDU - 5167 - Fibonacci
- 【预处理】 HDU 5167 Fibonacci
- hdu 5167 Fibonacci(预处理)
- HDU 5167 Fibonacci
- hdu 5167-Fibonacci
- hdu 5167 Fibonacci 打表