Covering

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 187    Accepted Submission(s): 107

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
 
Source
 
打表
/*
* @Author: Administrator
* @Date: 2017-08-31 17:40:04
* @Last Modified by: Administrator
* @Last Modified time: 2017-09-01 11:03:00
*/
/*
题意:给你一个4*n的矩阵,然后让你用1*2和2*1的木块放,问你完美覆盖的
方案数 思路:状压DP找规律
*/ #include <bits/stdc++.h> #define MAXN 100
#define MAXM 20
#define MAXK 15
using namespace std; int dp[MAXN][MAXM];//dp[i][j]表示前ihang
int n; inline bool ok(int x){
//判断是不是有连续个1的个数是奇数
int res=;
while(x){
if(x%==){
res++;
}else{
if(res%==) return false;
else res=;
}
x/=;
}
if(res%==) return false;
else return true;
} inline void init(){
memset(dp,,sizeof dp);
} int main(){
freopen("in.txt","r",stdin);
for(int n=;n<=;n++){
init();
for(int i=;i<=MAXK;i++){//初始化第一行的没种状态
if(ok(i)==true)
dp[][i]=;
}
for(int i=;i<n;i++){
for(int j=;j<=MAXK;j++){
if(dp[i][j]!=){
for(int k=;k<=MAXK;k++){
if( (j|k)==MAXK && ok(j&k) )
///j|k==tot-1的话就是能拼起来组成
dp[i+][k]+=dp[i][j];
}
}
}
}
printf("%d\n",dp[n][MAXK]);
}
return ;
}
/*
* @Author: Administrator
* @Date: 2017-09-01 11:17:37
* @Last Modified by: Administrator
* @Last Modified time: 2017-09-01 11:28:09
*/
#include <bits/stdc++.h> #define MAXN 5
#define mod 1000000007
#define LL long long using namespace std; /********************************矩阵快速幂**********************************/
class Matrix {
public:
LL a[MAXN][MAXN];
LL n; void init(LL x) {
memset(a,,sizeof(a));
if (x)
for (int i = ; i < MAXN ; i++)
a[i][i] = 1LL;
} Matrix operator +(Matrix b) {
Matrix c;
c.n = n;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
return c;
} Matrix operator +(LL x) {
Matrix c = *this;
for (int i = ; i < n; i++)
c.a[i][i] += x;
return c;
} Matrix operator *(Matrix b)
{
Matrix p;
p.n = b.n;
p.init();
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
for (int k = ; k < n; k++)
p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
return p;
} Matrix power(LL t) {
Matrix ans,p = *this;
ans.n = p.n;
ans.init();
while (t) {
if (t & )
ans=ans*p;
p = p*p;
t >>= ;
}
return ans;
}
}init,unit;
/********************************矩阵快速幂**********************************/ LL n; int main(){
// freopen("in.txt","r",stdin);
while(scanf("%lld",&n)!=EOF){
if(n<=){
switch(n){
case :
puts("");
break;
case :
puts("");
break;
case :
puts("");
break;
case :
puts("");
break;
}
continue;
}
init.init();
init.n=;
init.a[][]=;
init.a[][]=;
init.a[][]=;
init.a[][]=;
unit.init();
unit.n=;
unit.a[][]=;
unit.a[][]=;
unit.a[][]=;
unit.a[][]=-;
unit.a[][]=;
unit.a[][]=;
unit.a[][]=;
unit=unit.power(n-);
init=init*unit;
printf("%lld\n",(init.a[][]+mod)%mod);
}
return ;
}
05-11 02:52