【2017广西邀请赛】hdu 6185 Covering 矩阵快速幂
2017-09-07 08:28
411 查看
Problem Description
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?
Input
There are no more than 5000 test cases.
Each test case only contains one positive integer n in a line.
1≤n≤1018
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5
题意:
用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠。
思路:
骨牌覆盖的方法构造的矩阵太大,会超时。
手动推出递归式 f
=f[n-1]+5f[n-2]+f[n-3]-f[n-4]。然后再用矩阵快速幂即可。
推导过程:http://blog.csdn.net/elbadaernu/article/details/77825979
//
// main.cpp
// 1004
//
// Created by zc on 2017/9/6.
// Copyright © 2017年 zc. All rights reserved.
//
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll MOD=1000000007;
mat mul(mat &A,mat &B)
{
mat C(A.size(),vec(B[0].size()));
for(int i=0;i<A.size();i++)
for(int k=0;k<B.size();k++)
for(int j=0;j<B[0].size();j++)
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%MOD;
return C;
}
mat pow(mat A, ll n)
{
mat B(A.size(),vec(A.size()));
for(int i=0;i<A.size();i++) B[i][i]=1;
while(n>0)
{
if(n&1) B=mul(B,A);
A=mul(A,A);
n>>=1;
}
return B;
}
int ans[5]={0,1,5,11,36};
int main()
{
ll n;
mat A(4,vec(4));
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
A[i][j]=0;
A[0][0]=A[0][1]=A[1][2]=A[2][0]=A[2][3]=1;
A[1][0]=5;A[3][0]=-1;
while(~scanf("%lld",&n))
{
if(n<5)
{
printf("%d\n",ans
);
continue;
}
mat C=pow(A,n-4);
mat B(1,vec(4));
B[0][0]=36;B[0][1]=11;B[0][2]=5;B[0][3]=1;
B=mul(B,C);
printf("%lld\n",(B[0][0]+MOD)%MOD);
}
}
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?
Input
There are no more than 5000 test cases.
Each test case only contains one positive integer n in a line.
1≤n≤1018
Output
For each test cases, output the answer mod 1000000007 in a line.
Sample Input
1
2
Sample Output
1
5
题意:
用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠。
思路:
骨牌覆盖的方法构造的矩阵太大,会超时。
手动推出递归式 f
=f[n-1]+5f[n-2]+f[n-3]-f[n-4]。然后再用矩阵快速幂即可。
推导过程:http://blog.csdn.net/elbadaernu/article/details/77825979
//
// main.cpp
// 1004
//
// Created by zc on 2017/9/6.
// Copyright © 2017年 zc. All rights reserved.
//
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;
const ll MOD=1000000007;
mat mul(mat &A,mat &B)
{
mat C(A.size(),vec(B[0].size()));
for(int i=0;i<A.size();i++)
for(int k=0;k<B.size();k++)
for(int j=0;j<B[0].size();j++)
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%MOD;
return C;
}
mat pow(mat A, ll n)
{
mat B(A.size(),vec(A.size()));
for(int i=0;i<A.size();i++) B[i][i]=1;
while(n>0)
{
if(n&1) B=mul(B,A);
A=mul(A,A);
n>>=1;
}
return B;
}
int ans[5]={0,1,5,11,36};
int main()
{
ll n;
mat A(4,vec(4));
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
A[i][j]=0;
A[0][0]=A[0][1]=A[1][2]=A[2][0]=A[2][3]=1;
A[1][0]=5;A[3][0]=-1;
while(~scanf("%lld",&n))
{
if(n<5)
{
printf("%d\n",ans
);
continue;
}
mat C=pow(A,n-4);
mat B(1,vec(4));
B[0][0]=36;B[0][1]=11;B[0][2]=5;B[0][3]=1;
B=mul(B,C);
printf("%lld\n",(B[0][0]+MOD)%MOD);
}
}
相关文章推荐
- HDU 6185 && 2017广西邀请赛:Covering(矩阵快速幂)
- HDU 6185 Covering (2017 广西邀请赛重现赛)(矩阵快速幂)
- HDU-6185-Covering(广西邀请赛D题)(矩阵快速幂)
- 2017 ACM/ICPC 广西邀请赛||HDU 6185 Covering 【状压DP+矩阵快速幂】
- 【每日一题(9)】Duizi and Shunzi HDU - 6188 2017ACM/ICPC广西邀请赛
- 2017广西邀请赛 Covering(矩阵快速幂)
- 2017 广西邀请赛&& hdu 6191 Query on A Tree(字典树启发式合并)
- HDU 6191 2017广西邀请赛Query on A Tree:可持久化01字典树(区间抑或最大值查询)
- HDU 6186 && 2017广西邀请赛:CS Course
- 2017 icpc广西邀请赛 K.Query on A Tree (hdu 6191)可持久化字典树
- 【2017广西邀请赛】hdu 6182 A Math Problem
- HDU 6188 && 2017广西邀请赛:Duizi and Shunzi
- HDU 6191 && 2017广西邀请赛:Query on A Tree(字典树启发式合并)
- HDU 6184 && 2017广西邀请赛:Counting Stars(三元环)
- hdu Covering 2017广西邀请赛 矩阵快速幂
- HDU 6182 && 2017广西邀请赛:A Math Problem
- HDU 6184&& 2017广西邀请赛 Counting Stars(三元环计数)
- HDU-6186 CS Course (线段树)(2017ACM/ICPC广西邀请赛)
- HDU 6183 && 2017广西邀请赛:Color it(线段树)
- 【2017广西邀请赛】hdu 6188 Duizi and Shunzi 贪心