标签:for 最大 rgba com 思路 ble 链接 斐波那契数列 cst
考察:思维
顺着LCA的标签而来,结果这题貌似没什么关系(甚至想半天没想出来(
思路:
我们发现每个子结点和父节点都差一个斐波那契数.对于样例的树可以表示成 (1) (2) (3) (4 5) (6 7 8) (9 10 11 12 13) 每个数字减去斐波那契数列中最大但严格小于自己的数即可跳到父节点.
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 typedef long long LL; 5 const int N = 70; 6 LL f[N] = {0,1}; 7 int m; 8 LL lca(LL a,LL b) 9 { 10 while(a!=b) 11 { 12 if(a<b) swap(a,b); 13 int l = 0,r = 60; 14 while(l<r) 15 { 16 int mid = l+r+1>>1; 17 if(f[mid]<a) l = mid; 18 else r = mid-1; 19 } 20 a-=f[r]; 21 } 22 return a; 23 } 24 int main() 25 { 26 scanf("%d",&m); 27 for(int i=2;i<=N-10;i++) f[i] = f[i-1]+f[i-2]; 28 while(m--) 29 { 30 LL a,b; scanf("%lld%lld",&a,&b); 31 printf("%lld\n",lca(a,b)); 32 } 33 return 0; 34 }
标签:for 最大 rgba com 思路 ble 链接 斐波那契数列 cst
原文地址:https://www.cnblogs.com/newblg/p/14755505.html