_bzoj2005 [Noi2010]能量采集

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2005

令F(i)表示i | gcd(x, y)的对数,f(i)表示gcd(x, y) = i的对数,那么显然

f(i) = F(i) - f(2 * i) - f(3 * i) - ...

F(i)很好求,显然,F(i) = (n / i) * (m / i),除法向下取整。

之后就简单了~

#include <cstdio>
#include <algorithm>

const int maxn = 100005;

int n, m, lmt;
long long f[maxn], ans;

int main(void) {
	scanf("%d%d", &n, &m);
	lmt = std::min(n, m);
	for (int i = lmt; i; --i) {
		f[i] = (long long)(n / i) * (long long)(m / i);
		for (int j = i << 1; j <= lmt; j += i) {
			f[i] -= f[j];
		}
		ans += f[i] * ((i << 1) - 1);
	}
	printf("%lld\n", ans);
	return 0;
}

  

posted @ 2017-02-05 19:39  ciao_sora  阅读(...)  评论(...编辑  收藏