题目:
我是超链接
题解:
#include <cstdio>
#include <cstring>
#define LL long long
using namespace std;
const LL Mod=1e9+7;
LL fib[5][5];
struct hh
{
LL cr[5][5];
}a;
hh work(hh a,hh b)
{
hh c;
memset(c.cr,0,sizeof(c.cr));
for (int i=1;i<=2;i++)
for (int j=1;j<=2;j++)
for (int k=1;k<=2;k++)
c.cr[i][j]=(c.cr[i][j]+a.cr[i][k]*b.cr[k][j]%Mod)%Mod;
return c;
}
hh kum(hh x,LL p)
{
hh ans=a;
for (;p;p>>=1,x=work(x,x))
if (p&1) ans=work(ans,x);
return ans;
}
int main()
{
int i,j,k;LL n;
while (~scanf("%I64d",&n) && n!=0)
{
if (n==1 || n==2)
{
printf("1\n"); continue;
}
a.cr[1][1]=1; a.cr[1][2]=1; a.cr[2][1]=1; a.cr[2][2]=0;
LL c[5][5],fib[5][5];
memset(c,0,sizeof(c));
if (n>3) a=kum(a,n-3);
fib[1][1]=fib[2][1]=1;
for (i=1;i<=2;i++)
for (j=1;j<=1;j++)
for (k=1;k<=2;k++)
c[i][j]=(c[i][j]+a.cr[i][k]*fib[k][j]%Mod)%Mod;
printf("%I64d\n",c[1][1]);
}
}