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
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
4
17
233
Sample Output
Yes
No
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 ;
}