[bzoj2301][HAOI2011]Problem b

题目大意:给你$a,b,c,d,k(1\leqslant a,b,c,d,k\leqslant 5\times10^4)$,求$\displaystyle\sum\limits_{x=a}^b\displaystyle\sum\limits_{y=c}^d[(x,y)==k]$

题解:下文中令$n\leqslant m$先考虑求$\sum\limits_{i=1}^n\sum\limits_{j=1}^m[(i,j)==k]$
$$
\begin{align*}
\def\dsum{\displaystyle\sum\limits}
令f(p)&=\dsum_{i=1}^n\dsum_{j=1}^m[(i,j)==p]\\
令F(p)&=\dsum_{p|k}f(k)\\
    &=\dsum_{p|k}\dsum_{i=1}^n\dsum_{j=1}^m[(i,j)==k]\\
    &=\dsum_{i=1}^n\dsum_{j=1}^m[p|(i,j)]\\
    &=\left\lfloor\dfrac{n}{p}\right\rfloor\cdot \left\lfloor\dfrac{m}{p}\right\rfloor\\
莫比乌&斯反演得:\\
\therefore f(p)&=\dsum_{p|k}\mu\Big(\dfrac{k}{p}\Big)F(k)\\
            &=\dsum_{i=1}^n\mu(i)\left\lfloor\dfrac{n}{ip}\right\rfloor\cdot\left\lfloor\dfrac{m}{ip}\right\rfloor\\
\end{align*}\\
令g(p)=\dsum_{i=1}^p\mu(i)\\
然后容斥一下就好了\\
$$

卡点:


C++ Code:

#include <cstdio>
#define maxn 50010 
using namespace std;
int miu[maxn], plist[maxn], ptot;
bool isp[maxn];
void sieve(int n) {
	miu[1] = 1;
	for (int i = 2; i <= n; i++) {
		if (!isp[i]) {
			miu[i] = -1;
			plist[ptot++] = i;
		}
		for (int j = 0; j < ptot, i * plist[j] <= n; j++) {
			int tmp = i * plist[j];
			isp[tmp] = true;
			if (i % plist[j] == 0) {
				miu[tmp] = 0;
				break;
			}
			miu[tmp] = -miu[i];
		}
	}
	for (int i = 2; i <= n; i++) miu[i] += miu[i - 1];
}
inline int min(int a, int b) {return a < b ? a : b;}
int solve(int n, int m, int k) {
	n /= k, m /= k;
	int tmp = min(n, m);
	int ans = 0, l, r;
	for (l = 1; l <= tmp; l = r + 1) {
		r = min(n / (n / l), m / (m / l));
		ans += (miu[r] - miu[l - 1]) * (n / l) * (m / l);
	}
	return ans;
}
int Tim, a, b, c, d, k;
int main() {
	sieve(50000);
	scanf("%d", &Tim);
	while (Tim --> 0) {
		scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
		printf("%d\n", solve(b, d, k) - solve(a - 1, d, k) - solve(b, c - 1, k) + solve(a - 1, c - 1, k));
	}
	return 0;
}

 

posted @ 2018-08-23 15:17  Memory_of_winter  阅读(...)  评论(...编辑  收藏