fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. const int oo = 1e3 + 3;
  7.  
  8. int t, n, k;
  9. int a[oo];
  10. map<int, int> mp, visited;
  11.  
  12. int C2(int n) {
  13. return n * (n - 1) / 2;
  14. }
  15.  
  16. int next(int n) {
  17. int res = 0;
  18. for (; n != 0; n /= 10) {
  19. int x = n % 10;
  20. res += x * x;
  21. }
  22. return res;
  23. }
  24.  
  25. void solve() {
  26. cin >> n;
  27.  
  28. for (int i = 1; i <= n; i++)
  29. cin >> a[i];
  30.  
  31. for (int j = 1; j <= 1000; j++)
  32. for (int i = 1; i <= n; i++)
  33. a[i] = next(a[i]);
  34.  
  35. map<int, int> cnt;
  36. for (int i = 1; i <= n; i++)
  37. cnt[a[i]]++;
  38.  
  39. int res = 0;
  40. for (auto x : cnt)
  41. res += C2(x.second);
  42.  
  43. cout << res << '\n';
  44. }
  45.  
  46. int32_t main() {
  47.  
  48. cin >> t;
  49.  
  50. while(t--) {
  51. solve();
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 5324KB
stdin
4
5
7 4 16 4 2
4
1 7 10 100
3
4 16 37
3
2 20 4
stdout
1
6
0
1