Luogu P3938 斐波那契

Luogu P3938 斐波那契

第一眼看到这题,想到的是LCA,于是开始想怎么建树,倒是想出了\(n^{2}\)算法,看了下数据范围,果断放弃

想了想这数据范围,大的有点不正常,这让我想起了当年被小凯支配的恐惧QAQ

看了大约\(\mathcal{10min}\)后找出规律:根节点减去一个最接近它的小于等于它的Fibonacci数列中的数,就是它的父亲节点

然后就很简单了,先把Fibonacci打表,然后二分查找(\(\mathfrak{STL}\)大法好)

最后注意一点:不要忘了开\(\tt{long long}\)

夸赞一句:这个题思路真奇妙

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll f[1000010],tot;
ll read(){
    ll k=0; char c=getchar();
    for(;c<'0'||c>'9';) c=getchar();
    for(;c>='0'&&c<='9';c=getchar())
      k=(k<<3)+(k<<1)+c-48;
    return k;
}
int main(){
    f[0]=f[1]=1;
    for(int i=2;f[i-1]<=1e12;i++){
        f[i]=f[i-1]+f[i-2];
        tot++;
    }
    int m=read();
    while(m--){
        ll x=read(),y=read();
        if(x==y){
            printf("%lld\n",x);  continue;
        }
        while(x!=y){
            if(x<y) swap(x,y);
            int pos=lower_bound(f+1,f+tot+1,x)-f-1;
            x-=f[pos];
        }
        if(x) printf("%lld\n",x);
        else printf("1");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wxl-Ezio/p/9293454.html