UVA11526 H(n)

发布时间 2023-11-20 15:17:09作者: cxqghzj

题意

  • \(\sum_{i = 1}^{n} \lfloor \frac{n}{i} \rfloor\)

Sol

整除分块。

考虑 \(1 \to n\) 里面固然有很多算重的。

考虑去掉重复计算的东西,不难发现一个块内最大的数显然为 \(\lfloor \frac{n}{\lfloor \frac{n}{i} \rfloor} \rfloor\)

实现是 \(trivial\) 的。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
void solve() {
	int ans = 0;
	int n = read(), res = 0;
	for (int i = 1; i <= n; i = res + 1) {
		res = n / (n / i);
		ans += (n / i) * (res - i + 1);
	}
	write(ans), puts("");
}
signed main() {
	int T = read();
	while (T--) solve();
	return 0;
}