[POI2007] ZAP-Queries solution

Front Cheese

\ (\ epsilon (n) = [n = 1] \)
\ (1 * \ mu = \ epsilon \)


Solution
(in order to avoid duplicate names variables I put the title of \ (d \) into \ (D \) a -_- || )

Let \ (n = \ lfloor \ frac {a} D \ rfloor \) , \ (m = \ lfloor \ frac {b} D \ rfloor \) ,
then go to the routine

\[\sum_{i=1}^a \sum_{j=1}^b [gcd(i,j)=D] \]

\[=\sum_{i=1}^n \sum_{j=1}^m [gcd(i,j)=1] \]

\[=\sum_{i=1}^n \sum_{j=1}^m \epsilon(gcd(i,j)) \]

\[=\sum_{i=1}^n \sum_{j=1}^m \sum_{d|gcd(i,j)} \mu(d) \]

Since \ (d | gcd (i, j) \) , \ (d \) is the common divisor of \ (i \) and \ (j \) .
The count of each formula \ (I \) and \ (J \) divisor \ (\ MU \) and the sum of each \ (D \) is counted as the divisor \ (\ lfloor \ frac {n} {d} \ rfloor \ lfloor \ frac {m} {d} \ rfloor \) times, so the above formula becomes

\[\sum_{d=1}^{min(n,m)} \mu(d) \lfloor \frac{n}{d} \rfloor \lfloor \frac{m}{d} \rfloor \]

The range of \ (a \) and \ (b \) is very small, you can \ (O (n) \) pre-process \ (\ mu \) , and then divide the block to calculate this formula, the complexity of single calculation It is \ (O (\ sqrt n) \) . Due to \ (n \) queries, the complexity of \ (O (n \ sqrt n) \) can be passed in the time limit of \ (2s \) .

Standard inversion reasoning will be supplemented.

Luogu data AC code:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 50005;
#define int long long

int m, prime[maxn], v[maxn], mu[maxn];
void euler(int n) {
	mu[1] = 1;
	for(int i=2; i<=n; ++i) {
		if(!v[i]) {
			v[prime[++m] = i] = i;
			mu[i] = -1;
		}
		for(int j=1; j<=m; ++j) {
			if(prime[j] > n/i || prime[j] > v[i]) break;
			v[prime[j] * i] = prime[j];
			mu[prime[j] * i] = mu[i] * (i%prime[j] ? -1 : 0);
		}
	}
}

long long sol(int n, int m) {
	int len = min(n, m);
	long long res = 0ll;
	for(int i=1,j; i<=len; i=j+1) {
		j = min(n/(n/i), m/(m/i));
		j = min(j, len);
		res += (mu[j]-mu[i-1]) * (n/i) * (m/i);
	}
	return res;
}

signed main()
{
	euler(50000);
	for(int i=1; i<=50000; ++i) mu[i] += mu[i-1];
	int n; cin >> n; while(n--) {
		int a, b, d;
		scanf("%lld%lld%lld", &a, &b, &d);
		cout << sol(a/d, b/d) << '\n';
	}
	return 0;
}

Guess you like

Origin www.cnblogs.com/tztqwq/p/12759009.html