AcWing 5082. 矩阵运算(Python)
原题链接
简单
作者:
Code_Tyro
,
2024-11-25 18:47:30
,
所有人可见
,
阅读 3
Python代码
import sys
read_line = sys.stdin.readline
n, d = map(int, read_line().split())
Q = [list(map(int, read_line().split())) for _ in range(n)]
K = [list(map(int, read_line().split())) for _ in range(n)]
V = [list(map(int, read_line().split())) for _ in range(n)]
W = list(map(int, read_line().split()))
# 初始化结果矩阵
res_1 = [[0] * d for _ in range(d)]
res = [[0] * d for _ in range(n)]
# 计算res_1
for i in range(d):
for j in range(d):
for k in range(n):
res_1[i][j] += K[k][i] * V[k][j] # 注意这里的索引,K的转置表示为K[k][i]
# 计算res并输出结果
for i in range(n):
for j in range(d):
tem = 0
for k in range(d):
tem += Q[i][k] * res_1[k][j]
res[i][j] = tem * W[i]
print(' '.join(map(str,res[i])))