HDU 1588 Gauss Fibonacci (matrix Rapid power + binary proportional sequence summation)
ACM
Topic address: HDU 1588 Gauss Fibonacci
Question:
G (I) = K * I + B; I is a variable.
Given K, B, n, m, question (f (G (0) + f (G (1) +... + f (G (N) % m value.
Analysis:
If we include the Fibonacci matrix, we will find that this is an equiratio sequence.
Push down:
S(g(i))
= F(b) + F(b+k) + F(b+2k) + .... + F(b+nk)
// Set a = {1, 1, 1}, (curly brackets indicate the Matrix ...)
// This is the change matrix of the FIB number. f (x) = (a ^ X}
= F(b) + (A^k)F(b) + (A^2k)F(b)+….+(A^nk)F(b)
// Extract the formula F (B)
= F (B) [E + A ^ K + A ^ 2 K + .... + A ^ NK] // (E indicates the unit matrix)
// After K = a ^ K
E + A ^ K + A ^ 2 K + .... + A ^ NK becomes k ^ 0 + k ^ 1 + k ^ 2 +... + K ^ n
Then, the proportional sequence can be summed by two points: Number Theory _ the binary summation of the proportional Series
Code:
/** Author: zhangz <iw.zen [at] gmail.com> * blog: http://blog.csdn.net/hcbbt* file: 1588. CPP * Create Date: 16:13:51 * descripton: matrix */# include <cstdio> # include <cstring> # include <iostream> # include <map> # include <algorithm> using namespace STD; # define repf (I,, b) For (INT I = (a); I <= (B); I ++) typedef long ll; const int n = 20; const int size = 2; // Max size of the matrixll MOD; ll K, B, N; Struct mat {int N; ll V [size] [size]; // value of matrix MAT (INT _ n = size) {n = _ n ;} void Init (LL _ v = 0) {memset (v, 0, sizeof (v); If (_ v) repf (I, 0, n-1) V [I] [I] = _ v;} void output () {repf (I, 0, n-1) {repf (J, 0, n-1) printf ("% LLD", V [I] [J]); puts ("");} puts ("") ;}} A, B, C; mat operator * (MAT a, mat B) {mat C (. n); repf (I, 0,. n-1) {repf (J, 0,. n-1) {C. V [I] [J] = 0; Re PF (K, 0,. n-1) {C. V [I] [J] + = (. V [I] [k] * B. V [k] [J]) % MOD; C. V [I] [J] % = mod ;}} return C;} mat operator ^ (MAT a, ll K) {mat C (. n); C. init (1); While (k) {If (K & 1) C = A * C; A = A * A; k >>=1;} return C ;} mat operator + (MAT a, mat B) {mat C (. n); repf (I, 0,. n-1) repf (J, 0,. n-1) C. V [I] [J] = (B. V [I] [J] +. V [I] [J]) % MOD; return C;} mat operator + (MAT a, LL B) {mat c = A; Re PF (I, 0,. n-1) C. V [I] [I] = (. V [I] [I] + B) % MOD; return C ;}// the sum of binary sums 1 .. nmat calc (MAT a, int N) {If (n = 1) return a; If (N & 1) Return (a ^ n) + calc (, n-1); else return calc (A, n/2) * (a ^ (n/2) + 1);} int main () {. init ();. V [0] [0] =. V [0] [1] =. V [1] [0] = 1; while (~ Scanf ("% LLD", & K, & B, & N, & mod) {B = (a ^ K ); C = calc (B, n-1) + (a ^ 0); C = (a ^ B) * C; printf ("% LLD \ n", C. V [0] [1]);} return 0 ;}