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
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.
The end of input is indicated by N = K = 0.
自己寫的一份,略顯臃腫
#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;
}