hdu 5167 Fibonacci(DFS)

hdu 5167 Fibonacci

问题描述

斐波那契数列的递归定义如下:
Fi=???01Fi?1+Fi?2i = 0i = 1i > 1

现在我们需要判断一个数是否能表示为斐波那契数列中的数的乘积。

输入描述

有多组数据,第一行为数据组数T(T≤100,000)。
对于每组数据有一个整数n,表示要判断的数字。
0≤n≤1,000,000,000

输出描述

对于每组数据,如果可以输出“Yes”,否则输出"No"。

输入样例

3
4
17
233

输出样例

Yes
No
Yes

题目大意:中文题目。

解题思路:直接枚举会超时,可以先把符合条件的数(斐波那契数列中可以被num整除,小于num),存在一个数组中,减少DFS的时间。

#include<stdio.h>
#include<math.h>
#include<string.h>
long long fib[50], ans[50];
int num, flag, sum, cnt;
int DFS(int n, int p) {
	if (n == 1) {
		flag = 1;
		return 1;
	}
	for (int i = p; i < cnt; i++) {
		if (n % ans[i] == 0 && DFS(n / ans[i], i)) {
			flag = 1;
			return 1;
		}
	}
	return 0;
}
int main() {
	fib[0] = 0;
	fib[1] = 1;
	for (int i = 2; i < 46; i++) {
		fib[i] = fib[i - 1] + fib[i - 2];
	}
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &num);
		flag = cnt = 0;
		if (num == 0) {
			printf("Yes\n");
			continue;
		}
		for (int i = 3; fib[i] <= num; i++) {
			if (num == fib[i]) {
				flag = 1;
				break;
			}
			if (num % fib[i] == 0) {
				ans[cnt++] = fib[i];
			}
		}
		if (!flag) DFS(num, 0);
		if (flag) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}
时间: 2024-10-18 19:46:47

hdu 5167 Fibonacci(DFS)的相关文章

HDU 5167 Fibonacci(BestCoder Round #28)

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 

hdu 1568 Fibonacci (数论)

Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3654    Accepted Submission(s): 1671 Problem Description 2007年到来了.经过2006年一年的修炼,数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]=0,f[1]=1;f[i]

HDU 3158 PropBot(DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3158 Problem Description You have been selected to write the navigation module for PropBot. Unfortunately, the mechanical engineers have not provided a lot of flexibility in movement; indeed, the PropBot

Hdu 1175 连连看(DFS)

Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu 1728相比,都要考虑转弯次数,所以在判断转弯的次数上,两者可以相互借鉴. 这一点应该不难想到,在搜索前就应判断两点的值是否相等,以及两点的值中是否有0.如果不相等或其中一值为0,则可以判断是"NO". 时间虽是10000ms,但只是这样,还是超时. 后来又加了一个数组walk[][],用

HDU 5305 Friends(dfs)

Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 297    Accepted Submission(s): 127 Problem Description There are n people and m pairs of friends. For every pair of friends, they can cho

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

hdu 1518 Square (dfs搜索可参考poj1011)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8589    Accepted Submission(s): 2784 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

HDU 2209 翻纸牌游戏(dfs)

翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2180    Accepted Submission(s): 787 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张