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. 【iOS开发】UIView之userInteractionEnabled属性介绍

    http://my.oschina.net/hmj/blog/108002 属性作用 该属性值为布尔类型,如属性本身的名称所释,该属性决定UIView是否接受并响应用户的交互. 当值设置为NO后,UI ...

  2. Linux e1000e网卡驱动

    目录 识别网卡 命令行参数 附加配置 技术支持 一.识别网卡e1000e驱动支持Intel所有的GbE PCIe网卡,除了82575,82576,基于82580系列的网卡.提示:Intel(R) PR ...

  3. 修改CodeSmith中的SchemaExplorer.MySQLSchemaProvider

    修改C:\Program Files (x86)\CodeSmith\v6.5\Samples\Projects\CSharp\MySQLSchemaProvider\MySQLSchemaProvi ...

  4. bootsrap 上传插件fileinput 简单使用

    1.安装 下载fileinput文件,载入对应的css+js文件,如下: <link href="css/bootstrap.min.css" rel="style ...

  5. Oracle锁表查询以及解锁

    一.查看锁表进程SQL语句 select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo ...

  6. delphi如何模块内部获得自身路径ExtractFilePath和paramstr(0)

    如何模块内部获得自身路径?Exe程序:DLL程序: ExtractFilePath(Application.ExeName) GetCurrentDir TIniFile.Create(GetCurr ...

  7. hdu 6057 Kanade's convolution(子集卷积)

    题解: 然后就是接下来如何fwt 也就是如何处理bit(x) - bit(y) = bit(k)这个条件. 其实就是子集卷积. 把bit(x)和bit(y)划分成两个集合,然后就是子集卷积的形式. 这 ...

  8. [UOJ #51]【UR #4】元旦三侠的游戏

    题目大意:给$n$,一个游戏,给$a,b$,两个人,每人每次可以把$a$或$b$加一,要求$a^b\leqslant n$,无法操作人输.有$m$次询问,每次给你$a,b$,问先手可否必胜 题解:令$ ...

  9. MySQL事物机制具备四点:简称ACID操作

    MySQL事物机制具备四点:简称ACID操作 1.原子性:要么都做,要么都不做(两条数据(写入和存储)一步未成功,整体回滚) 2.一致性:数据库的状态改变(两条数据(写入和存储)均成功,符合原子性,但 ...

  10. 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)

    读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...