gcd求和问题(莫比乌斯反演)

​​​​​​\sum_{i=1}^N \sum_{j=1}^M [gcd(i,j)==k]

P2522 [HAOI2011]Problem b

P3455 [POI2007]ZAP-Queries

1、设  f_d=\sum_{i=1}^N \sum_{j=1}^M [\gcd(i,j)==d],\ F_d=\sum_{i=1}^N\sum_{j=1}^M[d|\gcd(i,j)]

2、那么有 F_d=\sum_{d|n}f_n, 通过枚举 d 可以将式子 F_d=\sum_{i=1}^N\sum_{j=1}^M[d|\gcd(i,j)] 化简到 F_d= \lfloor \frac{N}{d} \rfloor \lfloor \frac{M}{d} \rfloor

3、通过莫比乌斯反演,可以得到 f_d=\sum_{d|n}\mu(\frac{n}{d})F_n,将 F_n 化掉得到式子 f_d=\sum_{d|n}\mu(\frac{n}{d})\lfloor \frac{N}{n} \rfloor \lfloor \frac{M}{n} \rfloor

4、令 k*d=n, f_d=\sum_{k=1}^{n/d}\mu(k)\lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor, 然后使用整除优化,询问时间复杂度 O(\sqrt n)

P3455 [POI2007]ZAP-Queries   \sum_{i=1}^N \sum_{j=1}^M [gcd(i,j)==k]

    for (int i = 1; i < MAXN; i++) {
		mu[i] = (mu[i] + mu[i - 1]);
		phi[i] = (phi[i] + phi[i - 1]);
	}
}
int main()
{
	jzk();
	int T;
	sc("%d", &T);
	while(T--)
	{
		ll n, m, d;
		sc("%lld%lld%lld", &n, &m, &d);
		ll ans = 0, j;
		ll minn = min(n, m);
		for (ll i = 1; i <= minn; i = j + 1)
		{
			j = min(n / (n / i), m / (m / i));
			ans += (mu[j] - mu[i - 1]) * (n / i / d) * (m / i / d);
		}
		pr("%lld\n", ans);
	}
}

 

\sum_{i=1}^N \sum_{j=1}^M gcd(i,j)

P1447 [NOI2010]能量采集

1、设  f_d=\sum_{i=1}^N \sum_{j=1}^M d[\gcd(i,j)==d],\ F_d=\sum_{i=1}^N\sum_{j=1}^M d[d|\gcd(i,j)]

2、那么有 F_d=\sum_{d|n}f_n\ \ [n=\min(N,M)], 通过枚举 d 可以将式子 F_d=\sum_{i=1}^N\sum_{j=1}^M d[d|\gcd(i,j)] 化简到 F_d= \lfloor \frac{N}{d} \rfloor \lfloor \frac{M}{d} \rfloor d

3、通过莫比乌斯反演,可以得到 f_d=\sum_{d|n}\mu(\frac{n}{d})F_n,将 F_n 化掉得到式子 f_d=\sum_{d|n}\mu(\frac{n}{d})\lfloor \frac{N}{n} \rfloor \lfloor \frac{M}{n} \rfloor d

4、令 k*d=nf_d=\sum_{k=1}^{n/d} \mu(k) \lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor d

5、所以原式 = \sum_{d=1}^{n}(\sum_{k=1}^{n/d}\lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor *\sum_{k=1}^{n/d} \mu(k) d), 由 \frac{\phi_n}{n}=\sum_{d|n}\frac{\mu_d}{d}, 可以得到  \sum_{k|n} \mu(k)*\frac{n}{k}=\phi_n

所以原式 =  \sum_{k*d=1}^{n}\lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor \phi_{(k*d)}

6、令 T=k*d, 得到  \sum_{T=1}^{n}\lfloor \frac{N}{T} \rfloor \lfloor \frac{M}{T} \rfloor \phi_T, 然后使用整除优化,询问时间复杂度 O(\sqrt n)

