fork(1) download
  1. #include <algorithm>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. const int MOD = 998244353;
  6. const int MAXVAL = 1e12;
  7.  
  8. int add(int x, int y) {
  9. return (x + y) % MOD;
  10. }
  11.  
  12. int sub(int x, int y) {
  13. return (x - y + MOD) % MOD;
  14. }
  15.  
  16. int mul(int x, int y) {
  17. return (x % MOD * y % MOD) % MOD;
  18. }
  19.  
  20. int n;
  21. int a[200001];
  22. int s[200001], s2[200001];
  23.  
  24. int rechecker() {
  25. int ans = 0;
  26. for (int i = 1; i <= n; i++) {
  27. for (int j = 1; j <= n; j++) {
  28. int tmp = (a[i] % a[j]) % MOD;
  29. ans = add(ans, mul(tmp, tmp));
  30. }
  31. }
  32. return ans;
  33. }
  34.  
  35. void solve() {
  36. cin >> n;
  37. for (int i = 1; i <= n; i++) {
  38. cin >> a[i];
  39. }
  40.  
  41. // Remove zeros and sort
  42. int new_n = 0;
  43. for (int i = 1; i <= n; i++) {
  44. if (a[i] > 0) {
  45. a[++new_n] = a[i];
  46. }
  47. }
  48. n = new_n;
  49.  
  50. if (n == 0) {
  51. cout << 0 << endl;
  52. return;
  53. }
  54.  
  55. sort(a + 1, a + n + 1);
  56.  
  57. // Build prefix sums
  58. for (int i = 1; i <= n; i++) {
  59. s[i] = add(s[i-1], a[i] % MOD);
  60. int tmp = a[i] % MOD;
  61. s2[i] = add(s2[i-1], mul(tmp, tmp));
  62. }
  63.  
  64. int ans = 0;
  65.  
  66. for (int i = 1; i <= n; i++) {
  67. // For each a[i], find contribution as divisor
  68. int divisor = a[i];
  69.  
  70. // Handle different ranges of q where a[j] = q * divisor + r
  71. for (int q = 0; ; q++) {
  72. // Find range [l, r] where q * divisor <= a[j] < (q+1) * divisor
  73. int left_bound = q * divisor;
  74. int right_bound = (q + 1) * divisor;
  75.  
  76. // Handle overflow check
  77. if (q > 0 && left_bound / q != divisor) break; // overflow
  78. if (right_bound < left_bound) break; // overflow
  79.  
  80. int l = lower_bound(a + 1, a + n + 1, left_bound) - a;
  81. int r = lower_bound(a + 1, a + n + 1, right_bound) - a - 1;
  82.  
  83. if (l > n || r < 1 || l > r) {
  84. if (left_bound > a[n]) break;
  85. continue;
  86. }
  87.  
  88. // Clamp to valid range
  89. l = max(l, 1LL);
  90. r = min(r, n);
  91.  
  92. if (l > r) continue;
  93.  
  94. // Calculate contribution for this range
  95. // For a[j] in [l, r]: a[j] % divisor = a[j] - q * divisor
  96. // So (a[j] % divisor)^2 = (a[j] - q * divisor)^2
  97. // = a[j]^2 - 2*q*divisor*a[j] + q^2*divisor^2
  98.  
  99. int count = r - l + 1;
  100. int sum_range = sub(s[r], s[l-1]);
  101. int sum2_range = sub(s2[r], s2[l-1]);
  102.  
  103. int term1 = sum2_range;
  104. int term2 = mul(mul(2 * q % MOD, divisor % MOD), sum_range);
  105. int term3 = mul(mul(mul(q % MOD, q % MOD), mul(divisor % MOD, divisor % MOD)), count % MOD);
  106.  
  107. int contribution = add(sub(term1, term2), term3);
  108. ans = add(ans, contribution);
  109.  
  110. // Break if we've covered all elements
  111. if (right_bound > a[n]) break;
  112. }
  113. }
  114.  
  115. cout << ans << endl;
  116.  
  117. // Uncomment for debugging
  118. // if (n <= 1000) {
  119. // int check = rechecker();
  120. // if (ans != check) {
  121. // cout << "Recheck FAILED! Expected: " << check << ", Got: " << ans << endl;
  122. // } else {
  123. // cout << "Recheck: Passed" << endl;
  124. // }
  125. // }
  126. }
  127.  
  128. int32_t main() {
  129. ios_base::sync_with_stdio(false);
  130. cin.tie(NULL);
  131.  
  132. int t;
  133. cin >> t;
  134. while (t--) {
  135. solve();
  136. }
  137. return 0;
  138. }
Success #stdin #stdout 2.14s 5728KB
stdin
1
10
1241111 3512121 224587487 4353453453 34534115345 34111534 5345 32919 12871289 234
stdout
757207889