Fast Matrix Calculation HDU - 4965
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.
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.
InputThe 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.OutputFor 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
给你一个n*m和一个m*n的矩阵,经过上面的4步之后会得到一个新的矩阵M,求M中所有元素的总和。
n是一个可以到1000的数,但是m巨小,最多到6,矩阵开1000会爆栈,我们可以转化一下:
A*B^(n*n) = A*B*A*B*A*B...*A*B = A*(B*A)^(n*n-1)*B
B*A是一个m*m的矩阵,嘿嘿~~~
//Asimple #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <queue> #include <vector> #include <string> #include <cstring> #include <stack> #include <set> #include <map> #include <cmath> #define swap(a,b,t) t = a, a = b, b = t #define CLS(a, v) memset(a, v, sizeof(a)) #define test() cout<<"============"<<endl #define debug(a) cout << #a << " = " << a <<endl #define dobug(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl using namespace std; typedef long long ll; const int N = 10; const ll MOD=6; const int INF = ( 1<<20 ); const double PI=atan(1.0)*4; const int maxn = 1000+5; const ll mod = 10005; ll n, m, len, ans, sum, v, w, T, num; int A[maxn][maxn], B[maxn][maxn]; int c1[maxn][maxn], c2[maxn][maxn]; struct Matrix { long long grid[N][N]; int row,col; Matrix():row(N),col(N) { memset(grid, 0, sizeof grid); } Matrix(int row, int col):row(row),col(col) { memset(grid, 0, sizeof grid); } //矩阵乘法 Matrix operator *(const Matrix &b) { Matrix res(row, b.col); for(int i = 0; i<res.row; i++) for(int j = 0; j<res.col; j++) for(int k = 0;k<col; k++) res[i][j] = (res[i][j] + grid[i][k] * b.grid[k][j] + MOD) % MOD; return res; } //矩阵快速幂 Matrix operator ^(long long exp) { Matrix res(row, col); for(int i = 0; i < row; i++) res[i][i] = 1; Matrix temp = *this; for(; exp > 0; exp >>= 1, temp = temp * temp) if(exp & 1) res = temp * res; return res; } long long* operator[](int index) { return grid[index]; } void print() { for(int i = 0; i <row; i++) { for(int j = 0; j < col-1; j++) printf("%d ",grid[i][j]); printf("%d\n",grid[i][col-1]); } } }; void input(){ ios_base::sync_with_stdio(false); while( cin >> n >> m && (n+m) ) { for(int i=0; i<n; i++) for(int j=0; j<m; j++) cin >> A[i][j]; for(int i=0; i<m; i++) for(int j=0; j<n; j++) cin >> B[i][j]; Matrix C(m, m); for(int i=0; i<m; i++) { for(int j=0; j<m; j++) { C[i][j] = 0; for(int k=0; k<n; k++) { C[i][j] += ( B[i][k]*A[k][j]); C[i][j] %= 6; } } } C = C^(n*n-1); for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { c1[i][j] = 0; for(int k=0; k<m; k++) { c1[i][j] += A[i][k]*C[k][j]; c1[i][j] %= 6; } } } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { c2[i][j] = 0; for(int k=0; k<m; k++) { c2[i][j] += c1[i][k]*B[k][j]; } } } ans = 0; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { ans += (c2[i][j]%MOD); } } cout << ans << endl; } } int main(){ input(); return 0; }
低调做人,高调做事。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2016-09-13 每日一九度之 题目1033:继续xxx定律