problem 1683 Memorial Slingshotaccept:561 submit:1969
Time limit:1000 mSec Memory limit:32768 KBProblem Description
Known f (n) =3 * F (n-1) +2 * F (n-2) +7 * F (n-3), n>=3, where f (0) =1,f (1) =3,f (2) = 5, for each n given, output f (0) + F (1) + ... + f (n) MoD 2009. The first line of input is an integer m, representing a total of M cases. Output one row for each case. The format is shown in the sample, with a space after the colon. Sample Input236 Sample Outputcase 1:37case 2:313 sourcefoj Month-February 2009-Coralf (N) =3 * F (n-1) +2 * F (n-2) +7 * F (n-3)set S (n) =f (0) +f (1) + ... +f (n)also, S (n) =s (n-1) +f (n), gets the matrix below.
That is, the result is calculated using the Matrix fast power operation.
1#include <iostream>2#include <algorithm>3#include <map>4#include <vector>5#include <functional>6#include <string>7#include <cstring>8#include <queue>9#include <Set>Ten#include <cmath> One#include <cstdio> A using namespacestd; - #defineIOS Ios_base::sync_with_stdio (false)] -typedefLong LongLL; the Const intinf=0x3f3f3f3f; - - Const intmodnum= the; - Const intmaxn=5; +typedefstructmatrix{ - intV[MAXN][MAXN]; + voidInit () {memset (V,0,sizeof(v));} A }m; at M A, b; -M Mul_mod (ConstM &a,ConstM &b,intLintMintN) - { - M C; c.init (); - for(intI=0; i<l;i++){ - for(intj=0; j<n;j++){ in for(intk=0; k<m;k++)//Note J,k Range -C.v[i][j]= (c.v[i][j]+ (a.v[i][k]*b.v[k][j]%modnum))%Modnum; to } + } - returnC; the } *M Power (M x,intLintp) $ {Panax Notoginseng M tmp; Tmp.init (); - for(intI=0; i<l;i++) thetmp.v[i][i]=1; + while(p) { A if(p&1) tmp=Mul_mod (x,tmp,l,l,l); thex=Mul_mod (x,x,l,l,l); +p>>=1; - } $ returntmp; $ } - voidInit () - { the a.init (); -a.v[0][0]=1;Wuyia.v[0][1]=a.v[1][1]=3; thea.v[0][2]=a.v[1][2]=2; -a.v[0][3]=a.v[1][3]=7; Wua.v[2][1]=a.v[3][2]=1; - b.init (); Aboutb.v[0][0]=9; $b.v[1][0]=5; -b.v[2][0]=3; -b.v[3][0]=1; - } A intMain () + { the intM,n,ca=0; -scanf"%d",&m); $ while(m--){ thescanf"%d",&n); the init (); the if(n<3) {printf ("Case %d:%d\n", ++ca,b.v[3-n][0]);Continue;} theA=power (A,4, N-2); -A=mul_mod (A, B,4,4,1); inprintf"Case %d:%d\n", ++ca,a.v[0][0]); the } the}
View Code
Fzu 1683 Memorial Slingshot