hdu 5167(dfs)
Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2400 Accepted Submission(s): 610
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>
#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)的更多相关文章
- HDU 5143 DFS
分别给出1,2,3,4 a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合 ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- HDU 5167 Fibonacci 筛法+乱搞
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5167 题意: 给你一个x,判断x能不能由斐波那契数列中的数相乘得到(一个数可以重复使用) ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- hdu 1716(dfs)
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Problem Description Ray又对数字的列产生了兴趣:现 ...
随机推荐
- sendto函数的坑
测试unix数据报套接字时,一个程序收,一个程序发,分别绑定自己的socket.结果在收的部分,返回的发送方的地址总是空的,但是返回的地址长度又是对的. ) { bzero(&clientad ...
- java-2018-01-17计划
1.一句英语 包括单词 2.一个java版本的设计模式 参考:https://github.com/iluwatar/java-design-patterns 学习了抽象工厂模式 总结:java的RS ...
- java对象-String的用法
以某字符串结尾:“”.endwith("") 字符串比较:equals(anotherstr) 命名遵循英文语法
- 【题解】SCOI2009围豆豆
很久之前就很想做的一道题,一直思考到今天才下定决心看题解.这道题中,很关键的一点就在于:如何判断一个点是否在一个多边形内?其实如果计算几何基本功扎实的话,应该是可以很快给出答案的(可惜我完全不行):由 ...
- Maven 标准目录结构
Maven 标准目录结构 好的目录结构可以使开发人员更容易理解项目,为以后的维护工作也打下良好的基础.Maven2根据业界公认的最佳目录结构,为开发者提供了缺省的标准目录模板.Maven2的标准目录结 ...
- ntpq –p命令
ntpq用来监视ntpd操作,ntpq -p查询网络中的NTP服务器,同时显示客户端和每个服务器的关系 [root@ ~]# ntpq -p remote refid st t when poll r ...
- GoogleMap在js中的应用
<html> <head> <meta name="viewport" content="initial-scale=1.0, user-s ...
- 我们用CloudStack做什么
原文地址:http://www.sdfengxi.com/?p=376 我想很多同学会有类似的疑问,就是我配置好了CloudStack或者OpenStack之类的环境之后能够提供什么服务或者应用呢?下 ...
- C++中的各种“神奇”东西
将光标放到任意的位置 void gotoxy(int x,int y)//位置函数 { COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(Ge ...
- stdafx.h、stdafx.cpp的作用
这两个文件用于建立一个预编译的头文件".PCH"和一个预定义的类型文件"STDAFX.OBJ".由于MFC体系结构非常大,各个源文件中都包含许多头文件,如果每次 ...