P1447 [NOI2010]能量采集     \sum_{i=1}^N\sum_{j=1}^M 2*\gcd(i,j)-1

	for (int i = 1; i < MAXN; i++) {
		mu[i] = (mu[i] + mu[i - 1]);
		phi[i] = (phi[i] + phi[i - 1]);
	}
}
int main()
{
	jzk();
	ll n, m, d;
	sc("%lld%lld", &n, &m);
	ll ans = 0, j;
	ll minn = min(n, m);
	for (ll i = 1; i <= minn; i = j + 1)
	{
		j = min(n / (n / i), m / (m / i));
		ans += (phi[j] - phi[i - 1]) * (n / i) * (m / i);
	}
	pr("%lld\n", ans * 2 - n * m);
}

 

\sum_{i=1}^N \sum_{j=1}^M [gcd(i,j)\in prime]

P2257 YY的GCD

P2568 GCD

1、设  f_d=\sum_{i=1}^N \sum_{j=1}^M d[\gcd(i,j)==d],\ F_d=\sum_{i=1}^N\sum_{j=1}^M d[d|\gcd(i,j)]

2、那么有 F_d=\sum_{d|n}f_n\ \ [n=\min(N,M)], 通过枚举 d 可以将式子 F_d=\sum_{i=1}^N\sum_{j=1}^M d[d|\gcd(i,j)] 化简到 F_d= \lfloor \frac{N}{d} \rfloor \lfloor \frac{M}{d} \rfloor d

3、通过莫比乌斯反演,可以得到 f_d=\sum_{d|n}\mu(\frac{n}{d})F_n,将 F_n 化掉得到式子 f_d=\sum_{d|n}\mu(\frac{n}{d})\lfloor \frac{N}{n} \rfloor \lfloor \frac{M}{n} \rfloor d

4、令 k*d=nf_d=\sum_{k=1}^{n/d} \mu(k) \lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor d

5、所以原式  =\sum_{d=1,d \in prime}^{n}\lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor \sum_{k=1}^{n/d} \mu(k) d

 

6、令 T=k*d, 反向枚举 T 得到  \sum_{T=1,d\in prime}^{n}\lfloor \frac{N}{T} \rfloor \lfloor \frac{M}{T} \rfloor \sum_{k=1}^{T/d} \mu(k) d, 注意,这里还有一个要求 d\in prime

7、令 g_n=\sum_{k=1}^{T/d}\mu(k)d, 我们考虑枚举质数 d 的取值,然后对与每一个满足 d*k<=n 的 kg[k*d]+=\mu(k)*d,即可在 O(nlogn) 的时间预处理出 g_n,得到式子 \sum_{T=1}^{n}\lfloor \frac{N}{T} \rfloor \lfloor \frac{M}{T} \rfloor g_T,然后使用整除优化,询问时间复杂度 O(\sqrt n)

P2257 YY的GCD   \sum_{i=1}^N \sum_{j=1}^M [gcd(i,j)\in prime]

    for (int i = 0; i < tot; i++)
		for (int j = prime[i], cnt = 1; j <= 1e7; j += prime[i], cnt++)
			g[j] += mu[cnt];
	for (int i = 2; i < MAXN; i++)
		g[i] += g[i - 1];
}
int main()
{
	jzk();
	int T;
	sc("%d", &T);
	while (T--)
	{
		ll n, m;
		sc("%lld%lld", &n, &m);
		ll minn = min(n, m);
		ll ans = 0;
		int j = 0;
		for (int i = 1; i <= minn; i = j + 1)
		{
			j = min(n / (n / i), m / (m / i));
			ans += (n / i) * (m / i) * (g[j] - g[i - 1]);
		}
		pr("%lld\n", ans);
	}
}

 

\sum_{i=1}^N\sum_{j=1}^M lcm(i,j)       \sum_{i=1}^N\sum_{j=1}^M i*j*\gcd(i,j)

P1829 [国家集训队]Crash的数字表格

P3768 简单的数学题 (需要用到杜教筛)

