HDU 3893

来源:互联网 发布:淘宝开店虚拟充值类目 编辑:程序博客网 时间:2024/06/06 09:29

题意:给一串长度为N的小方格上色,一共有六种颜色分别是 red, orange, yellow, green, blue, violet。要求 1.左右两边颜色对称 2.相邻的小方格颜色不同 3.连续的六个小方格的颜色不能依次是 red, orange, yellow, green, blue, violet,求一共有多少种上色方法。(N<=10^9)

解法:
    如果N为偶数,不可能满足条件。因为如果N为偶数,要满足第一个条件,中间两个格子颜色必然相同,这会导致不能满足第二个条件。
    如果N为奇数,我们只需要考虑前面(N+1)/2个格子的着色方法数,后面的格子因为要和前面的格子对称,所以不需要考虑。
    我们假设六种颜色代号分别是 012345,因为有第三个条件,所以前面(N+1)/2个格子颜色不能出现 012345 序列,由于对称也不能出现 543210 序列。
定12个状态,分别为 [],[0],[5],[01],[54],[012],[543],[0123],[5432],[01234],[54321],[------]。
状态转移矩阵为:
    3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 3, 0,
    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
    1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0,
    0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 5

#include <stdio.h>#include <memory.h>const __int64 mod = 112233;__int64 init_mtx[12][12] = {    3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 3, 0,    1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,    1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0,    0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 5};__int64 init_cunt[12] = {    4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};__int64 mtx1[12][12], mtx2[12][12], mtx3[12][12];void mul(__int64 mtxa[12][12], __int64 mtxb[12][12]) {    int i, j, k;    for (i = 0; i < 12; i++) {        for (j = 0; j < 12; j++) {            mtx3[i][j] = 0;            for (k = 0; k < 12; k++)                mtx3[i][j] += mtxa[i][k] * mtxb[k][j];            mtx3[i][j] %= mod;        }    }    memcpy(mtxa, mtx3, sizeof(mtx3));}void solve(int m) {    int i, j;    memset(mtx1, 0, sizeof(mtx1));    for (i = 0; i < 12; i++)        mtx1[i][i] = 1;    memcpy(mtx2, init_mtx, sizeof(mtx2));        while (m) {        if (m & 1)            mul(mtx1, mtx2);        mul(mtx2, mtx2);        m >>= 1;    }    long long ans = 0;    for (i = 0; i < 11; i++)        for (j = 0; j < 11; j++)            ans += mtx1[i][j] * init_cunt[j];    printf("%I64d\n", ans % mod);}int main() {    int n;    while (scanf("%d", &n) != EOF) {        if (n % 2 == 0) printf("0\n");        else solve(n / 2);    }    return 0;}

原创粉丝点击