Editorial for Có phải số Fibo?


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

\(\color{red}{\text{Spoiler Alert}_{{}_{{}^{{}^{v2.0}}}}}\)

\(\color{red}{\text{Khuyến khích bạn đọc trước khi đọc phần lời giải xin hãy thử code ra thuật của mình dù nó có sai hay đúng}}\)

\(\color{red}{\text{Sau đó từ phần bài giải và thuật toán trước đó mà đối chiếu, rút nhận xét với thuật của mình và thu được bài học (không lãng phí thời gian đâu).}}\)

---

\(\color{orange}{\text{Hint }}\)

  • Dùng set để lưu tất cả các số Fibonacci bé hơn \(1e10\)
  • Dùng phương thức find của set để kiểm tra một số có phải là số Fibonacci hay không ?

\(\color{green}{\text{Preference AC Code }}\): Online Solving

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
set<ll> s;
set<ll>::iterator it;
ll a,b,c,i,n,t;
int main(){
    a=0,b=1;
    s.insert(1);
    while(1){
        c=b;
        b=b+a;
        a=c;
        s.insert(b);
        if(b>(ll)(1e10)) break;
    }
    cin>>t;
    while(t--){
        cin>>n;
        it=s.find(n);
        if(it==s.end()) cout<<"IsNotFibo"<<'\n';
        else cout<<"IsFibo"<<'\n';
    }
    return 0;
}


Comments

There are no comments at the moment.