Meaning of the questions:
Seeking Fibonacci of n items F [n] of Number film 10000 is f [n]% 10000
but 0 ≤ n ≤ 1,000,000,000
gave a formula:
analysis:
FIG matrix Fast Power n-1 th power of the matrix A 1 is three, the first row numbers in the first column is the answer.
(Remember modulo)
The following is the code board and ac
Quick copied to the matrix board power:
const int maxm=1e3+5;
struct Matrix{
int a[maxm][maxm];
}res,ans;
Matrix mul(Matrix A,Matrix B,int n){//矩阵A,B和阶数n
Matrix temp;//临时存放答案的矩阵
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
temp.a[i][j]=0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
temp.a[i][j]+=(A.a[i][k]%mod)*(B.a[k][j]%mod);
temp.a[i][j]%=mod;
}
}
}
return temp;
}
void quickpower(int N,int n){//N是幂数,n是阶数
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
ans.a[i][j]=(i==j?1:0);
}
}
while(N>0){
if(N&1){
ans=mul(ans,res,n);
}
res=mul(res,res,n);
N>>=1;
}
}
code:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxm=3;
const int mod=1e4;
struct Matrix{
int a[maxm][maxm];
}res,ans;
Matrix mul(Matrix A,Matrix B,int n){
Matrix temp;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
temp.a[i][j]=0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
temp.a[i][j]+=(A.a[i][k]%mod)*(B.a[k][j]%mod);
temp.a[i][j]%=mod;
}
}
}
return temp;
}
void quickpower(int N,int n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
ans.a[i][j]=(i==j?1:0);
}
}
while(N>0){
if(N&1){
ans=mul(ans,res,n);
}
res=mul(res,res,n);
N>>=1;
}
}
int main(){
int n;
while(cin>>n){
if(n==-1)break;
if(n==0){
cout<<0<<endl;
continue;
}
if(n==-1)break;
res.a[1][1]=res.a[1][2]=res.a[2][1]=1;
res.a[2][2]=0;
quickpower(n-1,2);
cout<<ans.a[1][1]<<endl;
}
return 0;
}