[Matrix Multiplication] [SSL 1530] Pebolaci Sequence III
topic
Find the sequence f [n] = f [n − 1] + f [n − 2] + 1 f[n]=f[n-1]+f[n-2]+1f[n]=f[n−1]+f[n−2]+1 The first ofNNN items.fff[1]=1, f f f[2]=1.
enter
n n n (1 < 4n $ < 2 ^ 31-1)
Output
The first N NN item resultmod modmod 9973
Sample
input
12345
output
8932
Problem-solving ideas
This problem and this problem like
to pay more 1
to answer matrix plus a put a
high-risk matrix becomes ↓ to
Code
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int mo=9973;
long long n;
struct lzf{
int n,m;
long long h[5][5];
}a,b,x;
lzf operator *(lzf l,lzf y)
{
x.n=l.n,x.m=y.m;
memset(x.h,0,sizeof(x.h));
for (int k=1;k<=l.m;k++)
for (int i=1;i<=x.n;i++)
for (int j=1;j<=x.m;j++)
x.h[i][j]=(x.h[i][j]+l.h[i][k]*y.h[k][j]%mo)%mo;
return x;
}
void power(long long n)
{
if (n & 1) a=a*b;
if (n==1) return;
b=b*b;
power(n/2);
}
int main()
{
a.n=1,a.m=3;
a.h[1][1]=a.h[1][2]=a.h[1][3]=1;
b.n=3,b.m=3;
b.h[1][2]=b.h[2][1]=b.h[2][2]=b.h[3][2]=b.h[3][3]=1;
scanf("%lld",&n);
power(n-1);
printf("%lld",a.h[1][1]);
return 0;
}