LightOJ 1244 - Tiles 猜递推+矩阵快速幂

http://www.lightoj.com/volume_showproblem.php?problem=1244

 

题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法。\(N <= 10^9 \)

思路:首先手写出前几项,猜出递推式,如果真有比赛出这种题,又不能上网进工具站查是吧?N比较大显然用矩阵快速幂优化一下

 

 

 

/** @Date    : 2016-12-18-22.44
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version :
  */
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

const LL mod = 1e4 + 7;
struct matrix
{
    LL mt[3][3];
    void init()
    {
        for(int i = 0; i < 3; i++)
            for(int j = 0; j < 3; j++)
                mt[i][j] = 0;
    }
    void cig()
    {
        for(int i = 0; i < 3; i++)
            mt[i][i] = 1;
    }
};

matrix mul(matrix a, matrix b)
{
    matrix c;
    c.init();
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            for(int k = 0; k < 3; k++)
            {
                c.mt[i][j] += a.mt[i][k] * b.mt[k][j];
                c.mt[i][j] %= mod;
            }
        }
    }
    return c;
}

matrix fpow(matrix a, LL n)
{
    matrix r;
    r.init();
    r.cig();
    while(n > 0)
    {
        if(n & 1)
            r = mul(r, a);
        a = mul(a, a);
        n >>= 1;
    }
    return r;
}

LL fun(LL n)
{
    if(n < 3)
    {
        return n;
    }
    matrix base;
    base.init();
    base.mt[0][0] = 2;
    base.mt[0][2] = 1;
    base.mt[1][0] = 1;
    base.mt[2][1] = 1;
    base = fpow(base, n - 3);
    return (base.mt[0][0] * 5 + base.mt[0][1] * 2 + base.mt[0][2]) % mod;

}
int main()
{
    int T;
    int cnt = 0;
    cin >> T;
    while(T--)
    {
        LL n;
        scanf("%lld", &n);
        LL ans = fun(n);
        printf("Case %d: %lld\n", ++cnt, ans);
    }
    return 0;
}


posted @ 2016-12-25 15:18  Lweleth  阅读(208)  评论(0编辑  收藏  举报