以第一个式子为例:

1、将式子的 lcm 拆成 gcd 得到 \sum_{i=1}^N\sum_{j=1}^M \frac{i*j}{\gcd(i,j)}, 设  f_d=\sum_{i=1}^N \sum_{j=1}^M \frac{i*j}{d}[\gcd(i,j)==d],\ F_d=\sum_{i=1}^N\sum_{j=1}^M \frac{i*j}{d}[d|\gcd(i,j)]

2、那么有 F_d=\sum_{d|n}f_n\ \ [n=\min(N,M)], 通过枚举 d 可以将式子 F_d=\sum_{i=1}^N\sum_{j=1}^M \frac{i*j}{d}[d|\gcd(i,j)] 化简到 F_d= \sum_{i=1,d|i}^N i \sum_{j=1,d|j}^M j\ \frac{1}{d} 考虑将 i,j 同时缩小 d 倍,那么可以将式子化简到 F_d= \sum_{i=1}^{N/d} i \sum_{j=1}^{M/d} j\ d^2 *\frac{1}{d} 

(由于我在第一步中定义函数 F 中重复使用了变量名 d,所以上面这个式子的 d 的意义是不一样的,\frac{1}{d} 中的 d 表示的是最大公因数,d^2 中的 d 表示的是函数的自变量)

3、通过莫比乌斯反演,可以得到 f_d=\sum_{d|n}\mu(\frac{n}{d})F_n,将 F_n 化掉得到式子 f_d=\sum_{d|n}\mu(\frac{n}{d}) \sum_{i=1}^{N/k/d} i \sum_{j=1}^{M/k/d} j\ n^2 *\frac{1}{d}

4、令 k*d=nf_d=\sum_{k=1}^{n/d} \mu(k) \sum_{i=1}^{N/k/d} i \sum_{j=1}^{M/k/d} j\ k^2d^2*\frac{1}{d}

5、所以原式  =\sum_{d=1}^n\sum_{k=1}^{n/d} \mu(k) \sum_{i=1}^{N/k/d} i \sum_{j=1}^{M/k/d} j\ k^2d^2*\frac{1}{d},化简得到 \sum_{d=1}^n d\sum_{k=1}^{n/d} k^2 \mu(k) \sum_{i=1}^{N/k/d} i \sum_{j=1}^{M/k/d} j

6、所以使用两次整除分块就可以算出上述式子,询问时间复杂度 O(n)

P1829 [国家集训队]Crash的数字表格   \sum_{i=1}^N\sum_{j=1}^M lcm(i,j)

    for (int i = 1; i <= MAXN; i++) {
		mu[i] = mu[i] * i % mod * i % mod + mu[i - 1];
		phi[i] += phi[i - 1];
	}
}
ll calc(ll n)
{
	return (n * (n + 1) / 2) % mod;
}
ll func(ll n, ll m)
{
	ll ans = 0, j;
	ll minn = min(m, n);
	for (int i = 1; i <= minn; i = j + 1)
	{
		j = min(n / (n / i), m / (m / i));
		ans = (ans + (mu[j] - mu[i - 1] + mod) * calc(n / i) % mod * calc(m / i) % mod) % mod;
	}
	return ans;
}
int main()
{
	jzk();
	ll n, m;
	sc("%lld%lld", &n, &m);
	ll ans = 0, j;
	ll minn = min(n, m);
	for (int i = 1; i <= minn; i = j + 1)
	{
		j = min(n / (n / i), m / (m / i));
		ans = (ans + 1LL * (calc(j) - calc(i - 1) + mod) * func(n / i, m / i)) % mod;
	}
	pr("%lld\n", ans);
}

 

\sum_{i=1}^N\sum_{j=1}^Md(ij)     d_x 表示 x 的约数个数

P3327 [SDOI2015]约数个数和

