链接:https://ac.nowcoder.com/acm/contest/3674/A
来源:牛客网
题目描述
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 ith 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).
输入描述:
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)
输出描述:
Foreach the case, your program will outputthe GCD(fm,fn).
示例1
输入
4
1 2
2 3
2 4
3 6
输出
1
1
1
2
思路:这道题就是用到了一个公式,本题中n和m的值都很大,还有要注意的一点就是GCD(n,m)<46的
gcd(f[n],f[m])=f[gcd(n,m)];
#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b){
return a == 0 ? b : gcd(b % a, a);
}
int main()
{
int t;
cin>>t;
int f[46];
f[0]=0;
f[1]=1;
for(int i=2; i<=45; i++)
f[i]=f[i-1]+f[i-2];
while(t--)
{
int a,b;
cin>>a>>b;
cout<<f[gcd(a,b)]<<endl;
}
return 0;
}