HDU 2065

    一开始以为递推公式是f[n]=2*f[n-1]+2*f[n-2]。但这个公式显然是错的,这个公式相当于限制了A,C必须成对出现,比如ABA这种就计算不到了。

    到网上搜了一下才发现原来公式是这样的,设f1[n]为长度为n的序列,A,C都出现偶数次的方法数;f2[n]为长度为n的序列,A出现奇数次,C出现偶数次的方法数;f3[n]为长度为n的序列,A出现偶数次,C出现奇数次的方法数;f4[n]为长度为n的序列,A,C都出现奇数次的方法数。
    f1[n] = 2*f1[n-1] + f2[n-1] + f3[n-1];
    f2[n] = f1[n-1] + 2*f2[n-1] + f4[n-1];
    f3[n] = f1[n-1] + 2*f3[n-1] + f4[n-1];
    f4[n] = f2[n-1] + f3[n-1] + 2*f4[n-1];
使用矩阵递推即可。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>
#include <sstream>

#define ll __int64

using namespace std;

const int mat1_[4][4] = {
    {1, 0, 0, 0},
    {0, 1, 0, 0},
    {0, 0, 1, 0},
    {0, 0, 0, 1}
};
const int mat2_[4][4] = {
    {2, 1, 1, 0},
    {1, 2, 0, 1},
    {1, 0, 2, 1},
    {0, 1, 1, 2}
};
int mat1[4][4], mat2[4][4], mat3[4][4];

void mul(int mat1[4][4], int mat2[4][4]) {
    int i, j, k;
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            mat3[i][j] = 0;
            for (k = 0; k < 4; k++)
                mat3[i][j] += mat1[i][k] * mat2[k][j];
            mat3[i][j] %= 100;
        }
    }
    memcpy(mat1, mat3, sizeof(mat3));
}

int main() {
    int t, c;
    ll n;
    while (scanf("%d", &t), t) {
        for (c = 1; c <= t; c++) {
            scanf("%I64d", &n);
            printf("Case %d: ", c);
            memcpy(mat1, mat1_, sizeof(mat1));
            memcpy(mat2, mat2_, sizeof(mat2));
            while (n) {
                if (n & 1) mul(mat1, mat2);
                mul(mat2, mat2);
                n >>= 1;
            }
            printf("%d\n", mat1[0][0]);
        }
        printf("\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值