Luogu P2260 [Tsinghua Training 2012] modular product sum

Disgusting! ! ! ! ! ! ! !

Title link

Ideas

\(\sum\limits_{i = 1}^{n} (n \mod i)\sum\limits_{j=1}^{m}(m\mod j)[i\neq j]\)

Assuming there are no restrictions \ (i \ neq j \)

\(\sum\limits_{i = 1}^{n} (n \mod i)\sum\limits_{j=1}^{m}(m\mod j)\)

Just look at the left half:

\(\ \ \ \sum\limits_{i=1}^{n}(n\% i)\)

\(= \sum\limits_{i=1}^{n}(n - \lfloor \frac{n}{i}\rfloor * i)\)

\(= \sum\limits_{i=1}^{n}n - \sum\limits_{i = 1}^{n}\lfloor\frac{n}{i}\rfloor*i\)

Obviously, the number-theoretic partitioning is the same as the right half, and can be done by the number-theoretic partitioning (the remainder is exactly the same as that question)

Look at the situation of \ (i = j \) , namely

$ \sum\limits_{i=1}^{k=\min(n,m)}(n\mod i)(m\mod i)$

\(=\sum\limits_{i=1}^{k}(n-\lfloor\frac{n}{i}\rfloor*i)(m-\lfloor\frac{m}{i}\rfloor*i)\)

\(=\sum\limits_{i=1}^{k}(nm-(\lfloor\frac{n}{i}\rfloor*m+\lfloor\frac{m}{i}\rfloor*n)*i+\lfloor\frac{n}{i}\rfloor*\lfloor\frac{m}{i}\rfloor*i^2)\)

Use number theory to find the above two formulas, subtract the following formula from the total, and pay attention to the inverse element of division

\(ps\)

  1. Able to model, good things, many models

  2. Little knowledge

    \[\sum\limits_{i = l}^{r}i=(r -l + 1)*(l + r) / 2 \]

    \[\sum\limits_{i = 1} ^{n}i^2= n * (n + 1) * (2* n + 1)/ 6 \]

Time complexity \ (O (\ sqrt {n}) \)

Code

/*
Author:loceaner
*/
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

const int A = 5e5 + 11;
const int B = 1e6 + 11;
const int inv6 = 3323403;
const int inv2 = 9970209;
const int mod = 19940417;
const int inf = 0x3f3f3f3f;

inline int read() {
	char c = getchar();
	int x = 0, f = 1;
	for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
	for( ; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
	return x * f;
}

int n, m, k;

inline int sum(int l, int r) {
	return (r - l + 1) * (l + r) / 2 % mod;
}

inline int sum1(int r) {
	return r * (r + 1) % mod * (2 * r + 1) % mod * inv6 % mod;
}

inline int sum2(int l, int r) {
	return (sum1(r) - sum1(l - 1)) % mod;
}

inline int solve(int n) {
	int ans = n * n;
	for (int l = 1, r; l <= n; l = r + 1) {
		if (n / l == 0) break;
		r = min(n / (n / l), n);
		ans -= (n / l) % mod * sum(l, r) % mod;
		ans %= mod;
	}
	return (ans % mod + mod) % mod;
}

signed main() {
	n = read(), m = read();
	int ans1 = solve(n) * solve(m) % mod, ans2 = 0;
	for (int l = 1, r, now1, now2, now3, now4; l <= min(n, m); l = r + 1) {
		r = min(n / (n / l), m / (m / l));
		now1 = n * m % mod * (r - l + 1) % mod;
		now2 = ((n / l) * (m / l) % mod * sum2(l, r) % mod + mod) % mod;
		now3 = ((n / l) * m % mod + (m / l) * n % mod) * sum(l, r) % mod;
		now4 = (now1 + now2 - now3 + mod) % mod;
		ans2 = (ans2 + now4) % mod;
	}
	cout << ((ans1 - ans2) % mod + mod) % mod;
	return 0;
}

Guess you like

Origin www.cnblogs.com/loceaner/p/12731906.html