# SSLOJ1531 斐波拉契数列IV

Description

Input

N(１＜Ｎ＜2^31-1)

Output

fn-1                      1 1 1 1                                 fn

fn-2                      1 0 0 0                                 fn-1

n              *           0 0 1 1              =                 n+1

1                          0 0 0 1                                  1

Problem Id:1531  User Id:BPM136
Memory:1380K  Time:0MS
Language:G++  Result:Accepted

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
</del>{
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
#define N 4
#define inf 9973
struct matrix
{
int a[N][N];<del>
</del>	void clear()
{
memset(a,0,sizeof(a));
}
matrix operator*(const matrix b)
{
matrix anss;
fo(i,0,N-1)
fo(j,0,N-1)
{
anss.a[i][j]=0;
fo(k,0,N-1)
{
anss.a[i][j]+=a[i][k]*b.a[k][j];
anss.a[i][j]%=inf;
}
}
return anss;
}
};
matrix I=
{
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
matrix A=
{
1,1,1,1,
1,0,0,0,
0,0,1,1,
0,0,0,1
};
int n,m;

matrix KSM(matrix a,int b)
{
if(b==0)return I;
if(b==1)return A;
matrix ret=KSM(a,b/2);
ret=ret*ret;
if(b%2)ret=ret*a;
return ret;
}

int main()
{
matrix ans;
ans.a[0][0]=1;ans.a[1][0]=1;ans.a[2][0]=3;ans.a[3][0]=1;
ans=KSM(A,n-3)*ans;
LL anss=0;
fo(i,0,N-1)anss+=ans.a[i][0];
anss%=inf;cout<<anss<<endl;
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120