码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ 2820】YY的GCD

时间:2016-04-23 19:47:46      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

线性筛积性函数$g(x)$,具体看Yveh的题解:

http://sr16.com:8081/%e3%80%90bzoj2820%e3%80%91yy%e7%9a%84gcd/

#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x=getint()
using namespace std;
const int N = 1E7 + 3;
int getint() {
	int k = 0, fh = 1; char c = getchar();
	for(; c < ‘0‘ || c > ‘9‘; c = getchar())
		if (c == ‘-‘) fh = -1;
	for(; c >= ‘0‘ && c <= ‘9‘; c = getchar())
		k = k * 10 + c - ‘0‘;
	return k * fh;
}
bool np[N];
int g[N], mu[N], prime[N], sum[N];
void shai() {
	memset(np, 0, sizeof(np));
	mu[1] = 1; g[1] = 0; sum[1] = 0; int num = 0;
	for(int i = 2; i <= 1E7; ++i) {
		if (!np[i]) {prime[++num] = i; mu[i] = - 1; g[i] = 1;}
		for(int j = 1; j <= num; ++j) {
			if (prime[j] * i > 1E7) break;
			np[prime[j] * i] = 1;
			if (i % prime[j] == 0) {
				mu[prime[j] * i] = 0;
				g[prime[j] * i] = mu[i];
				break;
			}
			mu[prime[j] * i] = - mu[i];
			g[prime[j] * i] = mu[i] - g[i];
		}
		sum[i] = sum[i - 1] + g[i];
	}
}
int main() {
	shai();
	long long ret;
	int t, n, m;
	read(t);
	while (t--) {
		read(n); read(m);
		if (n > m) swap(n, m);
		ret = 0;
		for(int i = 1, la = 1; i <= n; i = la + 1) {
			la = min(n / (n / i), m / (m / i));
			ret += (long long) (sum[la] - sum[i - 1]) * (n / i) * (m / i);
		}
		printf("%lld\n", ret);
	}
	return 0;
}

我确实弱==

【BZOJ 2820】YY的GCD

标签:

原文地址:http://www.cnblogs.com/abclzr/p/5425319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!