Logu P1306 Fibonacci common count, p1306 Fibonacci
Description
You should be familiar with the series of Fibonacci:, 13 ~~~ But now there is a very simple question: what is the maximum number of public appointments between the n and m items?
Input/Output Format
Input Format:
Two positive integers n and m. (N, m <= 10 ^ 9)
Note: The data is large.
Output Format:
The maximum number of common Fn and Fm.
Since reading big numbers makes you dizzy, you only need to output the last 8 digits.
Input and Output sample input sample #1: Copy
4 7
Output example #1: Copy
1
Description
Recursion and recurrence will time out.
The formula also times out.
Extended Euclidean has a very important nature
$ Gcd (F [I], F [j]) = F [gcd (I, j)] $
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #define LL long long 5 using namespace std; 6 const LL mod=100000000; 7 const LL MAXN=5000001; 8 inline LL read() 9 {10 char c=getchar();LL x=0,flag=1;11 while(c<'0'||c>'9') {if(c=='-') flag=-1;c=getchar();}12 while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();return x*flag;13 }14 LL dp[MAXN];15 LL gcd(LL a,LL b)16 {17 return b==0?a:gcd(b,a%b);18 }19 int main()20 {21 LL n=read(),m=read();22 dp[1]=1;dp[2]=1;23 for(LL i=3;i<=5000000;i++)24 dp[i]=(dp[i-1]+dp[i-2])%100000000;25 26 LL p=gcd(n,m);27 printf("%lld",dp[p]%100000000);28 return 0;29 }