P3938 斐波那契
我们想一下,第几个生的。那他的孩子就是排在新一波出生的第几个上的。
然后我们通过瞎试得到。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;
}

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号