1、\sum_{i=1}^N\sum_{j=1}^Md(ij) 化简得到 \sum_{i=1}^N\sum_{j=1}^M\sum_{x|i}\sum_{y|j}[\gcd(x,y)==d], 枚举 x,y 得到 \sum_{i=1}^N\sum_{j=1}^M\sum_{i|x}\sum_{j|y} [\gcd(i,j)==d]

2、发现最后两个求和符号可以变成公式,所以得到  \sum_{i=1}^N\sum_{j=1}^M\lfloor\frac{N}{i}\rfloor \lfloor\frac{M}{j}\rfloor [\gcd(i,j)==d]

3、设  f_d=\sum_{i=1}^N \sum_{j=1}^M \lfloor\frac{N}{i}\rfloor \lfloor\frac{M}{j}\rfloor [\gcd(i,j)==d],\ F_d=\sum_{i=1}^N\sum_{j=1}^M \lfloor\frac{N}{i}\rfloor \lfloor\frac{M}{j}\rfloor [d|\gcd(i,j)]

4、那么有 F_d=\sum_{d|n}f_n, 通过枚举 d 可以将式子 F_d=\sum_{i=1}^N \lfloor\frac{N}{i}\rfloor \sum_{j=1}^M \lfloor\frac{M}{j}\rfloor [d|\gcd(i,j)] 化简得到  F_d=\sum_{i=1}^{N/d} \lfloor\frac{N/d}{i}\rfloor \sum_{j=1}^{M/d} \lfloor\frac{M/d}{j}\rfloor

5、通过莫比乌斯反演,可以得到 f_d=\sum_{d|n}\mu(\frac{n}{d})F_n,将 F_n 化掉得到式子 f_d=\sum_{d|n}\mu(\frac{n}{d})\sum_{i=1}^{N/n} \lfloor\frac{N/n}{i}\rfloor \sum_{j=1}^{M/n} \lfloor\frac{M/n}{j}\rfloor

6、所以原式 f_1=\sum_{1|n}\mu(\frac{n}{1})\sum_{i=1}^{N/n} \lfloor\frac{N/n}{i}\rfloor \sum_{j=1}^{M/n} \lfloor\frac{M/n}{j}\rfloor, 化简得到 \sum_{T=1}^n\mu(n)\sum_{i=1}^{N/T} \lfloor\frac{N/T}{i}\rfloor \sum_{j=1}^{M/T} \lfloor\frac{M/T}{j}\rfloor

7、令 g(x)=\sum_{i=1}^k \lfloor \frac{k}{i} \rfloor, 所以得到 \sum_{T=1}^n\mu(n) g(\frac{N}{T}) g(\frac{M}{T}),一种朴素的方法是,对于每一个 i,使用整除分块来处理,预处理复杂度 O(nlogn),不过也存在线性的解法,对于每一个 i,求出 i 的倍数的个数,所以 g(x) 实际上就是 g(x)=\sum_{i=1}^k d(i), 其中 d(i) 表示约数个数,由于函数 d 是积性函数,可以线性筛出,预处理复杂度 O(n)

8、然后整除分块,就可以求出 \sum_{T=1}^n\mu(n) g(\frac{N}{T}) g(\frac{M}{T}), 询问时间复杂度 O(\sqrt n)

P3327 [SDOI2015]约数个数和   \sum_{i=1}^N\sum_{j=1}^Md(ij)

	for (int i = 1; i < MAXN; i++) {
		mu[i] = (mu[i] + mu[i - 1]);
		phi[i] = (phi[i] + phi[i - 1]);
	}
	for (int i = 1; i < MAXN; i++)
	{
		ll ans = 0, j;
		for (int k = 1; k <= i; k = j + 1)
		{
			j = i / (i / k);
			ans += (j - k + 1) * (i / k);
		}
		sub[i] = ans;
	}
}
int main()
{
	jzk();
	int T;
	sc("%d", &T);
	while (T--)
	{
		ll n, m, d;
		sc("%lld%lld", &n, &m);
		ll ans = 0, j;
		ll minn = min(n, m);
		for (ll i = 1; i <= minn; i = j + 1)
		{
			j = min(n / (n / i), m / (m / i));
			ans += (mu[j] - mu[i - 1]) * sub[n / i] * sub[m / i];
		}
		pr("%lld\n", ans);//gcd
	}
}

 

