很简单的题目,将嵌套循环先找到三个循环节,然后快速幂。
ACcode:
#include<cstdio>
#include<cstring>
typedef long long LL;
const int MOD1=183120;
const int MOD2=222222224;
const int MOD3=1e9+7;
LL n;
LL fun(LL mod)
{
LL a=0,b=1,c=-1;
LL cnt=1;
while (1)
{
c=(3*b+a)%mod;
if (c==1&&b==0) break;
cnt++;
a=b,b=c;
}
return cnt;
}
int qp(LL y,int mod)
{
LL t[2][2];
LL a[2][2]={3,1,1,0};
LL b[2][2]={1,0,1,0};
while (y)
{
if (y&1)
{
for (int i=0;i<2;i++)
for (int j=0;j<2;j++)
{
t[i][j]=0;
for (int k=0;k<2;k++)
t[i][j]=(t[i][j]+b[i][k]*a[k][j])%mod;
}
for (int i=0;i<2;i++)
for (int j=0;j<2;j++)
b[i][j]=t[i][j];
}
y>>=1;
for (int i=0;i<2;i++)
for (int j=0;j<2;j++)
{
t[i][j]=0;
for (int k=0;k<2;k++)
t[i][j]=(t[i][j]+a[i][k]*a[k][j])%mod;
}
for (int i=0;i<2;i++)
for (int j=0;j<2;j++)
a[i][j]=t[i][j];
}
return b[1][1];
}
int main()
{
while (~scanf("%I64d",&n))
{
n=qp(n,MOD1);
n=qp(n,MOD2);
n=qp(n,MOD3);
printf("%I64d\n",n);
}
return 0;
}