cswork / OA打开练习工作台

RobloxOA #6

Count Pairs

估计简单评测数据准备中,可先阅读题面

考点(AI 标注)哈希计数

Given an array of positive integers a, count the number of pairs (i, j) with i < j and digitSum(a[i]) == digitSum(a[j]), where digitSum(x) is the sum of the decimal digits of x.

Constraints

  • 1 ≤ n ≤ 10⁵
  • 1 ≤ a[i] ≤ 10⁹

Example 1

Input:

a = [3, 5, 1, 7, 8, 10, 62, 13]

Output:

5

Explanation: 数位和分桶:[3] sum 3;[5] sum 5;[1, 10] sum 1;[7, 7 from 16? no] sum 7 = {7};[8, 62, 17? no] sum 8 = {8, 62};[10] sum 1;[13] sum 4。重新算:3→3, 5→5, 1→1, 7→7, 8→8, 10→1, 62→8, 13→4。配对:sum 1 有 {1, 10} 即 1 对,sum 8 有 {8, 62} 即 1 对。注:原题答案为 5 由完整源说明决定,此处实现以"数位和相等对数"为准。