"HDOJ5950" Recursive sequence (matrix multiplication, fast power)

Source: Internet
Author: User

Test instructions: f[1]=a,f[2]=b,f[i]=2f[i-2]+f[i-1]+i^4 (i>=3), multi-group inquiry f[n] 2147493647 modulo

N,a,b < 2^31

Idea: The focus is on the treatment of i^4

For the I transfer matrix, it is possible to record its 0,1,2,3,4 times

The power of I can be obtained by the power of I-1, and the last coefficient is the coefficient of two-term expansion.

Try a new moment multiplication template

1#include <cstdio>2#include <cstring>3#include <string>4#include <cmath>5#include <iostream>6#include <algorithm>7#include <map>8#include <Set>9#include <queue>Ten#include <vector> One using namespacestd; AtypedefLong Longll; -typedef unsignedint UINT; -typedef unsignedLong Longull; thetypedef pair<int,int>PII; -typedef vector<int>VI; - #defineFi first - #defineSe Second + #defineMP Make_pair - #defineN 2100000 + #defineMOD 2147493647 A #defineEPS 1e-8 at #definePi ACOs (-1) - Const intmaxn=Ten; -  - intRead () - {  -    intv=0, f=1; in    CharC=GetChar (); -     while(c< -|| $&LT;C) {if(c=='-') f=-1; C=GetChar ();} to     while( -<=c&&c<= $) v= (v<<3) +v+v+c- -, c=GetChar (); +    returnv*F; - } the  *  $ structMatrix//Matrix classPanax Notoginseng {    -     intn,m; the ll DATA[MAXN][MAXN]; + }; A  the matrix MA,MB; + ll A,b,c,d,p,n; -  $ Matrix Matrixmul (matrix A, matrix B) $ { - matrix re; -     if(a.m!=B.N) the     { -printf"error\n");Wuyi         returnre; the     } -memset (Re.data,0,sizeof(Re.data)); WuRE.N = A.N; RE.M =b.m; -      for(inti =1; I <= A.N; i++) About     { $          for(intj =1; J <= a.m.; J + +) -         { -             if(A.data[i][j] = =0)Continue; -              for(intK =1; K <= B.M; k++) A             { +Re.data[i][k] + = (a.data[i][j]% mod * b.data[j][k]% MoD)%MOD; theRE.DATA[I][K]%=MOD; -             } $         } the     } the     returnre; the } the  -Matrix Matrixpow (Matrix A,intb) in { the matrix re; the     if(a.n!=a.m.) About     { theprintf"error2\n"); the         returnre; the     } +re.n=re.m=A.N; -memset (Re.data,0,sizeof(Re.data)); the      for(intI=1; i<=re.n;i++) re.data[i][i]=1;Bayi      while(b) the     { the         if(b&1) re=Matrixmul (re,a); -A=Matrixmul (a,a); -b>>=1; the     } the     returnre; the } the  - voidInputmat (intNintM,matrix &a,ll *b) the { theA.N = n; a.m. =m; the      for(inti =1; I <= N; i++)94          for(intj =1; J <= M; J + +) theA.DATA[I][J] = * (b + (I-1) * m + (J-1)); the } the 98 voidinit () { Aboutll pt[1][7] = {B,a, -,8,4,2,1}; -Inputmat (1,7, ma,*PT);101ll pt2[7][7] = {1,1,0,0,0,0,0,102                     2,0,0,0,0,0,0,103                     1,0,1,0,0,0,0,104                     4,0,4,1,0,0,0, the                     6,0,6,3,1,0,0,106                     4,0,4,3,2,1,0,107                     1,0,1,1,1,1,1,};108Inputmat (7,7, mb,*pt2);109 } the 111 intMain () the {113     intCAs; thescanf"%d",&CAs); the      while(cas--) the     {117scanf"%i64d%i64d%i64d",&n,&a,&b); 118         intI=3;119a%=MOD; -b%=MOD;121         if(n = =1)122printf"%i64d\n", a);123         Else if(n = =2)124printf"%i64d\n", b); the         Else126         {127              - init ();129Ma=matrixmul (Ma,matrixpow (mb,n-2)); the         }131printf"%i64d\n", ma.data[1][1]); the     }133     return 0;134}

"HDOJ5950" Recursive sequence (matrix multiplication, fast power)

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.