ICPC2017南宁邀请赛1005&&HDU6185 (矩阵快速幂/黑科技

Covering

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≤10^18

Output

For each test cases, output the answer mod 1000000007 in a line.

Sample Input

1
2

Sample Output

1
5

题意

递推推经典题目,首先可以暴力DFS跑出前10项 发现果然是线性关系,直接黑科技模板就可以。
当然也可以用高斯消元把公式搞出来,或者直接推

递推公式为
f(n) = f(n-1)+5*f(n-2)+f(n-3)-f(n-4)
直接矩阵快速幂 注意因为有个负号

AC代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXM = 1e3+10;
const int MAXN = 1e6+10;
const int mod = 1e9+7;
LL n;
struct node
{
    LL arr[4][4];
};
node mul(node x, node y)
{
    node ans;
    CLR(ans.arr,0);
    for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 4; j++) {
            for(int k = 0; k < 4; k++) {
                ans.arr[i][j] = (ans.arr[i][j]+(x.arr[i][k]*y.arr[k][j]+mod)%mod)%mod;
            }
        }
    }
    return ans;
}

node powMod(LL u, node x)
{
    node ans;
    CLR(ans.arr,0);
    for(int i = 0;i < 4; i++) {
        for(int j = 0; j < 4; j++) if(i == j) ans.arr[i][j] = 1;
    }
    while(u) {
        if(u&1) ans = mul(ans,x);
        x = mul(x,x);
        u >>= 1;
    }
    return ans;
}

int main()
{
    while(~scanf("%lld",&n)) {
        node res;
        CLR(res.arr,0);
        res.arr[0][0] = 1; res.arr[0][1] = 5; res.arr[0][2] = 1; res.arr[0][3] = -1;
        res.arr[1][0] = 1; res.arr[2][1] = 1; res.arr[3][2] = 1;
        if(n == 1)  puts("1");
        else if(n == 2) puts("5");
        else if(n == 3) puts("11");
        else if(n == 4) puts("36");
        else {
            node xx = powMod(n-4,res);
            LL yy = ((xx.arr[0][0]*36)%mod+(xx.arr[0][1]*11)%mod+(xx.arr[0][2]*5)%mod+(xx.arr[0][3]*1)%mod)%mod;
            printf("%lld\n",yy);
        }
    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值