#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 10;
bool no_prime[maxn];
int prime[maxn], mu[maxn], pre[maxn];
int shai(int n)
{
int cnt = 0;
mu[1] = 1;
for (int i = 2; i <= n; i++)
{
if (!no_prime[i])
prime[++cnt] = i, mu[i] = -1;
for (int j = 1; j <= cnt && prime[j] * i <= n; j++)
{
no_prime[prime[j] * i] = 1;
mu[prime[j] * i] = i % prime[j] == 0 ? 0 : -mu[i];
if (i % prime[j] == 0) break;
}
}
for (int i = 1; i <= n; i++)
pre[i] = pre[i - 1] + mu[i];
return cnt;
}
long long cal(int n, int m)
{
long long ans = 0;
int l = 1, r;
while (l <= m)
{
r = min(n / (n / l), m / (m / l));
ans += (long long)(pre[r] - pre[l - 1]) * (n / l) * (m / l);
l = r + 1;
}
return ans;
}
void solve()
{
int n, m, d;
scanf("%d%d%d", &n, &m, &d);
if (n < m) swap(n, m);
shai(n);
printf("%lld\n", cal(n / d, m / d));
}
int main()
{
solve();
return 0;
}
原文:https://www.cnblogs.com/danzh/p/11296690.html