2017ACM/ICPC广西邀请赛-重现赛 1004.Covering

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和2×1的矩形填满N×4的方格,求方案数
解题报告:打表发现规律:\(f_n=f_{n-1}+5*f_{n-2}+f_{n-3}-f_{n-4}\)
然后矩阵快速幂求解即可:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=5,mod=1000000007;
struct mat{
    ll a[N][N];
    mat(){memset(a,0,sizeof(a));}
  mat operator *(const mat p)const{
        mat tmp;
        for(int i=1;i<N;i++)
            for(int j=1;j<N;j++)
                for(int k=1;k<N;k++){
                    tmp.a[i][j]+=(a[i][k]*p.a[k][j])%mod;
                    tmp.a[i][j]=((tmp.a[i][j]%mod)+mod)%mod;
                }
        return tmp;
    }
};
ll n;int f[5];
void work()
{
    if(n<=4){
        printf("%d\n",f[n]);
        return ;
    }
    mat S,T;
    for(int i=1;i<=4;i++)S.a[1][i]=f[5-i];
    T.a[1][1]=1;T.a[2][1]=5;T.a[3][1]=1;T.a[4][1]=-1;
    for(int i=2;i<=4;i++)T.a[i-1][i]=1;
    n-=4;
    while(n){
        if(n&1)S=S*T;
        T=T*T;n>>=1;
    }
    printf("%lld\n",S.a[1][1]);
}

int main()
{
    f[1]=1;f[2]=5;f[3]=11;f[4]=36;
    while(~scanf("%lld",&n))work();
    return 0;
}

转载于:https://www.cnblogs.com/Yuzao/p/7459207.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值