G. [语言月赛 202502] 沿轴求和

    Type: RemoteJudge 3000ms 512MiB

[语言月赛 202502] 沿轴求和

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.

题目描述

给定一个高维数组 aa,该数组共有 nn 维,大小为 d0×d1××dn1d_0\times d_1\times \cdots \times d_{n-1}。其中,每一维称为一个轴,did_i 对应 i(0i<n)i(0 \le i < n) 号轴。数组下标从 00 开始。

高维数组沿 xx 号轴的求和结果,是一个 n1n-1 维的数组 SS,其大小为 $d_0\times d_1 \times \cdots \times d_{x-1} \times d_{x+1} \times \cdots \times d_{n-1}$。其中,

$$S[w_0][w_1]\cdots[w_{x-1}][w_{x+1}]\cdots[w_{n-1}]=\sum\limits_{i=0}^{d_x-1}{a[w_0][w_1]\cdots[w_{x-1}][i][w_{x+1}]\cdots[w_{n-1}]} $$

现在,给定该高维数组与 xx,求沿 xx 号轴求和的结果。

输入格式

第一行为两个整数 n,xn,x

第二行为 nn 个整数,依次表示 d0,d1,,dn1d_0,d_1,\cdots,d_{n-1}

接下来 d0×d1××dn1d_0\times d_1\times \cdots \times d_{n-1} 行,每行 n+1n+1 个整数 p0,p1,,pn1,valp_0,p_1,\cdots,p_{n-1},val,表示 a[p0][p1][pn1]=vala[p_0][p_1]\cdots[p_{n-1}]=val

输出格式

输出 $\dfrac{d_0\times d_1\times \cdots \times d_{n-1}}{d_x}$ 行,每行 nn 个整数 p0,p1,,px1,px+1,,pn1,wp_0,p_1,\cdots,p_{x-1},p_{x+1},\cdots,p_{n-1},w,表示 $S[p_0][p_1]\cdots[p_{x-1}][p_{x+1}]\cdots[p_{n-1}]=w$。

你可以以任意顺序输出这些行。

2 1
2 2
0 0 1
0 1 2
1 1 4
1 0 3
1 7
0 3
3 0
2 2 2
0 0 0 1
0 0 1 2
0 1 0 3
0 1 1 4
1 0 0 5
1 0 1 6
1 1 0 7
1 1 1 8
0 0 6
0 1 8
1 0 10
1 1 12

提示

样例解释 1

样例 1 给出了一个二维数组,其大小为 2×22\times 2,沿 1 号轴求和。在二维数组中,按 0 号轴求和可理解为按列求和,按 1 号轴求和可理解为按行求和。

$$\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \rightarrow \begin{bmatrix} 3 & 7 \end{bmatrix} $$

样例解释 2

样例 2 给出了一个三维数组,其大小为 2×2×22\times 2\times 2。沿 0 号轴求和的结果大小为 2×22\times 2

S[0][0]S[0][0] 的计算方式为例:

$$S[0][0]=\sum\limits_{i=0}^1{a[i][0][0]=a[0][0][0]+a[1][0][0]=1+5=6} $$

可以得到结果为

[681012]\begin{bmatrix} 6 & 8\\ 10 & 12 \end{bmatrix}

数据规模与约定

  • 对于 60%60\% 的测试数据,保证 n=2n=2
  • 对于 100%100\% 的测试数据,保证 2n162 \le n \le 161di1031 \le d_i \le 10^31i=0n1di2161 \le \prod\limits_{i=0}^{n-1}{d_i} \le 2^{16}100val100-100 \le val \le 100。保证 i[0,n)\forall i\in [0,n),输入的 pi[0,di)p_i \in [0,d_i)

青创八语言月赛欢乐赛

Not Attended
Status
Done
Rule
IOI
Problem
9
Start at
2025-11-25 14:00
End at
2025-11-25 15:00
Duration
1 hour(s)
Host
Partic.
24