题目
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(x−1)+f(x−2)
所以 当 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(m−n)+f(m−n−1)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\