[hdu4965]矩陣快速冪優化 不規則矩陣相乘 模板

Fast Matrix Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 506    Accepted Submission(s): 265


Problem Description
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.

Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he will choose a number N (4 <= N <= 1000), and for N times, he keeps throwing his dice for K times (2 <=K <= 6) and writes down its number on the top face to make an N*K matrix A, in which each element is not less than 0 and not greater than 5. Then he does similar thing again with a bit difference: he keeps throwing his dice for N times and each time repeat it for K times to write down a K*N matrix B, in which each element is not less than 0 and not greater than 5. With the two matrix A and B formed, Alice’s task is to perform the following 4-step calculation.

Step 1: Calculate a new N*N matrix C = A*B.
Step 2: Calculate M = C^(N*N). 
Step 3: For each element x in M, calculate x % 6. All the remainders form a new matrix M’.
Step 4: Calculate the sum of all the elements in M’. 

Bob just made this problem for kidding but he sees Alice taking it serious, so he also wonders what the answer is. And then Bob turn to you for help because he is not good at math.
 

Input
The input contains several test cases. Each test case starts with two integer N and K, indicating the numbers N and K described above. Then N lines follow, and each line has K integers between 0 and 5, representing matrix A. Then K lines follow, and each line has N integers between 0 and 5, representing matrix B.

The end of input is indicated by N = K = 0.
 

Output
For each case, output the sum of all the elements in M’ in a line.
 

Sample Input
4 2 5 5 4 4 5 4 0 0 4 2 5 5 1 3 1 5 6 3 1 2 3 0 3 0 2 3 4 4 3 2 2 5 5 0 5 0 3 4 5 1 1 0 5 3 2 3 3 2 3 1 5 4 5 2 0 0
 

Sample Output
14 56
 

Author
SYSU
 

Source

自己寫的一份,略顯臃腫

#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
const int MAXN = 7;
#define mod 6
struct Matrix
{
    int mat[MAXN][MAXN];
    void Zero()
    {
        memset(mat, 0, sizeof(mat));
    }
    void Unit()
    {
        memset(mat, 0, sizeof(mat));
        for (int i = 0; i < MAXN; i++)
            mat[i][i] = 1;
    }
    void output()
    {
        for (int i = 0; i < MAXN; i++)
        {
            for (int j = 0; j < MAXN; j++)
            {
                printf("%d ", mat[i][j]);
            }
            printf("\n");
        }
    }
};

Matrix operator*(Matrix &a, Matrix &b)
{
    Matrix tmp;
    tmp.Zero();
    for (int k = 0; k < MAXN; k++)
    {
        for (int i = 0; i < MAXN; i++)
        {
            if (!a.mat[i][k])
                continue;
            for (int j = 0; j < MAXN; j++)
            {
                tmp.mat[i][j] += a.mat[i][k] * b.mat[k][j] % mod;
                if ( tmp.mat[i][j] >= mod)
                    tmp.mat[i][j] -= mod;
            }

        }
    }
    return tmp;
}
Matrix operator ^(Matrix a, int k)
{
    Matrix tmp;
    tmp.Unit();
    for (; k; k >>= 1)
    {
        if (k & 1)
            tmp = tmp * a;
        a = a * a;
    }
    return tmp;
}

int mta[1005][1005] = zero;
int mtb[1005][1005] = zero;
int cc[1005][1005] = zero;
int tmp[1005][1005] = zero;
void read(int &res)
{
    res = 0; char c = ' ';
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar();
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n, k;
    while (scanf("%d%d", &n, &k), n || k)
    {

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < k; j++)
            {
                // scanf("%d", &mta[i][j]);
                read(mta[i][j]);
            }
        }
        for (int i = 0; i < k; i++)
        {
            for (int j = 0; j < n; j++)
            {
                // scanf("%d", &mtb[i][j]);
                read(mtb[i][j]);
            }
        }
        Matrix c;
        c.Zero();
        for (int i = 0; i < k; i++)
        {
            for (int j = 0; j < k; j++)
            {
                for (int d = 0; d < n; d++)
                {
                    c.mat[i][j] += mtb[i][d] * mta[d][j];
                }
                c.mat[i][j] %= mod;
            }
        }

        c = c ^ (n * n - 1);
        memset(cc, 0, sizeof(cc));
        for (int i = 0; i < k; i++)
        {
            for (int j = 0; j < k; j++)
            {
                cc[i][j] = c.mat[i][j];
            }
        }
        memset(tmp, 0, sizeof(tmp));
        int kk = k;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < kk; j++)
            {
                for (int k = 0; k < kk; k++)
                {
                    tmp[i][j] += mta[i][k] * cc[k][j] % mod;
                    if (tmp[i][j] >= mod)
                        tmp[i][j] -= mod;
                }
            }
        }
        memset(mta, 0, sizeof(mta));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < kk; k++)
                {
                    mta[i][j] += tmp[i][k] * mtb[k][j] % mod;
                    if (mta[i][j] >= mod)
                        mta[i][j] -= mod;
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                ans += mta[i][j];
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}


不規則矩陣相乘整合成版


#include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

struct Matrix
{
    int n, m;
    vector<vector<int> >M;
    Matrix() {n=0,m=0;}
    Matrix(int a, int b): n(a), m(b)
    {
        M.resize(n);
        for (int i = 0; i < n; i++)
            M[i].resize(m, 0);
    }
    void Zero()
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
                M[i][j] = 0;
        }
    }
    void Unit()
    {
        for (int i = 0; i < min(n, m); i++)
            M[i][i] = 1;
    }
    friend Matrix operator * (const Matrix &a, const Matrix &b)
    {
        Matrix c(a.n, b.m);
        for (int i = 0; i < a.n; i++)
        {
            for (int k = 0; k < b.n; k++)
            {
                for (int j = 0; j < b.m; j++)
                {
                    c.M[i][j] += a.M[i][k] * b.M[k][j];      //注意這裏的循環順序
                    c.M[i][j] %= 6;
                }
            }
        }
        return c;
    }
    friend Matrix operator ^(Matrix a, int k)
    {
        Matrix res(a.n, a.m);
        res.Unit();
        while (k)
        {
            if (k & 1)
                res = res * a;
            a = a * a;
            k >>= 1;
        }
        return res;
    }
};
void read(int &res)
{
    res = 0; char c = ' ';
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar();
}

int main(void)
{
    // freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin);
    int n, k;
    while (~scanf("%d %d", &n, &k) && n + k)
    {
        Matrix A(n, k), B(k, n);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < k; j++)
                read(A.M[i][j]);
        for (int i = 0; i < k; i++)
            for (int j = 0; j < n; j++)
                read(B.M[i][j]);
        Matrix C = B * A;
        int b = n * n - 1;
        Matrix res = C ^ b;
        Matrix ans = A * (res * B);
        int sum = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                sum += ans.M[i][j];
        printf("%d\n", sum);
    }

    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章