poj 3233 Matrix Power Series

Matrix Power Series

思路

题意比较简单,就是要求 S ( n ) = ∑ i = 1 n A i S(n) = \sum _{i = 1} ^{n} A^ {i} S(n)=i=1nAi,显然有 S ( n ) = S ( n − 1 ) ∗ A + A S(n) = S(n - 1) * A + A S(n)=S(n1)A+A,看到这里,那就简单了,递推式,加矩阵,矩阵快速幂无疑了嘛,所以我们开始构造矩阵。

显然有如下矩阵, E E E是单位矩阵, A A A是输入矩阵, O O O是零矩阵。

[ E E O A ] ∗ [ O O A O ] \begin{bmatrix} E & E \\ O & A\end{bmatrix} * \begin{bmatrix} O & O\\ A & O \end{bmatrix} [EOEA][OAOO]

通过这个矩阵的递推,我们就可以通过快速幂,达到快速求解的目的。

我严重怀疑这道题目数据有问题, l o n g   l o n g long\ long long long w a wa wa,然后 i n t int int就过了???

AC代码

/*
  Author : lifehappy
*/
// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #include <bits/stdc++.h>

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <cmath>


#define mp make_pair
#define pb push_back
#define endl '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
    ll f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-')    f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f * x;
}

void print(ll x) {
    if(x < 10) {
        putchar(x + 48);
        return ;
    }
    print(x / 10);
    putchar(x % 10 + 48);
}

const int N = 70;

int n, k, mod;

struct matrix {
    int a[N][N];
    matrix operator * (const matrix & t) const {
        matrix temp;
        for(int i = 1; i <= 2 * n; i++) {
            for(int j = 1; j <= 2 * n; j++) {
                temp.a[i][j] = 0;
                for(int k = 1; k <= 2 * n; k++) {
                    temp.a[i][j] = (temp.a[i][j] + a[i][k] * t.a[k][j]) % mod;
                }
            }
        }
        return temp;
    }
}E, A, O;

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    n = read(), k = read(), mod = read();
    matrix fat, ans;
    for(int i = 1; i <= 2 * n; i++) {//先置零,
        for(int j = 1; j <= 2 * n; j++) {
            fat.a[i][j] = ans.a[i][j] = 0;
        }
    }
    for(int i = 1; i <= n; i++) {//读入的时候置A加上置E矩阵。
        for(int j = 1; j <= n; j++) {
            fat.a[i + n][j + n] = ans.a[i + n][j] = read();
        }
        fat.a[i][i] = fat.a[i][i + n] = 1;
    }
    while(k) {
        if(k & 1) ans = fat * ans;
        fat = fat * fat;
        k >>= 1;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            printf("%d%c", ans.a[i][j], j == n ? '\n' : ' ');
        }
    }
	return 0;
}

调不出来的代码

写了一手逼格高一点的举证套矩阵的重载操作符的写法,可是太菜了,调不出来

/*
  Author : lifehappy
*/
// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #include <bits/stdc++.h>

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <cmath>


#define mp make_pair
#define pb push_back
#define endl '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
    ll f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-')    f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f * x;
}

void print(ll x) {
    if(x < 10) {
        putchar(x + 48);
        return ;
    }
    print(x / 10);
    putchar(x % 10 + 48);
}

const int N = 70;

int n, k, mod;

struct matrix {
    int a[N][N];
    matrix operator * (const matrix & t) const {
        matrix temp;
        for(int i = 1; i <= 2 * n; i++) {
            for(int j = 1; j <= 2 * n; j++) {
                temp.a[i][j] = 0;
                for(int k = 1; k <= 2 * n; k++) {
                    temp.a[i][j] = (temp.a[i][j] + a[i][k] * t.a[k][j]) % mod;
                }
            }
        }
        return temp;
    }

    matrix operator + (const matrix & t) const {
        matrix temp;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                temp.a[i][j] = (a[i][j] + t.a[i][j]) % mod;
            }
        }
        return temp;
    }
}E, A, O;

struct Matrix {
    matrix a[3][3];
    Matrix operator * (const Matrix & t) const {
        Matrix temp;
        for(int i = 1; i <= 2; i++) {
            for(int j = 1; j <= 2; j++) {
                temp.a[i][j] = O;
                for(int k = 1; k <= 2; k++) {
                    temp.a[i][j] = (a[i][k] * t.a[k][j]) + temp.a[i][j];
                }
            }
        }
    }
};

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    n = read(), k = read(), mod = read();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            A.a[i][j] = read();
            E.a[i][j] = O.a[i][j] = 0;
        }
        E.a[i][i] = 1;
    }
    Matrix fat, ans;
    fat.a[1][1] = E, fat.a[1][2] = E, fat.a[2][1] = O, fat.a[2][2] = A;
    ans.a[1][1] = O, ans.a[1][2] = O, ans.a[2][1] = A, ans.a[2][2] = O;
    while(k) {
        if(k & 1) ans = ans * fat;
        fat = fat * fat;
        k >>= 1;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            printf("%d%c", ans.a[1][1].a[i][j], j == n ? '\n' : ' ');
        }
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值