Title Link: http://poj.org/problem?id=3070
.
Another way to represent Fibonacci is to power the matrix;
So it's a fast-power matrix. Fast Matrix Learning
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<math.h>using namespacestd;#defineN 10structnode{inta[n][n];} S,e;node mul (node p, node Q)///the product of two matrices is obtained;{node tem; for(intI=0; i<2; i++) { for(intj=0; j<2; J + +) {Tem.a[i][j]=0; for(intk=0; k<2; k++) Tem.a[i][j]= (Tem.a[i][j]+p.a[i][k]*q.a[k][j])%10000; } } returntem;} Node Pow (node p,intN) {node tem; for(intI=0; i<2; i++) for(intj=0; j<2; J + +) Tem.a[i][j]= (i==j); while(n) {if(n&1)///n is odd;TEM = MUL (TEM, p);///multiply matrix E and matrix A;N/=2; P=Mul (P, p); } returntem;}intMain () {s.a[0][0] =1; s.a[0][1] =1; s.a[1][0] =1; s.a[1][1] =0; intN; while(SCANF ("%d", &n), n!=-1) {e=Pow (s, N); printf ("%d\n", e.a[0][1]); } return 0;}
View Code
Compare to think fast power;
Fibonacci----poj3070 (Matrix fast Power, template)