HDU 1588 Gauss Fibonacci (matrix Rapid power + binary proportional sequence summation)

Source: Internet
Author: User
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:

 
 
  1. S(g(i))
  2. = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk)
  3. // Set a = {1, 1, 1}, (curly brackets indicate the Matrix ...)
  4. // This is the change matrix of the FIB number. f (x) = (a ^ X}
  5. = F(b) + (A^k)F(b) + (A^2k)F(b)+….+(A^nk)F(b)
  6. // Extract the formula F (B)
  7. = F (B) [E + A ^ K + A ^ 2 K + .... + A ^ NK] // (E indicates the unit matrix)
  8. // After K = a ^ K
  9. 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 ;}


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.