#P16668. [CSPro 30] 矩阵运算
[CSPro 30] 矩阵运算
Background
The testdata on Luogu is for non-official communication only and is not official testdata. Official judging link: https://www.cspro.org/。
Problem Description
$\text{Softmax}(\frac{Q \times K^T}{\sqrt{d}}) \times V$ is the core expression of the attention module in Transformers, where , , and are all matrices with rows and columns, denotes the transpose of matrix , and denotes matrix multiplication.
To make computation easier, student Dundun simplifies Softmax into an element-wise multiplication by a one-dimensional vector of length :
Element-wise multiplication here means multiplying corresponding positions. Let be the -th element of vector . That is, every element in the -th row of is multiplied by .
Given matrices , , and , and vector , compute the result produced by Dundun using the simplified formula.
Input Format
Read input from standard input.
The first line contains two positive integers and separated by spaces, representing the size of the matrices.
Then matrices , , and are given in order. Each matrix is given in lines, and each line contains integers separated by spaces. The -th number in the -th line corresponds to the element in row and column of the matrix.
The last line contains integers, representing vector .
Output Format
Write output to standard output.
Output lines in total, each containing integers separated by spaces, representing the computed result.
3 2
1 2
3 4
5 6
10 10
-20 -20
30 30
6 5
4 3
2 1
4 0 -5
480 240
0 0
-2200 -1100
Hint
Subtasks
of the testdata satisfies: and ; all elements in the input matrices and vectors are integers, and their absolute values do not exceed .
All testdata satisfies: and ; all elements in the input matrices and vectors are integers, and their absolute values do not exceed .
Hint
Please carefully estimate the value range after matrix multiplication, and use an appropriate data type to store the integers in the matrices.
Translated by ChatGPT 5