fork(4) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MOD = 998244353;
  5.  
  6. const int a_max = 1000001;
  7. int count[a_max];
  8. long long T_g[a_max];
  9.  
  10. int main() {
  11. int N;
  12. std::cin >> N;
  13.  
  14. for(int i = 0; i < N; ++i) {
  15. int x;
  16. std:: cin >> x;
  17. count[x]++;
  18. }
  19.  
  20. for(int g = 1; g < a_max; ++g) {
  21. long long prefix_sum = 0;
  22.  
  23. // Now loop through all the multiples of g
  24. for(long long gmult = g; gmult < a_max; gmult += g) {
  25. // Multiply current element(s) with the sum of the previously seen
  26. T_g[g] += prefix_sum * count[gmult] * gmult;
  27.  
  28. // Don't forget the pairwise sums in the current set
  29. T_g[g] += (gmult * gmult * count[gmult] * (count[gmult] - 1))/2;
  30.  
  31. // Update the prefix sum
  32. prefix_sum += count[gmult] * gmult;
  33. }
  34. }
  35.  
  36. long long answer = 0;
  37. for(int g = a_max - 1; g > 0; --g) {
  38. for(int gmult = 2 * g; gmult < a_max; gmult += g) {
  39. T_g[g] -= T_g[gmult];
  40. }
  41. answer += T_g[g] / g;
  42. }
  43.  
  44. std::cout << answer % MOD << "\n";
  45. return 0;
  46. }
Success #stdin #stdout 0.11s 11524KB
stdin
10
356822 296174 484500 710640 518322 888250 259161 609120 592348 713644
stdout
353891724