[关闭]
@wsndy-xx 2018-06-13T15:06:29.000000Z 字数 1389 阅读 803

清华集训2012 [模积和]

题解


question


solve


数论分块
时间复杂度

Code

  1. #include <bits/stdc++.h>
  2. #define LL long long
  3. const LL Mod = 19940417, Inv = (Mod + 1) / 6;
  4. LL n, m, Answer, Answer_1, Answer_2, Answer_3;
  5. LL Calc(LL x) {return x * (x + 1) % Mod * (2 * x + 1) % Mod * Inv % Mod;}
  6. LL Calc_2(LL l, LL r) {return (l + r) * (r - l + 1) / 2 % Mod;}
  7. int main() {
  8. std:: cin >> n >> m;
  9. if(n > m) std:: swap(n, m);
  10. for(int i = 1, r; i <= n; i = r + 1) {
  11. r = n / (n / i);
  12. Answer_1 = (Answer_1 + (Calc_2(i, r) * (n / i) % Mod)) % Mod;
  13. }
  14. Answer_1 = (n * n - Answer_1);
  15. Answer_1 %= Mod;
  16. for(int i = 1, r; i <= m; i = r + 1) {
  17. r = m / (m / i);
  18. Answer_2 = (Answer_2 + (Calc_2(i, r) * (m / i) % Mod)) % Mod;
  19. }
  20. Answer_2 = (m * m - Answer_2) % Mod;
  21. for(int i = 1, r; i <= n; i = r + 1) {
  22. r = std:: min(n / (n / i), m / (m / i));
  23. LL imp = n * m % Mod * (r - i + 1) % Mod;
  24. imp = (imp + (- (m * (n / i) + n * (m / i)) % Mod * Calc_2(i, r) % Mod)) % Mod;
  25. imp = (imp + n / i * (m / i) % Mod * (Calc(r) - Calc(i - 1)) % Mod) % Mod;
  26. Answer_3 = (Answer_3 + imp) % Mod;
  27. }
  28. Answer = Answer_1 * Answer_2 % Mod;
  29. Answer -= Answer_3;
  30. Answer %= Mod;
  31. if(Answer < 0) Answer += Mod;
  32. std:: cout << Answer;
  33. return 0;
  34. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注