提交: 46 解决: 8
[ 提交][ 状态][ 讨论版]
题目描述
Bob’s school has a big playground, boys and girls always play games here after school.
To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.
Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.
He has infinite carpets with sizes of 1 × 2 and 2 × 1, and the size of the playground is 4 × n.
Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?
To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets.
Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more.
He has infinite carpets with sizes of 1 × 2 and 2 × 1, and the size of the playground is 4 × n.
Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping?
输入
There are no more than 5000 test cases.
Each test case only contains one positive integer n(1 ≤ n ≤ 1018) in a line.
Each test case only contains one positive integer n(1 ≤ n ≤ 1018) in a line.
输出
For each test cases, output the answer mod 1000000007 in a line.
样例输入
1
2
样例输出
1
5
提示
来源
找规律,快速幂(推导过程请看http://blog.youkuaiyun.com/elbadaernu/article/details/77825979)
今天终于发现我之前的矩阵快速幂中的矩阵相乘,不太准确,导致我一直错,以后存这个模本
代码如下:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
typedef long long ll;
struct Matrix{
ll matrix[5][5];
};
int n;//矩阵的阶数
const int mod=1000000007;
void init_matrix(Matrix &mx)
{
memset(mx.matrix,0,sizeof(mx.matrix));
mx.matrix[0][0]=1;
mx.matrix[0][1]=5;
mx.matrix[0][2]=1;
mx.matrix[0][3]=-1;
mx.matrix[1][0]=1;
mx.matrix[2][1]=1;
mx.matrix[3][2]=1;
}
void init(Matrix &res)
{
memset(res.matrix,0,sizeof(res.matrix));
for(int i=0;i<=n;i++)
res.matrix[i][i]=1;
}
Matrix multiplicative(Matrix a,Matrix b)
{
Matrix res;
memset(res.matrix, 0, sizeof(res.matrix));
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(a.matrix[i][j] == 0)
continue;
for(int k = 0; k < 4; k++)
{
if(b.matrix[j][k] == 0)
continue;
res.matrix[i][k] += a.matrix[i][j] * b.matrix[j][k];
res.matrix[i][k]=(res.matrix[i][k]%mod+mod)%mod;
}
}
}
return res;
}
Matrix pow(Matrix mx,ll m)
{
Matrix res,base=mx;
init(res);
while(m)
{
if(m&1)
res=multiplicative(res,base);
base=multiplicative(base,base);
m>>=1;
}
return res;
}
void output(Matrix &mx)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<mx.matrix[i][j]<<" ";
cout<<endl;
}
}
int main()
{
ll m;
while(~scanf("%lld",&m))
{
if(m==1)
printf("1\n");
else if(m==2)
printf("5\n");
else if(m==3)
printf("11\n");
else if(m==4)
printf("36\n");
else
{
Matrix mx,req;
n=4;
init_matrix(mx);
//output(mx);
req=pow(mx,m-4);
//output(req);
ll res=((req.matrix[0][0]*36)%mod+(req.matrix[0][1]*11)%mod
+(req.matrix[0][2]*5)%mod+(req.matrix[0][3]*1)%mod)%mod;
printf("%lld\n",res);
}
}
return 0;
}