"Matrix fast Power" codeforces 450b-jzzhu and sequences (formula conversion)

Source: Internet
Author: User

"topic link"Click here~~

"The main topic"

Jzzhu have invented a kind of sequences, they meet the following property:

You is given x and y, please calculate fn modulo1000000007 (Ten9?+?7).

"problem-solving ideas"

/*a-jzzhu and sequencescodeforces 450b-jzzhu and sequences (Matrix fast power)given F1 and F2, ask for FNAnalysis:Special Award F1,f2when n>=3 is used, the matrix is quickly idempotent .convert the formula to get a transformation matrixby F (i) =f (i-1) +f (i+1);move the left-hand side to the right.f (i+i) =f (i)-f (i-1);the subscript is also reduced by onef (i) =f (i-1)-F (i-2);thus constructing the matrix(f (i-1), F (i-2)) *[1-1]= (f (i), F (i-1) )[1 0]brought into the i=3to be(f (2) =y,f (1) =x) *[1-1]^ (i-2) = (f (3), F (2))[1 0]The code is as follows * /
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < Math.h>using namespace Std;const long long mod=1e9+7; #define LL long longstruct matrlc{long Long mapp[2][2];} ans , base; MATRLC unit={1,0,0,1};    MATRLC mult (MATRLC A,MATRLC b) {MATRLC C;            for (int i=0, i<2; i++) for (int j=0; j<2; J + +) {c.mapp[i][j]=0;                for (int k=0; k<2; k++) c.mapp[i][j]+= (a.mapp[i][k]*b.mapp[k][j])%mod;        C.mapp[i][j]%=mod; } return C;}    void Pow1 (LL n) {base.mapp[0][0] = 1;    BASE.MAPP[0][1] =-1;    Base.mapp[1][0] = 1;    BASE.MAPP[1][1] = 0;    Ans.mapp[0][0] = ans.mapp[1][1] = 1;//ans initialized to the unit matrix ans.mapp[0][1] = ans.mapp[1][0] = 0;        while (n) {if (n&1) Ans=mult (ans,base);        Base=mult (base,base);    n>>=1;    }}int Main () {LL x,y,n,i,j;    scanf ("%lld%lld%lld", &x,&y,&n); if (n==1) printf ("%lld\n", (x%mod+mod)%mod);    else if (n==2) printf ("%lld\n", (y%mod+mod)%mod);        else {pow1 (N-2);        LL result= (((ans.mapp[0][0]*y+ans.mapp[0][1]*x)%mod) +mod)%mod;    printf ("%lld\n", result); } return 0;}

"Matrix fast Power" codeforces 450b-jzzhu and sequences (formula conversion)

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.