[Matrix fast Power + follow link]hdu4291

Source: Internet
Author: User
Tags mul

Test instructions
Given N (1 <= n <= 1018), you should solve for
G (g (n)) MoD 109 + 7
where
G (N) = 3g (n-1) + g (n-2)
G (1) = 1
G (0) = 0
Analysis:
This recursive relationship can be solved with a matrix quick power, but the problem is that the MoD is large and will explode long and time out. Then this will require some stunts.
So see everyone use of the Cycle festival, but on the Internet why to take this cycle of the festival is vague or irrelevantly replying, probably not too know, know can be a mention on it, or the great God think these pediatrics things do not need to mention. My understanding is that this is an independent variable and function value of the relationship between the use of it, such as g (x)%mod, with the increase of G (X), one day will be larger than mod, and then be turned into 0, restart, so g (x) is a loop, its cycle is mod, then g (x) Changes with the change of X, then the g (x) cycle, X will also have a corresponding circular section (which is determined by the nature of the function, not all functions have a cyclic section, at least this kind of Fibonacci sequence of functions can be), that is, when x reaches a value of N, g (x) ==mod, After X is greater than N, g (x) is also larger than mod, so the n+1 is the same as the previous 1 effect, so the x loop is n. In this problem, there are multiple loop sections because of the nesting of functions, then the outermost derived arguments of the loop section Mod1 is the function of the nested function of the value of the loop section, recursion, to find three loop section.

#include <iostream>#include <cstdio>#include <stack>#include <vector>#include <queue>#include <cstring>using namespace STD;#define Read Freopen ("Q.in", "R", stdin)#define LL Long LongLL MoD;structmatri{LL mat[2][2];//matri () {memset (mat,0,sizeof (MAT));}    voidInit () {mat[0][1]=mat[1][0]=0; mat[0][0]=mat[1][1]=1; }}; Matri Mul (Matri A,matri b) {Matri res;intI,j,k; for(i=0;i<2; i++) { for(j=0;j<2; j + +) {res.mat[i][j]=0; for(k=0;k<2; k++) {res.mat[i][j]= (res.mat[i][j]+ (a.mat[i][k]*b.mat[k][j])%mod)%mod; }        }    }returnRes;} MatriExp(Matri a,ll N)    {Matri res; Res.init ();if(n==0)returnResif(n==1)returnA while(n) {if(n&1) Res=mul (res,a);        A=mul (A,a); n>>=1; }returnRes;}intMain () {Matri B; b.mat[0][0]=3; b.mat[0][1]=1; b.mat[1][0]=1; b.mat[1][1]=0;//Circle ();    //cout<<mod<< "?? £í£í£í£í£í?? " <<endl;LL N; while(~scanf("%i64d", &n)) {if(n==0)        {puts("0");Continue; }if(n==1)        {puts("1");Continue; } mod=183120L; Matri res=Exp(b,n-1); n=res.mat[0][0];if(n!=0&& n!=1) {mod=222222224L; res=Exp(b,n-1); n=res.mat[0][0]; }if(n!=0&& n!=1) {mod=1000000007L; res=Exp(b,n-1); }cout<<res.mat[0][0]<<endl; }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Matrix fast Power + follow link]hdu4291

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.