Matrix Fast Power POJ 3070 Fibonacci

Source: Internet
Author: User

Topic Portal

1 /*2 Matrix Fast Power: To find the Fibonacci number of the nth term, transpose the matrix are given, set a template can be. It's high efficiency.3 */4#include <cstdio>5#include <algorithm>6#include <cstring>7#include <cmath>8 using namespacestd;9 Ten Const intMAXN = 1e3 +Ten; One Const intINF =0x3f3f3f3f; A Const intMOD =10000; - structMat { -     intm[2][2]; the }; -  - Mat Multi_mod (Mat A, Mat b) { -MAT ret; memset (RET.M,0,sizeof(RET.M)); +      for(intI=0; i<2; ++i) { -          for(intj=0; j<2; ++j) { +              for(intk=0; k<2; ++k) { ARET.M[I][J] = (Ret.m[i][j] + a.m[i][k] * b.m[k][j])%MOD; at             } -         } -     } -     returnret; - } -  in intMatrix_pow_mod (Mat x,intN) { -MAT ret; ret.m[0][0] = ret.m[1][1] =1; ret.m[0][1] = ret.m[1][0] =0; to      while(n) { +         if(N &1) ret =Multi_mod (ret, x); -x =multi_mod (x, x); theN >>=1; *     } $     returnret.m[0][1];Panax Notoginseng } -  the intMainvoid)  {//POJ 3070 Fibonacci +     intN; A      while(SCANF ("%d", &n) = =1)   { the         if(n = =-1) Break; + Mat x; -x.m[0][0] = x.m[0][1] = x.m[1][0] =1; x.m[1][1] =0; $printf ("%d\n", Matrix_pow_mod (x, N)); $     } -  -     return 0; the}

Matrix Fast Power POJ 3070 Fibonacci

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.