码迷,mamicode.com
首页 > 其他好文 > 详细

P3938 斐波那契

时间:2018-07-07 23:26:51      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:algorithm   暴力   main   space   clu   pre   else   www.   https   

坑爹入口

我们想一下,第几个生的。那他的孩子就是排在新一波出生的第几个上的。

然后我们通过瞎试得到。10^12<斐波那契的第60项。就是说我们不用建图(也建不下),每次最多60次暴力就可以了。

出题人真是个人才。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
long long f[61];
int find(long long val)
{
    int l=1,r=60,mid;
    while(l<r)
    {
        mid=(l+r)>>1;
        if(val>f[mid])  l=mid+1;
        else    r=mid;  
    }
    return l-1;
}
long long lca(long long a,long long b)
{
    while(a!=b)
    {
        if(a<b) swap(a,b);
        a-=f[find(a)];
    }
    return a;
}
int main()
{
    f[0]=0;f[1]=1;
    for(int i=2;i<=60;i++)  f[i]=f[i-1]+f[i-2];
    /*printf("%lld",find(11));*/
    int n;long long a,b;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",lca(a,b));
    }
    return 0;
}

P3938 斐波那契

标签:algorithm   暴力   main   space   clu   pre   else   www.   https   

原文地址:https://www.cnblogs.com/Lance1ot/p/9278785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!