[BZOJ3994] [SDOI2015] 约数个数和 [莫比乌斯反演][除法分块]

Link
https://www.lydsy.com/JudgeOnline/problem.php?id=3994


Description
T \mathrm{T} T 组数据,每一组数据给出 n , m n,m n,m ,求 ∑ i = 1 n ∑ j = 1 m d ( i j ) \sum\limits_{i=1}^n\sum\limits_{j=1}^md(ij) i=1nj=1md(ij)
1 ≤ n , m , T ≤ 5 × 1 0 4 1\le n,m,\mathrm{T}\le 5\times10^4 1n,m,T5×104


Analysis
约数个数和显然可以变一下,得到
∑ i = 1 n ∑ j = 1 m d ( i j ) = ∑ i = 1 n ∑ j = 1 m ∏ k = 1 ω ( i j ) ( a k − 1 ) \begin{array}{rcl} \sum\limits_{i=1}^n\sum\limits_{j=1}^md(ij)&=&\sum\limits_{i=1}^n\sum\limits_{j=1}^m\prod\limits_{k=1}^{\omega(ij)}(a_k-1) \end{array} i=1nj=1md(ij)=i=1nj=1mk=1ω(ij)(ak1)
但是这个没有什么用,我们考虑变求约数个数和为考虑每个数作为约数的贡献
为了方便下面我就当 n = min ⁡ { n , m } n=\min\{n,m\} n=min{n,m}
∑ i = 1 n ∑ j = 1 m d ( i j ) = ∑ i = 1 n ∑ j = 1 m [ ( i , j ) = 1 ] ⌊ n i ⌋ ⌊ m j ⌋ = ∑ i = 1 n ∑ j = 1 m ϵ ( ( i , j ) ) ⌊ n i ⌋ ⌊ m j ⌋ = ∑ i = 1 n ∑ j = 1 m ∑ d ∣ ( i , j ) μ ( d ) ⌊ n i ⌋ ⌊ m j ⌋ = ∑ d = 1 n μ ( d ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j = ∑ d = 1 n μ ( d ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ ⌊ n d i ⌋ ⌊ m d j ⌋ = ∑ d = 1 n μ ( d ) ( ∑ i = 1 ⌊ n d ⌋ ⌊ ⌊ n d ⌋ i ⌋ ⋅ ∑ j = 1 ⌊ m d ⌋ ⌊ ⌊ m d ⌋ j ⌋ ) \begin{array}{rcl} \sum\limits_{i=1}^n\sum\limits_{j=1}^md(ij)&=&\sum\limits_{i=1}^n\sum\limits_{j=1}^m[(i,j)=1]\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{j}\rfloor\\ &=&\sum\limits_{i=1}^n\sum\limits_{j=1}^m\epsilon((i,j))\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{j}\rfloor\\ &=&\sum\limits_{i=1}^n\sum\limits_{j=1}^m\sum\limits_{d|(i,j)}\mu(d)\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{j}\rfloor\\ &=&\sum\limits_{d=1}^n\mu(d)\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}ij\\ &=&\sum\limits_{d=1}^n\mu(d)\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}\lfloor\frac{n}{di}\rfloor\lfloor\frac{m}{dj}\rfloor\\ &=&\sum\limits_{d=1}^n\mu(d)\left(\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\left\lfloor\frac{\lfloor\frac{n}{d}\rfloor}{i}\right\rfloor\cdot\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}\left\lfloor\frac{\lfloor\frac{m}{d}\rfloor}{j}\right\rfloor\right)\\ \end{array} i=1nj=1md(ij)======i=1nj=1m[(i,j)=1]injmi=1nj=1mϵ((i,j))injmi=1nj=1md(i,j)μ(d)injmd=1nμ(d)i=1dnj=1dmijd=1nμ(d)i=1dnj=1dmdindjmd=1nμ(d)(i=1dnidnj=1dmjdm)
预处理 f ( x ) = ∑ i = 1 x ⌊ x i ⌋ f(x)=\sum\limits_{i=1}^x\lfloor\frac{x}{i}\rfloor f(x)=i=1xix ,然后对每个询问再整除分块即可。 O ( m m + T m ) O(m\sqrt{m}+\mathrm{T}\sqrt{m}) O(mm +Tm )


这该死的莫反竟然如此的套路
oier平均一人一道约数个数和 noip出莫反裸题指日可待


#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 5e4+5;
int T, n, m;
int mu[MAXN], smu[MAXN];
long long f[MAXN];
bool NotPrime[MAXN];
int PrimeList[MAXN];
int Diven[MAXN];
void Init()
{
	const int n = 5e4;
	for (register int i = 1; i <= n; ++i)
	{
		for (register int t, L = 1, R; L <= i; L = R + 1)
		{
			R = i / (t = i / L);
			f[i] += (R - L + 1) * t;
		}
	}
	NotPrime[0] = NotPrime[1] = 1;
	mu[1] = 1;
	for (register int i = 2; i <= n; ++i)
	{
		if (!NotPrime[i]) PrimeList[++PrimeList[0]] = i, Diven[PrimeList[0]] = n / i, mu[i] = -1;
		for (register int t, j = 1; j <= PrimeList[0] && i <= Diven[j]; ++j)
		{
			NotPrime[t = i * PrimeList[j]] = 1;
			if (i % PrimeList[j] == 0)
			{
				mu[t] = 0;
				break;
			}
			mu[t] = -mu[i];
		}
	}
	for (register int i = 1; i <= n; ++i)
	{
		smu[i] = smu[i-1] + mu[i];
	}
}
#define getchar() (frS==frT&&(frT=(frS=frBB)+fread(frBB,1,1<<12,stdin),frS==frT)?EOF:*frS++)
char frBB[1<<12], *frS=frBB, *frT=frBB;
inline int read(int& x)
{
	x=0;char ch=getchar();bool w=0;
	while(!isdigit(ch))w|=(ch=='-'),ch=getchar();
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	w?(x=-x):0;
}
int main()
{
	Init();
	read(T);
	register int fafa;
	register long long Ans;
	while (T--)
	{
		read(n); read(m);
		fafa = (n - m) >> 31;
		fafa = n & fafa | m & ~fafa;
		Ans = 0;
		for (register int tn, tm, L = 1, R; L <= fafa; L = R + 1)
		{
			R = min(n/(tn=n/L),m/(tm=m/L));
			Ans += f[tn] * f[tm] * (smu[R] - smu[L-1]);
		}
		printf("%lld\n", Ans);
	}
	return 0;
}

你可能感兴趣的:(BZOJ,数论分块,数论)