1. 程式人生 > >[BZOJ 2956]模積和

[BZOJ 2956]模積和

HP markdown solution lin rip swap putchar sin it is

Description

題庫鏈接

\[\sum_{i=1}^n\sum_{j=1\wedge i\neq j}^m (n\mod i)(m\mod j)\] 取模。

\(1\leq n,m\leq 10^9\)

Solution

\[\begin{aligned}\Rightarrow&\sum_{i=1}^n\sum_{j=1}^m(n\mod i)(m\mod j)-\sum_{i=1}^{min\{n,m\}}(n\mod i)(m\mod i)\\=&\sum_{i=1}^n\sum_{j=1}^m\left(n-\left\lfloor\frac{n}{i}\right\rfloor i\right)\left(m-\left\lfloor\frac{m}{j}\right\rfloor j\right)-\sum_{i=1}^{min\{n,m\}}\left(n-\left\lfloor\frac{n}{i}\right\rfloor i\right)\left(m-\left\lfloor\frac{m}{i}\right\rfloor i\right)\\=&n^2m^2-m^2\sum_{i=1}^n\left\lfloor\frac{n}{i}\right\rfloor i-n^2\sum_{i=1}^m\left\lfloor\frac{m}{i}\right\rfloor i+\sum_{i=1}^n\left(\left\lfloor\frac{n}{i}\right\rfloor i\sum_{j=1}^m\left\lfloor\frac{m}{j}\right\rfloor j\right)-\sum_{i=1}^{min\{n,m\}}\left(nm-\left\lfloor\frac{n}{i}\right\rfloor im-\left\lfloor\frac{m}{i}\right\rfloor in+\left\lfloor\frac{n}{i}\right\rfloor\left\lfloor\frac{m}{i}\right\rfloor i^2\right)\end{aligned}\]

然後就是喜聞樂見的數論分塊了。

Code

//It is made by Awson on 2018.2.28
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n')) #define lowbit(x) ((x)&(-(x))) using namespace std; const int yzh = 19940417, inv6 = 3323403; void read(int &x) { char ch; bool flag = 0; for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar()); for
(x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar()); x *= 1-2*flag; } void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); } void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); } int n, m, ans, t1, t2; int cal(int l, int r) {return 1ll*(l+r)*(r-l+1)/2%yzh; } int cal2(int l, int r) {return (1ll*r*(r+1)%yzh*(r*2+1)%yzh*inv6%yzh-1ll*l*(l+1)%yzh*(l*2+1)%yzh*inv6%yzh)%yzh; } void work() { read(n), read(m); ans = 1ll*n*m%yzh*n%yzh*m%yzh; for (int i = 1, last; i <= n; i = last+1) last = n/(n/i), t1 = (t1+1ll*n/i*cal(i, last)%yzh)%yzh; ans = (ans-1ll*m*m%yzh*t1%yzh)%yzh; for (int i = 1, last; i <= m; i = last+1) last = m/(m/i), t2 = (t2+1ll*m/i*cal(i, last)%yzh)%yzh; ans = (ans-1ll*n*n%yzh*t2%yzh)%yzh; for (int i = 1, last; i <= n; i = last+1) last = n/(n/i), ans = (ans+1ll*n/i*cal(i, last)%yzh*t2%yzh)%yzh; if (n > m) Swap(n, m); for (int i = 1, last; i <= n; i = last+1) { last = Min(n/(n/i), m/(m/i)); int tmp = 1ll*(last-i+1)*n%yzh*m%yzh; tmp = (tmp-1ll*n/i*m%yzh*cal(i, last)%yzh)%yzh; tmp = (tmp-1ll*m/i*n%yzh*cal(i, last)%yzh)%yzh; tmp = (tmp+1ll*(n/i)*(m/i)%yzh*cal2(i-1, last)%yzh)%yzh; ans = (ans-tmp)%yzh; } writeln((ans+yzh)%yzh); } int main() { work(); return 0; }

[BZOJ 2956]模積和