Number of Fibonacci conventions (Luogu 1306)

Source: Internet
Author: User
Tags gcd greatest common divisor mul

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.