首页 > 其他 > 详细

bestcoder#28 1002 dfs

时间:2015-03-23 01:55:10      阅读:243      评论:0      收藏:0      [点我收藏+]

bestcoder#28 1002  dfs

Fibonacci

Accepts: 40
Submissions: 996
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description

Following is the recursive definition of Fibonacci sequence:

Fi=???01Fi1+Fi2i = 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. (T100,000) For each test case , the first line contains a integers n , which means the number need to be checked. 0n1,000,000,000

Output

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

Sample Input
3
4
17
233
Sample Output
Yes
No
Yes

题意:判断n是否为一个或多个fib数的乘积
思路:dfs,从大数往小数遍历更省时
技术分享
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
#include<vector>

using namespace std;

const int maxn=1000100;
const int INF=1000000000;

int T;
int n;
vector<int> fib;

inline int read()
{
    char c=getchar();
    while(c!=-&&!isdigit(c)) c=getchar();
    int f=0,tag=1;
    if(c==-){
        tag=-1;
        f=getchar()-0;
    }
    else f=c-0;
    while(isdigit(c=getchar())) f=f*10+c-0;
    return f*tag;
}

void creat_fib()
{
    fib.clear();
    fib.push_back(0);
    fib.push_back(1);
    int x=fib[0]+fib[1];
    while(x<=INF&&x>=0){
        fib.push_back(x);
        x=fib[fib.size()-1]+fib[fib.size()-2];
    }
}

bool dfs(int n,int t)
{
    if(t==2) return false;
    while(true){
        if(dfs(n,t-1)) return true;
        if(n%fib[t]) break;
        n/=fib[t];
    }
    if(n==1) return true;
    return false;
}

int main()
{
    cin>>T;
    creat_fib();
    while(T--){
        n=read();
        if(n==0||n==1||dfs(n,fib.size()-1)) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
View Code

 

bestcoder#28 1002 dfs

原文:http://www.cnblogs.com/--560/p/4358647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!