atcoder 競プロ典型90問 004 - Cross Sum
2022.03.06

問題

pythonコード

python
h, w = map(int, input().split())
 
a =  [list(map(int, input().split())) for at in range(h)]
row_sum = []
col_sum = []
 
for i in range(h):
    row_sum.append(sum(a[i]))
for i in range(w):
    col = list(map(lambda x: x[i], a))
    col_sum.append(sum(col))
 
ans = []
for i in range(h):
    row_ans = []
    for j in range(w):
        row_ans.append(str(row_sum[i] + col_sum[j] - a[i][j]))
    ans.append(row_ans)
 
for i in range(h):
    print(' '.join(ans[i]))

キーポイント

  • row_sumとcol_sumを用意する
  • 計算量が10の8乗 から 10の9乗で TLE