The meaning is to use matrix multiplication to find the last four digits of the nth of the Fibonacci series. If the last four digits are all 0, the output is 0. Otherwise
Remove the leading 0 from the last four digits of the output... ... Yes... ... Output FN % 10000.
The question is so clear .. I am still trying to find the last four digits of % and/and determine whether all the values are 0 and whether the output values are 0.
Remove the leading 0. O (Clerk □clerk) O
In addition, when the power of the matrix's fast power is 0, it must be judged.
P.s: This afternoon I want to learn about Matrix Multiplication. This is my first question about matrix multiplication!
#include<cstdio>#include<iostream>#include<cstring>#define maxn 5using namespace std;const int mod = 10000;int n,q;struct Mat{ int mp[maxn][maxn]; bool operator = (Mat a) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) mp[i][j] = a.mp[i][j]; } }};Mat operator *(Mat a,Mat b){ Mat c; memset(c.mp,0,sizeof(c.mp)); for(int k=0;k<n;k++) { for(int i=0;i<n;i++) { if(a.mp[i][k]<=0) continue; for(int j=0;j<n;j++) { if(b.mp[k][j]<=0) continue; c.mp[i][j] = c.mp[i][j]+(a.mp[i][k]*b.mp[k][j])%mod; if(c.mp[i][j]>mod) c.mp[i][j]-=mod; } } } return c;}Mat operator ^(Mat a,int k){ Mat c; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) c.mp[i][j] = (i==j); } while(k) { if(k&1) c = c*a; a =a*a; k>>=1; } return c;}int main(){ while(scanf("%d",&q)!=EOF) { if(q == -1) break; Mat x; x.mp[0][0] = 0; x.mp[0][1] = 1; x.mp[1][0] = 1; x.mp[1][1] = 1; Mat y; y.mp[0][0] = 0; y.mp[1][0] = 1; Mat z; n = 2; if(!q) { printf("0\n"); continue; } z = x^(q-1); Mat tmp = z*y; printf("%d\n",tmp.mp[1][0]%10000); } return 0;}