gym 101061-B RGB plants 裸矩阵快速幂

来源:互联网 发布:软件界面模糊 编辑:程序博客网 时间:2024/05/17 12:05

heu_summer_final_contest_G
http://codeforces.com/gym/101061/problem/B
题意略, 快速幂求出matrix 的 (n-1)次方, 对其中所有元素求和即可.

#include <stdio.h>#include <iostream>#include <cmath>#include <cstring>using namespace std;const int MAX = 10;const int mod = 1e9+7;struct Martix {    int array[MAX][MAX];    int row, column;    void clear() {        memset(array, 0, sizeof(array));        row = 0, column = 0;    }    void getin() {        cin >> row >> column;        for (int i = 0; i < row; ++i)            for (int j = 0; j < column; ++j)                cin >> array[i][j];    }    void output() {        for (int i = 0; i < row; ++i) {            for (int j = 0; j < column; ++j)                cout << array[i][j] << " ";            cout << endl;        }    }    Martix operator + (const Martix &b) const {        Martix tmp;        tmp.row = row, tmp.column = column;        for (int i = 0; i < row; ++i)            for (int j = 0; j < column; ++j)                tmp.array[i][j] = array[i][j] + b.array[i][j];    }    Martix operator - (const Martix &b) const {        Martix tmp;        tmp.row = row, tmp.column = column;        for (int i = 0; i < row; ++i)            for (int j = 0; j < column; ++j)                tmp.array[i][j] = array[i][j] - b.array[i][j];    }    Martix operator * (const Martix &b) const {        Martix tmp;        tmp.clear();        if (column != b.row)            return tmp;        tmp.row = row, tmp.column = b.column;        for (int i = 0; i < row; ++i)            for (int j = 0; j < b.column; ++j)                for (int k = 0; k < column; ++k) {                    long long t = (((long long)(array[i][k]%mod) * (b.array[k][j]%mod)) % mod);                    tmp.array[i][j] += (int)t;                    while (tmp.array[i][j] >= mod) tmp.array[i][j] -= mod;                }        return tmp;    }} matrix, mt;Martix matpow(Martix tmp, int n, int mod) {    Martix ret;    ret.row = tmp.row, ret.column = tmp.column;    for (int i = 0; i < tmp.row; ++i)        for(int j = 0; j < tmp.row; ++j)            ret.array[i][j] = (i == j);    while (n) {        if (n & 1) {            ret = (ret * tmp);        }        tmp = (tmp * tmp);        n >>= 1;    }    return ret;}void init() {    matrix.row = 3, matrix.column = 3;    for (int i = 0; i < 3; ++i)        for (int j = 0; j < 3; ++j)            matrix.arra[i][j] = j+1+3*i;}void judge() {    init();    int n = 0, T = 0;    cin >> T;    long long sum = 0;    while (T--) {        cin >> n;        sum = 0;        mt = matpow(matrix, n-1);        for (int i = 0; i < 3; ++i)            for (int j = 0; j < 3; ++j){                sum += mt.arra[i][j];                while (sum >= mod) sum -= mod;            }        cout << sum << endl;    }}int main() {//  freopen("in.in", "r", stdin);//  freopen("out.out", "w", stdout);    judge();    return 0;}

本来 memset() 函数是用的两个rep循环, 结果因为 row 没有初始化结果一直T掉,找了半天才发现是这里的问题....

2017-08-28

阅读全文
0 0
原创粉丝点击