Topic Links:
Problem 2198 come on, get a count.
Title Description:
Give the n hexagonal row in a row, A[i] represents the number of spanning tree I can make up, set s[i] equals a[1]+a[2]+a[3]+....+a[i-1]+a[i], ask S[n]?
Problem Solving Ideas:
n range of values [1, 1018], the table memory is not enough, then you need to consider the fast power! Nani!!!! Fast power to write out to unexpectedly time-out, dare to believe? Sure enough, there are too few problems to see. (GG)
For a[n] = 6*a[n-1]-a[n-2], it can be clearly seen.
Then the sum of the time will be reduced, but not difficult, the final formula is: s[n] = 6*s[n-1]-s[n-2] + 5; then constructs the matrix, the preprocessing constructs the matrix, runs quickly powers can!!
Code:
1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <algorithm>5#include <vector>6#include <queue>7#include <cstring>8 using namespacestd;9 Ten #defineLL Long Long One ConstLL MAXN =3; A ConstLL mod =1000000007; - structMat - { the LL col, row; - LL P[MAXN][MAXN]; -} pp[ $]; - + Mat Mul (Mat A, Mat b); - Mat Pow (ll N, ll Res, Mat B); + A intMain () at { - LL N, t; - Mat B; -scanf ("%lld", &t); -memset (pp[0].P,0,sizeof(pp[0].p)); -memset (B.P,0,sizeof(B.P)); inpp[0].col = pp[0].row = B.row =MAXN; -B.col =1; topp[0].p[0][0] =6; +pp[0].p[1][0] = -1; -pp[0].p[0][1] = pp[0].p[2][0] = pp[0].p[2][2] =1; theb.p[0][0] =6; *b.p[0][1] =0; $b.p[0][2] =5;Panax Notoginseng for(intI=1; i< $; i++) -Pp[i] = Mul (pp[i-1], pp[i-1]); the + while(T--) A { the Mat A; +scanf ("%lld", &n); -A = POW (n1,0, b); $printf ("%lld\n", a.p[0][0] %MoD); $ } - - return 0; the } - Wuyi Mat Mul (Mat A, mat b) the { - Mat C; WuC.col =A.col; -C.row =B.row; Aboutmemset (C.P,0,sizeof(C.P)); $ for(intk=0; k<a.row; k++) - for(intI=0; i<a.col; i++) - { - if(A.p[i][k] = =0)Continue; A for(intj=0; j<b.row; J + +) + { the if(B.p[k][j] = =0)Continue; -C.P[I][J] = (C.p[i][j] + a.p[i][k] * B.p[k][j] + MoD)%MoD; $ } the } the returnC; the } the - Mat Pow (ll N, ll Res, Mat B) in { the while(n) the { About if(n%2) theb =Mul (b, Pp[res]); theRes + +; theN/=2; + } - returnb; the}
It's a very ugly writing! Don't squirt me (cover your face ~ ~ ~ ~ ~ ~)
Fzu problem 2198 come on, come on. Count (Fast Power + optimize)