Problem descriptionfollowing is the recursive definition of Fibonacci sequence:FI=???01Fi− 1+ fi−2< Span id= "mathjax-span-15" class= "mtext" >i = 0i = 1i > 1 Now we need to check whether a number can is expressed as the product of numbers in the Fibonacci sequence.
Inputthere is a numberTShows there isTTest cases below. (T≤ The first line contains a integers n, which means the number need to be checked. 0≤n≤1 ,
Outputfor each case output "Yes" or "No".
Sample INPUT3 4 17 233
Sample Outputyes No Yes
Sourcebestcoder Round #28
Test Instructions: Determine whether a number can be multiplied by any of the Fibonacci that Tina, can output "Yes", otherwise output "No"
idea: First to deal with Fibonacci that Tina Array, and then use a queue to achieve Fibonacci that Tina number of multiplication, with a map array to mark. Specific look at the code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stdlib.h>6#include <algorithm>7#include <queue>8#include <map>9 using namespacestd;Ten #defineN 46 One #definell Long Long A ll F[n]; -Map<ll,BOOL>MP; - voidInit () the { - mp.clear (); -Queue<ll>Q; -f[0]=0; +f[1]=1; -mp[0]=true; +mp[1]=true; AQ.push (0); atQ.push (1); - for(intI=2; i<n;i++) - { -f[i]=f[i-1]+f[i-2]; -mp[f[i]]=true; - Q.push (F[i]); in } - while(!q.empty ()) to { +ll tmp=Q.front (); - Q.pop (); the for(intI=0; i<n;i++) * { $ll cnt=tmp*F[i];Panax Notoginseng if(cnt>1000000000L) - Break; the if(mp[cnt])Continue; +mp[cnt]=true; A Q.push (CNT); the } + } - $ } $ intMain () - { - init (); the intT; -scanf"%d",&t);Wuyi while(t--) the { - ll N; Wuscanf"%i64d",&n); - if(mp[n]==true) Aboutprintf"yes\n"); $ Else -printf"no\n"); - - } A return 0; +}
View Code
HDU 5167 Fibonacci (pretreatment)