noip模拟赛 斐波那契

分析:暴力分有90,真良心啊.

      a,b这么大,连图都建不出来,肯定是有一个规律.把每个点的父节点写出来:0 1 1 12 123 12345 12345678,可以发现每一个循环的长度刚好是斐波那契数列中的第i项,那么求个前缀和,二分求一下LCA就可以了.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

ll sum[70], f[70], m, a, b;

int main()
{
    f[1] = 1, f[2] = 1;
    sum[1] = 2, sum[2] = 3;
    for (int i = 3; i <= 65; i++)
        f[i] = f[i - 1] + f[i - 2], sum[i] = sum[i - 1] + f[i];
    scanf("%lld", &m);
    while (m--)
    {
        scanf("%lld%lld", &a, &b);
        if (a == b)
        {
            printf("%lld\n", a);
            continue;
        }
        if (a < b)
            swap(a, b);
        while (a != b && a != 1 && b != 1)
        {
            ll l = 1, r = 65, res = 1;
            while (l <= r)
            {
                ll mid = (l + r) >> 1;
                if (sum[mid] < a)
                {
                    res = mid;
                    l = mid + 1;
                }
                else
                    r = mid - 1;
            }
            a = a - sum[res];
            if (b > a)
                swap(a, b);
        }
        if (a == 1 || b == 1)
            printf("1\n");
        else
            printf("%lld\n", a);
    }

    return 0;
}

 

posted @ 2017-11-05 23:31  zbtrs  阅读(252)  评论(0编辑  收藏  举报