The GCD of Fibonacci Numbers

该博客探讨了如何计算斐波那契数列中两个数的GCD(最大公约数)。通过数学推导得出,GCD(f(m), f(n))可以通过递归公式Gcd(f(n+1),f(n)) * Gcd(f(m-n),f(n))来计算,最终简化为f(Gcd(m,n))。博主分享了避免暴力求解的方法,并提供了相关代码实现。" 135333590,10282337,华为OD机试真题:中庸行者地图移动最大次数,"['华为od', 'javascript', '编程题', '算法', 'dfs']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively zero and one. The first few numbers in the Recaman’s Sequence is 0,1,1,2,3,5,8,… The i-th Fibonacci number is denoted fi.

The largest integer that divides both of two integers is called the greatest common divisor of these integers. The greatest common divisor of a and b is denoted by gcd(a,b).

Two positive integers m and n are given,you must compute the GCD(fm,fn).

Input

The first linecontains one integer T(T ≤ 100),the number of test cases.
For each test case,there are two positive integers m and n in one line. (1 ≤m,n ≤ 231 , GCD(m,n) ≤ 45)

Output

Foreach the case, your program will outputthe GCD(fm,fn).

Sample Input
Input

4
1 2
2 3
2 4
3 6

Output

1
1
1
2

思路

自己做的时候知道不可能暴力,看着求Fibonacci和Gcd差不多,找规律没找到~
                              f(x)=f(x−1)+f(x−2)f\left( x\right) =f\left( x-1\right) +f\left( x-2\right)f(x)=f(x1)+f(x2)
所以 当 m>nm>nm>n时,有f(m)=f(n+1)f(m−n)+f(m−n−1)f(n)f\left( m\right) =f\left( n+1\right) f\left( m-n\right) +f\left( m-n-1\right) f\left( n\right)f(m)=f(n+1)f(mn)+f(mn1)f(n)  (自己推一下就可以出来撒)
所以有Gcd(f(m),f(n))=Gcd(f(n+1)f(m−n)+f(m−n−1)f(n),f(n))Gcd\left( f\left( m\right) ,f\

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值