跳到正文

题解

题解:P2260 [清华集训2012]模积和

Hsl_Beat约 6 分钟
原文

这是 2025年9月3日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年9月3日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 6 分钟
获赞
2
题目
P2260

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

原式就等于:

∑i=1n∑j=1m(n mod i)×(m mod j),i≠j \sum_{i = 1} ^ n \sum_{j = 1} ^ m (n \bmod i) \times (m \bmod j),i \not=j

不妨设n≤mn \leq m。

=∑i=1n(n mod i)×∑j=1m(m mod j)−∑i=1n(n mod i)×(m mod i) = \sum_{i = 1} ^ n (n \bmod i) \times \sum_{j = 1} ^ m (m \bmod j) - \sum_{i = 1} ^ n (n \bmod i) \times (m \bmod i) =∑i=1n(n−⌊ni⌋×i)×∑j=1m(m−⌊mj⌋×j)−∑i=1n(n−⌊ni⌋×i)(m−⌊nj⌋×j) = \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i) \times \sum_{j = 1} ^ m (m - \left \lfloor \frac{m}{j} \right \rfloor \times j) - \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i) (m - \left \lfloor \frac{n}{j} \right \rfloor \times j) =∑i=1n(n−⌊ni⌋×i)×∑j=1m(m−⌊mj⌋×j)−∑i=1n(n−⌊ni⌋×i)(m−⌊mi⌋×i) = \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i) \times \sum_{j = 1} ^ m (m - \left \lfloor \frac{m}{j} \right \rfloor \times j)-\sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i) (m - \left \lfloor \frac{m}{i} \right \rfloor \times i) =∑i=1n(n−⌊ni⌋×i)×∑j=1m(m−⌊mj⌋×j)−∑i=1n(nm−n×i×⌊mi⌋−m×i×⌊ni⌋+i2×⌊ni⌋×⌊mi⌋)= \sum_{i = 1} ^ n (n - \left \lfloor \frac{n}{i} \right \rfloor \times i) \times \sum_{j = 1} ^ m (m - \left \lfloor \frac{m}{j} \right \rfloor \times j)-\sum_{i = 1} ^ n (nm - n \times i \times \left \lfloor \frac{m}{i} \right \rfloor - m \times i \times \left \lfloor \frac{n}{i} \right \rfloor + i ^ 2 \times \left \lfloor \frac{n}{i} \right \rfloor \times \left \lfloor \frac{m}{i} \right \rfloor)

显然对这33坨数论分块就可以了

C++
#include<bits/stdc++.h>
namespace std;
#define int long long
const int mod = 19940417;
int fastpow(int a, int b)
{
    int res = 1;
    while (b) {
        if (b & 1) {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}
int calc(int x)
{
    return (x * (x + 1) / 2) % mod;
}
int calcc(int x)
{
    return (((x * (x + 1)) % mod * (2 * x + 1)) % mod * 3323403) % mod;
}
signed main()
{
    int n, m;
    cin >> n >> m;
    if (n > m) {
        swap(n, m);
    }
    int ans1 = 0;
    for (int l = 1; l <= n; ) {
        int r = n / (n / l);  
        int d = n / l;
        ans1 = (ans1 + mod + ((calc(r) - calc(l - 1)) * d) % mod) % mod;  
        l = r + 1;
    }
    ans1 = (n * n % mod + mod - ans1) % mod;  
    int ans2 = 0;
    for (int l = 1; l <= m; ) {
        int r = m / (m / l);  
        int d = m / l;
        ans2 = (ans2 + mod + ((calc(r) - calc(l - 1)) * d) % mod) % mod;  
        l = r + 1;
    }
    ans2 = (m * m % mod + mod - ans2) % mod;
    int ans = ans1 * ans2 % mod;
    int ans3 = 0;
    for (int l = 1; l <= n; ) {
        int r = min(n / (n / l), m / (m / l));  
        int d1 = n / l;
        int d2 = m / l;
        int b = ((calc(r) - calc(l - 1) + mod) % mod * (d1 * m % mod + d2 * n % mod)) % mod;  
        int c = (((calcc(r) - calcc(l - 1) + mod) % mod * d1) % mod * d2) % mod;  
        ans3 = (ans3 + (((r - l + 1) * n) % mod * m) % mod + mod - b + c) % mod;  
        l = r + 1;
    }
    ans = (ans - ans3 + mod) % mod;  
    cout << ans;
    return 0;
}