"红色病毒"问题

http://acm.hdu.edu.cn/showproblem.php?pid=2065

矩阵乘法推出递推公式。

View Code
Matrix tmp(4,4);

void get_init() {
    int i,j,k;
    for(i=0;i<4;i++) for(j=0;j<4;j++) tmp(i,j)=1;
    for(i=0;i<4;i++) tmp(i,i)=2, tmp(i,3-i)=0;
}
void solve() {
    int i,j,k;
    Matrix ans(1,4), tt=tmp;
    ans(0,0)=ans(0,3)=1;
    ans(0,1)=0;ans(0,2)=2;
    scanf("%I64d",&N);
    tt=tt^(N-1);
    ans=ans*tt;
    printcase();
    printf("%d\n",ans(0,2));
}

int main() {
    int ca;
    get_init();
    while(scanf("%d",&ca), ca) {
        g=1; while(ca--) solve(); printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhang1107/p/3015419.html