---
id: 2489
lang: en
translation_of: index.md
source_sha256: 8d3fde95f3ff4f90a2be729d014bc093b7d3c6f2a3fd444c3143e07d5e1080b4
title: "LibreOJ#6181: A Certain Standard-Trick Summation Problem — Solution"
description: "What a good problem."
tags: ["OI","Number Theory","Min_25 Sieve"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "某个套路求和题 (the LibreOJ problem name) rendered as “A Certain Standard-Trick Summation Problem”; 套路 is not chipped because chips are not allowed in titles."
  - "没有效益 (literally “no benefit”) rendered as “brings no benefit”."
---

## Main Idea

What a good problem.

Notice that $f(x)$ brings no benefit for any number with a square factor, so we only consider $f(x) = \prod_{i = 1}^m p_i$. Then it is easy to see that $f(x) = \prod_{r = 0}^m (-1)^{2^{(r - 1)}}$, so:

$$
f(x) = \begin{cases} -1, x \in primes or x = 1 \\ 1, otherwise \end{cases}
$$

<!--more-->

With that worked out, we can think about splitting this thing into polynomials and handling it with the min_25 sieve. After some magical rearranging:

$$
\sum_{i = 1}^n f(i) = \sum_{i = 1}^n \mu^2(i) - \sum_{i = 1}^n 2[i \in primes]
$$

The latter part is obviously done by just throwing min_25 at it. For the former part, let's simplify it. Let $p(i)$ be the largest square factor of $i$:

$$
\begin{aligned} \sum_{i = 1}^n \mu^2(i) &= \sum_{i = 1}^n [p(i) = 1] \\ &= \sum_{i = 1}^n \sum_{d|p(i)} \mu(d) \\ &= \sum_{d = 1}^n \mu(d) \sum_{d^2|i} 1 \\ &= \sum_{d = 1}^n \mu(d) \lfloor \frac{n}{d^2} \rfloor \end{aligned}
$$

Computing up to around $\sqrt{n}$ is enough.

## Code

```cpp
// LOJ6181.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e7 + 200, mod = 998244353;

typedef long long ll;

int mu[MAX_N], ptot, idx[2][MAX_N], block, primes[MAX_N], tot;
ll n, weights[MAX_N], g[MAX_N];
bool vis[MAX_N];

struct locator
{
	int &operator[](const ll &rhs) { return rhs <= block ? idx[0][rhs] : idx[1][n / rhs]; }
} loc;

int main()
{
	scanf("%lld", &n), block = sqrt(n), mu[1] = 1;
	for (int i = 2; i < MAX_N; i++)
	{
		if (!vis[i])
			primes[++tot] = i, mu[i] = mod - 1;
		for (int j = 1; j <= tot && 1LL * i * primes[j] < MAX_N; j++)
		{
			vis[i * primes[j]] = true;
			if (i % primes[j] == 0)
			{
				mu[i * primes[j]] = 0;
				break;
			}
			mu[i * primes[j]] = (mod - mu[i]) % mod;
		}
	}
	for (ll i = 1, gx; i <= n; i = gx + 1)
	{
		gx = n / (n / i), weights[++ptot] = n / i;
		loc[weights[ptot]] = ptot;
		g[ptot] = (0LL + weights[ptot] - 1) % mod;
	}
	for (int i = 1; i <= tot; i++)
		for (int j = 1; j <= ptot && 1LL * primes[i] * primes[i] <= weights[j]; j++)
			g[j] = (0LL + g[j] + mod - g[loc[weights[j] / primes[i]]] + i - 1) % mod;
	int ans = 0;
	for (int i = 1; 1LL * i * i <= n; i++)
		ans = (0LL + ans + 1LL * (n / (1LL * i * i)) % mod * mu[i] % mod) % mod;
	ans = (0LL + ans + mod - 2LL * g[1] % mod) % mod;
	printf("%d\n", ans);
	return 0;
}
```
