Fibonacci(dfs搜索)

Following is the recursive definition of Fibonacci sequence: 


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,000T≤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,0000≤n≤1,000,000,000

Output

For each case output "Yes" or "No".

Sample Input

3
4
17
233

Sample Output

Yes
No
Yes

剪枝!:dfs第二个参数意味着枚举时最多需要扫到的位置

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;

#define LL long long
#define mst(a) memset(a,0,sizeof(a)) 

const int max_n=2e5+5;
LL a[100],t,n;
int dfs(LL x,LL max1)
{
	if(x<=3)return 1;
	for(int i=3;i<=max1;i++)
	{
		if(x%a[i]==0)
		{
			if(dfs(x/a[i],i))return 1;
		}
	}
	return 0;
}
int main()
{
	//freopen("1.in","r",stdin);freopen("1.out","w",stdout);
	for(int i=0;i<=48;i++)
	{
		if(i==0)a[i]=0;
		if(i==1)a[i]=1;
		else a[i]=a[i-1]+a[i-2];
		//printf("%d\n",a[i]);
	}
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld",&n);
		if(dfs(n,48))
		{
			printf("Yes\n");
		}
		else printf("No\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43484493/article/details/88086112