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

Source: Internet
Author: User

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:

 
 
  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.