\prod _{i=1}^N\prod _{j=1}^M fib(\gcd(i,j))

P3704 [SDOI2017]数字表格

1、先将式子化简,考虑枚举 \gcd(i,j),化简得到 \prod_{i=d}^n fib(d)^{\sum_{i=1}^N\sum_{j=1}^M[\gcd(i,j)==d]},显然指数的式子可以化简成 \prod_{d=1}^n fib(d)^{\sum_{k=1}^{n/d}\mu(k)\lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor}

2、考虑指数的加法就是底数的乘法,所以进一步化简得到 \prod_{d=1}^n\prod_{k=1}^{n/d} fib(d)^{\mu(k)\lfloor \frac{N/d}{k} \rfloor \lfloor \frac{M/d}{k} \rfloor}

3、令 T=k*d,考虑直接枚举 T 得到 \prod_{T=1}^n({\prod_{k|T} fib(d)^{\mu(\frac{T}{k})})^{\lfloor \frac{N}{T} \rfloor \lfloor \frac{M}{T} \rfloor}},考虑将括号里面作为一个整体,因为我们知道,函数 \mu 只有三个取值 -1,0,1,所以这个次方只需要预处理 fib 和 他的逆元即可,然后暴力枚举括号里面的式子,再整除分块即可。

4、有一个小优化,由于 mod 是质数,所以 a^{mod-1}=1 ,所以可以在整除分块的时候让指数对 mod-1 取模(时间差了一倍),询问时间复杂度 O(\sqrt n *log ^{mod})

P3704 [SDOI2017]数字表格  \prod _{i=1}^N\prod _{j=1}^M fib(\gcd(i,j))

	fib[0] = 0, fib[1] = 1;
	inv[0] = 0, inv[1] = 1;
	for (int i = 2; i < MAXN; i++)
	{
		fib[i] = (fib[i - 1] + fib[i - 2]) % mod;
		inv[i] = power(fib[i], mod - 2) % mod;
	}
	for (int i = 0; i < MAXN; i++)
		sub[i] = 1;
	for (ll d = 1; d < MAXN; d++)
	{
		if (mu[d] == 0)
			continue;
		for (ll T = d, k = 1; T < MAXN; T += d, k++)
		{
			if (mu[d] == 1)
				sub[T] = (sub[T] * fib[k]) % mod;
			else if (mu[d] == -1)
				sub[T] = (sub[T] * inv[k]) % mod;
		}
	}
	inv[0] = power(sub[0], mod - 2);
	for (int i = 1; i < MAXN; i++)
	{
		sub[i] = (sub[i] * sub[i - 1]) % mod;
		inv[i] = power(sub[i], mod - 2);
	}
}
int main()
{
	jzk();
	int T;
	sc("%d", &T);
	while (T--)
	{
		ll n, m;
		sc("%lld%lld", &n, &m);
		ll ans = 1, j;
		ll minn = min(n, m);
		for (int i = 1; i <= minn; i = j + 1)
		{
			j = min(n / (n / i), m / (m / i));
			ans = (ans * power(sub[j] * inv[i - 1] % mod, (n / i) * (m / i) % (mod - 1))) % mod;
		}
		pr("%lld\n", ans);
	}
}

参考博客链接:

 https://blog.csdn.net/fo0old/article/details/82020110

https://blog.csdn.net/jk_chen_acmer/article/details/82708274

莫比乌斯反演公式证明:https://blog.csdn.net/litble/article/details/72804050

分块优化证明:https://blog.csdn.net/outer_form/article/details/50590197

贼详细的各种反演和解释:https://www.luogu.org/blog/An-Amazing-Blog/mu-bi-wu-si-fan-yan-ji-ge-ji-miao-di-dong-xi

你可能感兴趣的:(数论,洛谷)