Title Description
For Fibonacci series: 1,1,2,3,5,8,13 ... everyone should be familiar with it. But now there is a very simple question: what is the greatest common divisor of the nth and the M items?
Update: Added a set of data.
input/output format
Input format:
Two positive integers n and M. (n,m<=10^9)
Note: The data is very large
Output format:
FN and FM greatest common divisor.
As a result of looking at the large number of dizziness, so as long as the output of the last 8 digits can be.
input/Output sampleInput Sample
4 7
Output Sample
1
Description
Recursion & recursion will time out
Using the pass formula will also time out
Code
#include <stdio.h>#include<string.h>#include<algorithm>#definell Long Longusing namespacestd;Const intp=1e8;structNode {ll mp[3][3]; inth,l;} P,ans;intgcdintXinty) { if(x<y) Swap (x, y); if(x%y==0)returny; Else returnGCD (y,x%y);} Node Mul (node X,node y) {node tep; memset (&tep,0,sizeof(TEP)); for(intI=0; i<x.h;++i) for(intj=0; j<y.l;++j) for(intk=0; k<x.l;++k) Tep.mp[i][j]= (Tep.mp[i][j]+x.mp[i][k]*y.mp[k][j])%P; Tep.h=x.h,tep.l=Y.L; returnTep;}intJuc (ll k) {ans.mp[0][0]=ans.mp[0][1]=1; Ans.h=1, ans.l=2; p.mp[0][0]=p.mp[0][1]=p.mp[1][0]=1; P.h=p.l=2; while(k) {if(k&1) ans=Mul (ans,p); P=Mul (p,p); K>>=1; } returnans.mp[0][0];}intMain () {intn,m; scanf ("%d%d",&n,&m); intD=gcd (n,m); if(d<=2) printf ("1"); Elseprintf"%d", Juc (d2)); return 0;}
Number of Fibonacci conventions (Luogu 1306)