【BZOJ2154】Crash的数字表格

【题目链接】

  • 点击打开链接

【思路要点】

  • 补档博客,无题解。

【代码】

#include
using namespace std;
#define MAXN	10000005
#define P	20101009
template  void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
int tot, prime[MAXN];
int f[MAXN], g[MAXN], miu[MAXN];
long long sum[MAXN];
void init(int n) {
	miu[1] = 1; sum[1] = 1;
	for (int i = 2; i <= n; i++) {
		if (f[i] == 0) {
			f[i] = g[i] = prime[++tot] = i;
			miu[i] = -1; sum[i] = 1 - i;
			for (int j = i; n / j >= i; j *= i)
				sum[j * i] = 1 - i;
		} else sum[i] = sum[g[i]] * sum[i / g[i]] % P;
		for (int j = 1; j <= tot && prime[j] <= f[i]; j++) {
			int tmp = prime[j] * i;
			if (tmp > n) break;
			f[tmp] = prime[j];
			if (prime[j] == f[i]) miu[tmp] = 0, g[tmp] = g[i] * f[i];
			else miu[tmp] = -miu[i], g[tmp] = prime[j];
		}
	}
}
int main() {
	long long n, m;
	read(n), read(m);
	if (n > m) swap(n, m);
	init(m);
	long long ans = 0 ;
	for (int i = 1; i <= n; i++) {
		long long tmp = n / i;
		tmp = tmp * (tmp + 1) / 2 % P;
		long long tnp = m / i;
		tnp = tnp * (tnp + 1) / 2 % P;
		ans = (ans + sum[i] * i % P * tmp % P * tnp) % P;
	}
	cout << (ans % P + P) % P << endl;
	return 0;
}

你可能感兴趣的:(【OJ】BZOJ,【类型】做题记录,【算法】数学,【算法】莫比乌斯反演)