"Hdoj" 3509 buge ' s Fibonacci number problem

Source: Internet
Author: User

The fast matrix is a power, and the coefficient matrix consists of several two-item distributions.
The 1th column is (0, (a+b) ^k)
The 2nd column is (0, (a+b) ^ (k-1), 0)
The 3rd column is (0, (a+b) ^ (k-2), 0,0)
And so on

1 /*3509*/2#include <iostream>3#include <string>4#include <map>5#include <queue>6#include <Set>7#include <stack>8#include <vector>9#include <deque>Ten#include <algorithm> One#include <cstdio> A#include <cmath> -#include <ctime> -#include <cstring> the#include <climits> -#include <cctype> -#include <cassert> -#include <functional> +#include <iterator> -#include <iomanip> + using namespacestd; A //#pragma COMMENT (linker, "/stack:102400000,1024000") at  - //#define DEBUG - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> in #definePII pair<int,int> - #defineVpii vector<pair<int,int> > to #defineRep (I, A, n) for (int i=a;i<n;++i) + #definePer (i, a, n) for (int i=n-1;i>=a;--i) - #defineCLR Clear the #definePB Push_back * #defineMP Make_pair $ #defineFIR FirstPanax Notoginseng #defineSEC Second - #defineAll (x) (x). Begin (), (x). End () the #defineSZ (x) ((int) (x). Size ()) + #defineLson L, Mid, rt<<1 A #defineRson mid+1, R, rt<<1|1 the  +typedefstructmat_t { -__int64 m[ -][ -]; $      $ mat_t () { -Memset (M,0,sizeof(m)); -     } the } mat_t; - Wuyi Const intMAXN = -; the __int64 A[MAXN], B[MAXN], F1[MAXN], F2[MAXN]; - intmod, L; Wu  - mat_t MatMult (mat_t A, mat_t b) { About mat_t C; $      -Rep (K,0, L) { -Rep (I,0, L) { -             if(A.m[i][k]) { ARep (J,0, L) { +                     if(B.m[k][j]) { theC.M[I][J] = (C.m[i][j] + a.m[i][k]*b.m[k][j]%mod)%MoD; -                     } $                 } the             } the         } the     } the      -     returnC; in } the  themat_t Matpow (mat_t A,intN) { About mat_t ret; the      theRep (I,0, L) Ret.m[i][i] =1; the      +      while(n) { -         if(N &1) theRET =MatMult (ret, a);BayiA =MatMult (A, a); theN >>=1; the     } -      -     returnret; the } the  the intMain () { theIos::sync_with_stdio (false); - #ifndef Online_judge theFreopen ("data.in","R", stdin); theFreopen ("Data.out","W", stdout); the     #endif94      the     intT; the     intF1, F2, A, b; the     intN, K_;98 __int64 ans; About mat_t E, tmp; -     intI, J, K;101 __int64 C;102     intL, R, NC;103     104scanf"%d", &t); the      while(t--) {106scanf"%d %d%d%d%d%d%d", &f1,&f2, &a,&b, &k_, &n, &MoD);107L = K_ +2;108a[0] = b[0] = f1[0] = f2[0] =1;109          for(i=1; i<l; ++i) { theA[i] = a[i-1] * A%MoD;111B[i] = b[i-1] * B%MoD; theF1[i] = f1[i-1] * F1%MoD;113F2[i] = f2[i-1] * F2%MoD; the         } the          thememset (E.M,0,sizeof(E.M));117e.m[0][0] = e.m[1][0] =1;118          for(j=1, K=k_; j<l; ++j,--k) {119              for(i=1, c=1, nc=k+1, r=k,l=1; nc ++i,--nc,c=c*r/l,--r,++l) { -E.M[I][J] = (c% MoD) * a[k+1-i]% mod * b[i-1] %MoD;121             }122         }123 #ifdef DEBUG124          for(i=0; i<l; ++i) { the              for(j=0; j<l; ++j)126printf"%i64d", E.m[i][j]);127Putchar ('\ n'); -         }129         #endif the         131TMP = MATPOW (E, n1); the         133Ans = f1[k_] * tmp.m[0][0] %MoD;134          for(j=1; j<l; ++j) {135Ans = (ans + f2[k_+1-j]*f1[j-1]%mod*tmp.m[j][0]%MOD)%MoD;136         }137         138printf"%i64d\n", ans);139     } $     141 #ifndef Online_judge142printf"Time =%d.\n", (int) clock ());143     #endif144     145     return 0;146}

"Hdoj" 3509 buge ' s Fibonacci number problem

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.