http://acm.hdu.edu.cn/showproblem.php?pid=6185
题目描述
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?
输入
There are no more than 5000 test cases.
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
题意:
用没有数量限制2×1和1×2的矩形去铺一块4×n的矩形,有多少种方法。
但是可以观察到此题的数据范围很大,达到了1e18,我们自然而然的想到应该要找规律,可能要用到矩阵快速幂
猜测是线性递推方程,根据其他的公式得出n<=10情况下的方案数
1 1
2 5
3 11
4 36
5 95
6 281
7 781
8 2245
9 6336
10 18061
根据这十个数利用高斯消元,先假设是f(n)=f(n-1)*a+f(n-2)*b
如果高斯消元得到的结果不是整数接着试
最终f(n)=f(n-1)*a+f(n-2)*b+f(n-3)*c+f(n-4)*d
//f(n)=f(n-1)*a+f(n-2)*b+f(n-3)*c+f(n-4)*d情况下的高斯消元
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=105;
typedef double Matrix[maxn][maxn];
Matrix A,S;
//n是方程的个数
void gauss(Matrix A,int n)
{
int i,j,k,r;
for(int i=0; ifor( j=i+1; jif(fabs(A[j][i])>fabs(A[r][i]))r=j;
if(r!=i)
for(j=0; j<=n; j++)swap(A[r][j],A[i][j]);
for(k=i+1; kdouble f=A[k][i]/A[i][i];
for(j=i; j<=n; j++)
A[k][j]-=f*A[i][j];
}
}
for(i=n-1; i>=0; i--)
{
for(j=i+1; jint main()
{
memset(A,0,sizeof(A));
A[0][0]=95,A[0][1]=36,A[0][2]=11,A[0][3]=5,A[1][0]=6336,A[1][1]=2245,A[1][2]=781;
A[1][3]=281,A[2][0]=781,A[2][1]=281,A[2][2]=95,A[2][3]=36,A[3][0]=2245,A[3][1]=781;
A[3][2]=281,A[3][3]=95,A[0][4]=281,A[1][4]=18061,A[2][4]=2245,A[3][4]=6336;
gauss(A,4);
for(int i=0; i<4; i++)
{
printf("%8.2f\n",A[i][4]);
}
return 0;
}
运行得到结果
1.00
5.00
1.00
-1.00
所以f(n)=f(n-1)+f(n-2)*5+f(n-3)-f(n-4)
然后根据递推公式用矩阵快速幂解决
值得注意的是由于矩阵系数有负数,在做矩阵相乘时注意+mod再取膜
#include
#include
#include
#include
#define ll long long
using namespace std;
ll mod=1000000007;
struct Matrix
{
ll a[4][4];
};
Matrix multi(Matrix x,Matrix y)
{
Matrix ans;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
{
ans.a[i][j]=0;
for(int k=0; k<4; k++)
ans.a[i][j]=(ans.a[i][j]+x.a[i][k]*y.a[k][j]+mod)%mod;
}
return ans;
}
Matrix mqmod(ll l)
{
Matrix p,q;
memset(p.a,0,sizeof(p.a));
p.a[0][0]=p.a[0][2]=p.a[1][0]=p.a[2][1]=p.a[3][2]=1;
p.a[0][1]=5;p.a[0][3]=-1;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
q.a[i][j]=(i==j);
while(l)
{
if(l&1)q=multi(q,p);
p=multi(p,p);
l>>=1;
}
return q;
}
int main()
{
ll n;
while(~scanf("%I64d",&n))
{
if(n==1)
{
printf("1\n");
continue;
}
else if(n==2)
{
printf("5\n");
continue;
}
else if(n==3)
{
printf("11\n");
continue;
}
else if(n==4)
{
printf("36\n");
continue;
}
else
{
ll num=0;
Matrix ans;
ans=mqmod(n-4);
num=(ans.a[0][0]*36+ans.a[0][1]*11+ans.a[0][2]*5+ans.a[0][3])%mod;
num=(num+mod)%mod;
printf("%I64d\n",num);
}
}
return 0;
}