Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2400    Accepted Submission(s): 610

Problem Description
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.

 
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
 
题意:给出一个数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[];
bool flag;
void init(){
f[] = ;
f[] = ;
for(int i=;i<=;i++){
f[i] = f[i-]+f[i-];
} }
void dfs(LL ans,int step){
if(ans==){
flag = true;
return;
}
for(int i=;i<=step;i++){
if(ans<f[i]) break;
if(ans%f[i]==){
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==){
printf("Yes\n");
continue;
}
flag = false;
dfs(n,);
if(flag) printf("Yes\n");
else printf("No\n");
} return ;
}

-------------------------------------------------------------------------------------------------------------------------------------------------------

然后我把循环顺序改了,171msAC...

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
LL f[];
bool flag;
void init(){
f[] = ;
f[] = ;
for(int i=;i<=;i++){
f[i] = f[i-]+f[i-];
} }
void dfs(LL ans,int step){
if(ans==){
flag = true;
return;
}
for(int i=step;i>=;i--){
if(ans%f[i]==){
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==){
printf("Yes\n");
continue;
}
flag = false;
dfs(n,);
if(flag) printf("Yes\n");
else printf("No\n");
} return ;
}

hdu 5167(dfs)的更多相关文章

  1. HDU 5143 DFS

    分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...

  2. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  3. HDU 5167 Fibonacci 筛法+乱搞

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5167 题意: 给你一个x,判断x能不能由斐波那契数列中的数相乘得到(一个数可以重复使用) ...

  4. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  5. hdu 4751(dfs染色)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...

  6. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  7. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  8. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  9. hdu 1716(dfs)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716     排列2   Problem Description Ray又对数字的列产生了兴趣:现 ...

随机推荐

  1. Uuuuuunity

    foreach http://blog.csdn.net/byondocean/article/details/6871881 yield  http://www.cnblogs.com/CareyS ...

  2. java基础知识(一)- 数据类型

    Java有两大数据类型: 基本数据类型,基本数据类型都可以直接分配到栈中 引用数据类型,引用存放在栈中,对象本身存放在堆中 基本数据类型共有四类八种 第一类:字符型(char) 第二类:逻辑型(boo ...

  3. Java中动态代理实现原理深究

    一.前言 笔者平时开发使用“动态代理”不多,最近在看设计模式的时候,“动态代理”又在面前晃了几次,所以这次想从源码的角度去分析动态代理的实现原理,以窥探其精妙~ 二.正文 2.1 静态代理  本文源码 ...

  4. Intellij IDEA将工程打包成jar包并执行

    打开File -> Project Structure -> Artifacts 点击“+”,选择“Jar”,选择Empty或From modules with dependencies, ...

  5. LeetCode -- Best Time to Buy and Sell Stock系列

    Question: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri ...

  6. Codeforces Round #430 (Div. 2) Vitya and Strange Lesson

    D.Vitya and Strange Lesson(字典树) 题意: 给一个长度为\(n\)的非负整数序列,\(m\)次操作,每次先全局异或\(x\),再查询\(mex\) \(1<=n< ...

  7. Conjugate 解题报告

    Conjugate 问题描述 在不存在的 \(\text{noip day3}\) 中,小 \(\text{w}\) 见到了一堆堆的谜题. 比如这题为什么会叫共轭? 他并不知道答案. 有 \(n\) ...

  8. C&C++——库头文件及其作用

    1. 一些头文件的作用::ANSI C.提供断言,assert(表达式):GCC.GTK,GNOME的基础库,提供很多有用的函数,如有数据结构操作函数.使用glib只需要包含:GCC.文件夹操作函数. ...

  9. NEYC 2017 自动取款机 atm Day6 T1

                                                                                          自动取款机 [问题描述] 小 ...

  10. CentOS 7, Attempting to create directory /root/perl5

    By francis_hao    Apr 10,2017 在使用CentOS 7的时候,首次登陆会出现新建一个perl5文件夹的提示,删除该文件后,之后登陆还是会出现该提示并新建了perl5文件夹. ...