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;
}

猜你喜欢

转载自blog.csdn.net/feather2016/article/details/79941649