Fibonacci hdu 5167

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
BestCoder Round #28


dfs 搜索即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

ll cal(ll x) {
	ll ans = 0;
	while (x) {
		ans += (x % 10);
		x /= 10;
	}
	return ans;
}

ll n;
int T;
int f[50];
bool fg;
int k;
int a[50];

bool dfs(int n, int stp) {
	if (n == 1) {
		fg = true;
		return  true;
	}
	if (fg)return true;
	for (int i = stp; i < k; i++) {
		if (n%a[i] == 0) {
			if (dfs(n / a[i], i))return true;
		}
	}
	return false;
}

int main()
{
	//ios::sync_with_stdio(false);
	rdint(T);
	f[0] = 0; f[1] = 1;
	for (int i = 2; i <= 45; i++) {
		f[i] = f[i - 1] + f[i - 2];
	}
	while (T--) {
		rdllt(n); fg = false;
		ms(a); k = 0;
		if (n == 0) {
			cout << "Yes" << endl; continue;
		}
		for (int i = 3; i <= 45; i++) {
			if (n%f[i] == 0) {
				a[k] = f[i]; k++;
			}
		}
		if (dfs(n, 0))cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/82939741