HDU 4549 m Fibonacci sequence (Fast power matrix fast Power Fermat theorem)

Source: Internet
Author: User

Topic link http://acm.hdu.edu.cn/showproblem.php?pid=4549;

It is easy to understand the topic of Chinese. But at first I read the wrong question, this problem haha. At first I read the wrong question, used a quick power to solve, needless to say, WA, see the passage rate of the topic is not high, I think there will be what pit ah. But I'm the big pit, haha.

Do not say, directly to say the question, first discuss k=1,2,3; This should be solved, not much to say;

Starting from item fourth F (4) =a^1+b^2;f (5) =a^2+b^3;f (6) =a^3+b^5 ...; See, the index on a is Cheng Feibo, and B is the same, and the index of a is the previous item of B;

Then the table to find the Fibonacci sequence, dare not, the scope is too big, and burst memory and overtime.

F (n) =a^k (n-4) +b^k (n-3), wherein n>=4;k (0) =1;k (1) = 2;

A matrix is used to find the adjacent two items of the Fibonacci sequence [K[n-2],k[n-1]]*[0 1]

[1 1]

The above formula equals [K[n-1],k[n]];

Then request [K[n-4],k[n-3]]=[1,2]*[0 1]^ (n-3)

[1 1]

In this way, the matrix can be used to quickly power the k[n-4],k[n-3];

But the light is not enough, in the matrix fast power process also has to take the mold, otherwise it will overflow.

The Fermat theorem has x^p==x for any integer x in the case of P as a prime number (mod p)

If x cannot be divisible by P with x^ (p-1) =1 (mod p), because a,b<1e9; can not be divisible by 1e9+7, K[n], then a^k[n]%p=a^ (k[n]% (p-1))%p;

The proof is as follows: k[n]=m* (p-1) +d; then a^k[n]%p=a^[(m* (p-1)) +d]%p= (a^[m* (p-1)]%p*a^ (d)%p)%p;

The a^ (m* (p-1))%p=1 is known by the Fermat theorem; and d=k[n]% (p-1);

So the matrix of the fast power to 1e+6 to take the mold on the line; when K (n) is obtained is then a fast power operation can be, the complexity of 2*LOG2 (n); Be careful to open longlong or it will overflow.

Here's a look at the code:

1#include <stdio.h>2#include <string.h>3#include <iostream>4#include <algorithm>5#include <stdlib.h>6#include <math.h>7 Const Long Longn=1e9+7;8 Const Long Longm=1e9+6;9typedefLong Longll;Ten voidKK (ll y); One ll pp (ll x,ll y); All b[2][2]; -ll a[2][2]; - using namespacestd; the intMainvoid) - { - ll I,j,k,p,q,n,m; -  +      while(SCANF ("%lld%lld%lld", &p,&q,&k)! =EOF) -     { +         if(k==0) A         { atprintf"%lld\n", p%N); -  -         } -         Else if(k==1) -         { -printf"%lld\n", q%N); in  -         } to         Else if(k==2) +         { -printf"%lld\n", (p%n*q%n)%N); the         } *         Else $         {Panax Notoginsengk=k-3; - KK (k); thell p1= (b[0][0]%m+2*b[1][0]%M)%M;//Request K (n-4) +ll P2= (b[0][1]%m+2*b[1][1]%M)%M;//K[n-3] All x1=pp (P,P1);//a^k (n-4); thell x2=pp (Q,P2);//b^k (n-3); +ll dd= (x1*x2)%N;//d= (A^k (n-4) *b^k (n-3))%N; -printf"%lld\n", DD); $  $         } -  -     } the  -     return 0;Wuyi  the } -  WuLL pp (ll X,ll y)//Fast Power - { Aboutll p=x; $ll q=1; -  -      while(y) -     { A         if(y&1) +         { theq= (q%n*p%n)%N; -         } $p= (p%n*p%n)%N; the  they=y/2; the     } the     returnQ; -  in } the  the  About voidKK (ll y)//The fast power form of matrix is the same as that of fast power.  the { the ll I,j,k; the ll x1,x2,x3,x4; +a[0][0]=0;//A is a transformation matrix; -a[0][1]=1; thea[1][0]=1;Bayia[1][1]=1; theb[0][0]=1;//b is the unit array; theb[0][1]=0; -b[1][0]=0; -b[1][1]=1; the  the      while(y) the     { the         if(y&1) -         { theX1= ((b[0][0]*a[0][0])%m+ (b[0][1]*a[1][0])%M)%M; theX2= ((b[0][0]*a[0][1])%m+ (b[0][1]*a[1][1])%M)%M; theX3= ((b[1][0]*a[0][0])%m+ (b[1][1]*a[1][0])%M)%M;94X4= ((b[1][0]*a[0][1])%m+ (b[1][1]*a[1][1])%M)%M; theb[0][0]=X1; theb[0][1]=x2; theb[1][0]=X3;98b[1][1]=x4; About         } -X1= ((a[0][0]*a[0][0])%m+ (a[0][1]*a[1][0])%M)%M;101X2= ((a[0][0]*a[0][1])%m+ (a[0][1]*a[1][1])%M)%M;102X3= ((a[1][0]*a[0][0])%m+ (a[1][1]*a[1][0])%M)%M;103X4= ((a[1][0]*a[0][1])%m+ (a[1][1]*a[1][1])%M)%M;104a[0][0]=X1; thea[0][1]=x2;106a[1][0]=X3;107a[1][1]=x4;108Y/=2;109  the     }111  the 113  the  the}

HDU 4549 m Fibonacci sequence (Fast power matrix fast Power Fermat theorem)

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.