hdoj-5167-Fibonacci
2016-07-30 23:27
357 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
For each case output "Yes" or "No".
[align=left]Sample Input[/align]
3
4
17
233
[align=left]Sample Output[/align]
Yes
No
Yes
一定会要先打表把所有的斐波那契数求出来,然后直接dfs就ok了,然后又点剪枝
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 100000
ll f
,n;
int cnt;
bool flag;
void init()
{
f[0]=0;
f[1]=1;
cnt=2;
for(; f[cnt-1]<=1000000000; cnt++)
f[cnt]=f[cnt-1]+f[cnt-2];
}
void dfs(ll k,int num)
{
if(k==1){
flag=true;
return;
}
for(int i=num;i>=3;i--)
{
if(f[i]>k) continue;
if(k%f[i]==0)
dfs(k/f[i],i);
if(flag) return;
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if(!n||n==1) printf("Yes\n");
else
{
flag=false;
dfs(n,cnt-1);
if(flag) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
For each case output "Yes" or "No".
[align=left]Sample Input[/align]
3
4
17
233
[align=left]Sample Output[/align]
Yes
No
Yes
一定会要先打表把所有的斐波那契数求出来,然后直接dfs就ok了,然后又点剪枝
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 100000
ll f
,n;
int cnt;
bool flag;
void init()
{
f[0]=0;
f[1]=1;
cnt=2;
for(; f[cnt-1]<=1000000000; cnt++)
f[cnt]=f[cnt-1]+f[cnt-2];
}
void dfs(ll k,int num)
{
if(k==1){
flag=true;
return;
}
for(int i=num;i>=3;i--)
{
if(f[i]>k) continue;
if(k%f[i]==0)
dfs(k/f[i],i);
if(flag) return;
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&n);
if(!n||n==1) printf("Yes\n");
else
{
flag=false;
dfs(n,cnt-1);
if(flag) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
相关文章推荐
- 【BestCoder】 HDOJ 5167 Fibonacci
- HDOJ 5018 Revenge of Fibonacci
- HDOJ Fibonacci Again
- hdoj-1568-Fibonacci
- (SG函数)Fibonacci again and again--HDOJ
- hdu 5167-Fibonacci
- hdoj 5018 Revenge of Fibonacci
- HDOJ 题目5018 Revenge of Fibonacci(水题)
- HDOJ 1848.Fibonacci again and again SG函数
- hdu 5167 Fibonacci(DFS)
- hdoj 5018 Revenge of Fibonacci
- hdoj2046(Fibonacci 数列(找规律))(骨牌铺方格)
- 【DFS】hdu 5167 Fibonacci
- hdu-5167 Fibonacci
- HDOJ 1250 Hat's Fibonacci
- HDOJ-----1568---Fibonacci数学题
- hdoj-【1568 Fibonacci】
- hdoj problem 5018 Revenge of Fibonacci(杂码&&菲菠数列)
- 杭电OJ(HDOJ)2041题:超级阶梯(规律递推,Fibonacci)
- 【预处理】 HDU 5167 Fibonacci