Covering (矩阵快速幂)

Covering

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

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
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

Recommend
liuyiding | We have carefully selected several similar problems for you: 6263 6262 6261 6260 6259

题意很好理解,需要用到递推,推荐博客:很好地博客

然后需要注意,取模!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long ll;
ll u;
int n;
const ll MOD = 1000000007;
struct mat
{
    ll m[10][10];
}init, q;
ll mod(ll x)
{
    return (x+MOD)%MOD;//可能有负数,先加再取模
}
void init_mat()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            init.m[i][j] = 0;
            q.m[i][j] = 0;
        }
    }
    init.m[0][0] = 1;
    init.m[0][1] = 5;
    init.m[0][2] = 1;
    init.m[0][3] = -1;
    init.m[1][0] = 1;
    init.m[2][1] = 1;
    init.m[3][2] = 1;
    q.m[0][0] = 36;
    q.m[1][0] = 11;
    q.m[2][0] = 5;
    q.m[3][0] = 1;
}

mat operator *(mat a, mat b)
{
    mat ret;
    ll x;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            x = 0;
            for(int k=0;k<n;k++)
            {
                x += mod((ll)(a.m[i][k]*b.m[k][j]));
                x %=MOD;//这里要取模
            }
            ret.m[i][j] = x;
        }
    }
    return ret;
}

mat mat_pow(mat a, ll x)
{
    mat ret;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(j==i)
                ret.m[i][j] = 1;
            else
                ret.m[i][j] = 0;
        }
    }
    while(x)
    {
        if(x&1)
            ret = ret * a;
        a = a * a;
        x>>=1;
    }
    return ret;
}
int main()
{
    n = 4;
    while(~scanf("%lld", &u))
    {
        if(u==1)
            cout<<1<<endl;
        else if(u==2)
            cout<<5<<endl;
        else if(u==3)
            cout<<11<<endl;
        else if(u==4)
            cout<<36<<endl;
        else
        {
            init_mat();
            mat a = mat_pow(init, u-4);
            a = a * q;
            printf("%lld\n", mod((ll)a.m[0][0]));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值