[POI2007]ZAP-Queries && [HAOI2011]Problem b 莫比乌斯反演

时间:2022-09-23 00:13:15

1,[POI2007]ZAP-Queries

~~~题面~~~
题解: 首先列出式子:$$ans = \sum_{i = 1}^{n}\sum_{j = 1}^{m}[gcd(i, j) == d]$$
    $$[gcd(i, j) == d] = [gcd(\lfloor{\frac{i}{d}}\rfloor,\lfloor{\frac{j}{d}}\rfloor) == 1]$$
    所以原式 $$\Rightarrow \quad \sum_{i = 1}^{\lfloor{\frac{n}{d}}\rfloor}\sum_{j = 1}^{\lfloor{\frac{m}{d}}\rfloor}[gcd(i, j)==1]$$
    $$\Rightarrow \quad \sum_{i = 1}^{n}\sum_{j = 1}^{m}\sum_{d|gcd(i, j)}\mu(d)$$
    因为$\mu(d)$会被统计到当且仅当$d | i \quad and \quad d | j$,即$d | gcd(i, j)$
    那么考虑将满足条件的i和j两两搭配,组成的方案数就是$\mu(d)$会被统计到的次数,
    也就是$\mu(d)$会被统计到$\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor$次
    $$\Rightarrow \quad ans=\sum_{i=1}^{min(n,m)}{\mu(d)\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor}$$
    然后观察到$\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor$中有很多小段$\lfloor{\frac{n}{d}}\rfloor\lfloor{\frac{m}{d}}\rfloor$乘积是固定的,也就是$\lfloor{\frac{n}{d}}\rfloor$和$\lfloor{\frac{m}{d}}\rfloor$同时为一个固定的值,因此我们可以用整数分块优化

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 55000
int t, n, m, d;
int prime[AC], mu[AC], sum[AC], tot;
bool z[AC]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void getprime()
{
int now;
mu[] = ;
for(R i = ; i <= ; i++)
{
if(!z[i]) prime[++tot] = i, mu[i] = -;
for(R j = ; j <= tot; j++)
{
now = prime[j];
if(now * i > ) break;
// printf("!!!%d %d\n", now, i);
z[now * i] = true;
if(!(i % now)) break;
mu[i * now] = -mu[i];
}
}
for(R i = ; i <= ; i++)
sum[i] = mu[i] + sum[i - ];
} int ans; void work()
{
int pos;
t = read();
for(R i = ; i <= t; i++)
{
n = read(), m = read(), d = read();
n /= d, m /= d;
int b = min(n, m);
ans = ;
for(R j = ; j <= b; j = pos + )
{
pos = min(n / (n / j), m / (m / j));
ans += (sum[pos] - sum[j-]) * (n / j) * (m / j);
}
printf("%d\n", ans);
}
} int main()
{
// freopen("in.in", "r", stdin);
getprime();
work();
// fclose(stdin);
return ;
}

2,[HAOI2011]Problem b

~~~题面~~~
题解: 这题相比上题多出了两个限制条件,同时对上下限都有限制,那么此时可以用一个容斥来求。
    设$cal(n,m)$表示满足$x \le n$和$y \le m$且$gcd(x, y) = d$的点对个数,
    那么$$ans = cal(b, d) - cal(a - 1, d) - cal(b, c - 1) + cal(a - 1, c - 1);$$

 #include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 55000
#define LL long long
int a, b, c, d, tot, k, t, ans;
int prime[AC], mu[AC], sum[AC];
bool z[AC]; inline int read()
{
int x = ; char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void pre()
{
int now;
mu[] = ;
for(R i = ; i <= ; i++)
{
if(!z[i]) prime[++tot] = i, mu[i] = -;//质数的mu值肯定都是-1
for(R j = ; j <= tot; j++)
{
now = prime[j];
if(now * i > ) break;
z[now * i] = true;
if(!(i % now)) break;
mu[i * now] = -mu[i];//error这里的mu是由mu[i]得来的啊!!!
}
}
for(R i = ; i <= ; i++) sum[i] = sum[i - ] + mu[i];
} int cal(int n, int m)
{
n /= k, m /= k;
int b = min(n, m), pos, rnt = ;
for(R i = ; i <= b; i = pos + )
{
pos = min(n / (n / i), m / (m / i));
rnt += (sum[pos] - sum[i-]) * (n / i) * (m / i);
}
return rnt;
}
//ans = cal(b, d) - cal(a - 1, d) - cal(b, c - 1) + cal(a - 1 , c - 1),相当于容斥 void work()
{
t = read();
for(R i = ; i <= t; i++)
{
a = read(), b = read(), c = read(), d = read(), k = read();
ans = cal(b, d) - cal(a - , d) - cal(b, c - ) + cal(a - , c - );
printf("%d\n", ans);
}
} int main()
{
// freopen("in.in", "r", stdin);
pre();
work();
// fclose(stdin);
return ;
}