余数之和(HYSBZ - 1257,约数 + 数论分块)

一.题目链接.

HYSBZ-1257

二.题目大意:

 Calculate \; f(n, k)=k \%1 + k \% 2 + ... + k \% n\;\;\; (k,n \leq 1e9)

三.分析:

\because a \% b = a - \lfloor \frac{a}{b} \rfloor b

\therefore f(n, k) = (k - \lfloor \frac{k}{1} \rfloor \times 1) + (k - \lfloor \frac{k}{2} \rfloor \times 2) + ... + (k - \lfloor \frac{k}{n} \rfloor \times n)

\therefore f(n, k) = nk - [( \lfloor \frac{k}{1} \rfloor \times 1) + (\lfloor \frac{k}{2} \rfloor \times 2) + ... + (\lfloor \frac{k}{n} \rfloor \times n)]

Conclusion:\;\;\; \lfloor \frac{k}{x} \rfloor \; has \; only \; 2\sqrt k \; values \; at \; most. 

Prove:\;\;\; when \; x \leq \sqrt k, \;\; because \; x \; has \; only \; \sqrt k \; values, \;\; \lfloor \frac{k}{x} \rfloor \; has \; only \; \sqrt k \; values \; at \; most. \\ when \; x > \sqrt k, \;\; because \; \lfloor \frac{k}{x} \rfloor < \sqrt k, \;\; \lfloor \frac{k}{x} \rfloor \; has \; only \; \sqrt k \; values \; at \; most.

From \; the \; above, \; \lfloor \frac{k}{x} \rfloor \; has \; only \; 2\sqrt k \; values \; at \; most.

 From \; the \; above \; we \; can \; see \; that \; \lfloor \frac{k}{x} \rfloor \; is \; composed \; of \; 10^5 \; segments \; at \; most, \; and \; \lfloor \frac{k}{x} \rfloor \; in \; each \; segment \; is \; the \; same.

Therefore, \;we \; only \; need \; to \; find \; the\; position \; of\; the\; beginning\; and\; end\; of \;each\; segment. \\ Then \; the \; sum \; of \; each\; segment \; can\; be \;calculated \; in \; O(10^5) \;by \;formula\; for\; sum\; of\; difference\; series.

 Conclusion:\;\;\; Segment\; that\; start\; with\; x \;end\; at\; g(x) = \lfloor {\frac{k}{ \lfloor {\frac{k}{x}} \rfloor }} \rfloor.

Prove1: \;\;\;Easy \;to\; get:\; g (x) \;is \;a \;non-incrementing\; function.

Prove2: \;\;\; \lfloor \frac{k}{x} \rfloor = \lfloor \frac{k}{g(x)} \rfloor

\because g(x) = \lfloor { \frac{k}{ \lfloor \frac{k}{x} \rfloor } } \rfloor

\therefore k = g(x) \lfloor \frac{k}{x} \rfloor + q \;\; (0 \leq q < \lfloor \frac{k}{x} \rfloor ) \;\;\;\;\;\;\;\;\;\; (1)

\therefore \lfloor \frac{k}{g(x)} \rfloor = \lfloor {\frac{g(x) \lfloor \frac{k}{x} \rfloor + q}{g(x)}} \rfloor = \lfloor \lfloor \frac{k}{x} \rfloor + \frac{q}{g(x)} \rfloor \geq \lfloor \frac{k}{x} \rfloor \;\;\;\;\;\;\;\;\;\; (2)

\because \frac{k}{x} \geq \lfloor \frac{k}{x} \rfloor

\therefore \frac{k}{\frac{k}{x}} \leq \frac{k}{\lfloor \frac{k}{x} \rfloor}

\therefore \lfloor \frac{k}{\frac{k}{x}} \rfloor \leq \lfloor \frac{k}{\lfloor \frac{k}{x} \rfloor } \rfloor

\therefore g(x) \geq \lfloor \frac{k}{\frac{k}{x}} \rfloor = \lfloor x \rfloor = x

\therefore \frac{k}{g(x)} \leq \frac{k}{x}

\therefore \lfloor \frac{k}{g(x)} \rfloor \leq \lfloor \frac{k}{x} \rfloor \;\;\;\;\;\;\;\;\;\; (3)

From \; (2) \;and\; (3), \; \lfloor \frac{k}{g(x)} \rfloor = \lfloor \frac{k}{x} \rfloor

Prove3: \;\;\; \lfloor \frac{k}{g(x + 1)} \rfloor \neq \lfloor \frac{k}{x} \rfloor

Suppose \;\lfloor \frac{k}{g(x) + 1} \rfloor = \lfloor \frac{k}{x} \rfloor

\therefore k = (g(x) + 1) \lfloor \frac{k}{x} \rfloor + q\;' \;\;\; (0 \leq q\;' < g(x) + 1)

\therefore k = g(x) \lfloor \frac{k}{x} \rfloor + \lfloor \frac{k}{x} \rfloor + q\;'\;\;\; (0 \leq q\;' < g(x) + 1) \;\;\;\;\;\;\;\;\;\; (4)

From \; (1) \;and\; (4), \; \lfloor \frac{k}{x} \rfloor + q\;' = q < \lfloor \frac{k}{x} \rfloor \;\;\; (0 \leq q\;' < g(x) + 1)

\because The \;above\; formula \;is\; obviously\; contradictory.

\therefore \lfloor \frac{k}{g(x + 1)} \rfloor \neq \lfloor \frac{k}{x} \rfloor

From\; Prove_{1, 2, 3},\;segment\; that\; start\; with\; x \;end\; at\; g(x) = \lfloor {\frac{k}{ \lfloor {\frac{k}{x}} \rfloor }} \rfloor.

四.代码实现:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main()
{
    int n, k;
    scanf("%d %d", &n, &k);
    ll ans = 1ll * n * k;
    for(int l = 1, r; l <= n; l = r + 1)
    {
        if(k / l == 0)  break;
        r = min(k / (k / l), n);
        ans -= 1ll * (k / l) * (l + r) * (r - l + 1) / 2;
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/The___Flash/article/details/104189563