HDU 1588 Gauss Fibonacci (matrix Rapid power + binary proportional sequence summation), hdufibonacci
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 ;}