Number-Theoretic Block Decomposition

Number-theoretic block decomposition can quickly calculate sums of the form

∑i=1nf(i)g(⌊ni⌋)\sum_{i=1}^nf(i)g\left(\left\lfloor\dfrac ni\right\rfloor\right)

If O(1)O(1) can be calculated in ∑i=lrf(i)\sum_{i=l}^{r}f(i) time, or the prefix sums of ff have already been precomputed, number-theoretic block decomposition can calculate the value of the above sum in O(n)O(\sqrt{n}) time.

Number-theoretic block decomposition is often combined with techniques such as the Möbius inversion.

Idea

First, this article uses a simple example to explain the idea of number-theoretic block decomposition. Suppose we want to count the lattice points under the hyperbola shown in the figure:

Lattice points under a hyperbola
Lattice points under a hyperbola

This is equivalent to calculating the sum:

∑i=111⌊11i⌋.\sum_{i=1}^{11}\left\lfloor\dfrac{11}{i}\right\rfloor.

This is the special case of the sum above with f(k)=1, g(k)=kf(k)=1,~g(k)=k.

The simplest method is of course to calculate each column and then sum them, but this requires calculating the number of lattice points in each of columns i=1,2,⋯ ,11i=1,2,\cdots,11. From the figure, we can see that these lattice-point columns can be divided into 55 blocks. The columns within each block have the same height and form a rectangular lattice. Therefore, as long as we know the widths of these blocks, we can quickly finish the count by calculating the sizes of the rectangular blocks.

This is the basic idea of divisor block decomposition.

Properties

This section discusses several conclusions about block decomposition of lattice points under the hyperbola y=nxy = \dfrac{n}{x}. Specifically, the integers from 1∼n1\sim n need to be divided into blocks according to the values of ⌊ni⌋\left\lfloor\dfrac{n}{i}\right\rfloor. Let

D(n)={⌊ni⌋:1≤i≤n, i∈N+}.D(n) = \left\{\left\lfloor\dfrac{n}{i}\right\rfloor : 1 \le i \le n,~i\in\mathbf N_+\right\}.

This is the set of all possible values of ⌊ni⌋\left\lfloor\dfrac{n}{i}\right\rfloor.

First, there are only O(n)O(\sqrt{n}) different values of this form. Therefore, the number of blocks obtained by number-theoretic block decomposition is only O(n)O(\sqrt{n}).

Property 1

∣D(n)∣≤2n|D(n)|\le 2\sqrt{n}.

Proof

Consider two cases:

  • When i≤ni\le\sqrt{n}, there are at most ii possible values of n\sqrt{n}, so there are also at most ⌊ni⌋\left\lfloor\dfrac{n}{i}\right\rfloor possible values of n\sqrt{n}.
  • When i>ni>\sqrt{n}, ⌊ni⌋≤ni<n\left\lfloor\dfrac{n}{i}\right\rfloor \le\dfrac{n}{i} < \sqrt{n}, so there are again at most n\sqrt{n} possible values.

Therefore, the total number of possible values is ∣D(n)∣≤2n|D(n)|\le 2\sqrt{n}.

With a more detailed analysis, the set D(n)D(n) and its size can in fact be described exactly.

Property 2

Let s=⌊n⌋s = \lfloor\sqrt{n}\rfloor. The elements of D(n)D(n) in increasing order are

1<2<⋯<s−1<s≤⌊ns⌋<⌊ns−1⌋<⋯<⌊n2⌋<⌊n1⌋=n.1 < 2 < \cdots < s-1 < s \le \left\lfloor\dfrac{n}{s}\right\rfloor < \left\lfloor\dfrac{n}{s-1}\right\rfloor < \cdots < \left\lfloor\dfrac{n}{2}\right\rfloor < \left\lfloor\dfrac{n}{1}\right\rfloor = n.

Consequently, ∣D(n)∣=⌊4n+1⌋−1|D(n)| = \lfloor \sqrt{4n+1}\rfloor - 1.

Proof

First, for 1≤i≤s1 \le i \le s, one can prove that ⌊n⌊n/i⌋⌋=i\left\lfloor\dfrac{n}{\lfloor n/i\rfloor}\right\rfloor = i. Let d=⌊ni⌋d = \left\lfloor\dfrac{n}{i}\right\rfloor; we need to prove ⌊nd⌋=i\left\lfloor\dfrac{n}{d}\right\rfloor = i. Written as inequalities, this means that given d≤ni<d+1d \le \dfrac{n}{i} < d + 1, we need to prove i≤nd<i+1i \le \dfrac{n}{d} < i + 1. The given condition can be written as i≤nd<i+idi\le\dfrac{n}{d} < i + \dfrac{i}{d}, so it is enough to prove id≤1\dfrac{i}{d} \le 1, that is, i≤d=⌊ni⌋i \le d = \left\lfloor\dfrac{n}{i}\right\rfloor. This is equivalent to i≤nii \le \dfrac{n}{i}, which holds for every 1≤i≤s≤n1 \le i \le s \le \sqrt{n}.

This result actually shows that the mapping i↦⌊ni⌋i \mapsto \left\lfloor\dfrac{n}{i}\right\rfloor is a bijection between {i:1≤i≤s}\{i:1\le i \le s\} and {⌊ni⌋:1≤i≤s}\left\{\left\lfloor\dfrac{n}{i}\right\rfloor: 1\le i\le s\right\}. Since the values of ii are distinct, the values of ⌊ni⌋\left\lfloor\dfrac{n}{i}\right\rfloor are also distinct. The only possible common elements of the two sets are ss and ⌊ns⌋\left\lfloor\dfrac{n}{s}\right\rfloor. Therefore, ∣D(n)∣=2s−[s=⌊n/s⌋]|D(n)|=2s - \left[s = \lfloor n/s\rfloor\right].

To obtain the final expression for ∣D(n)∣|D(n)|, we need to determine when s=⌊ns⌋s = \left\lfloor\dfrac{n}{s}\right\rfloor holds.

  • When s=⌊ns⌋s = \left\lfloor\dfrac{n}{s}\right\rfloor, we always have s≤ns<s+1s \le \dfrac{n}{s} < s + 1, that is, s2≤n<s2+ss^2 \le n < s^2+s. Thus, 4s2+1≤4n+1<4s2+4s+1=(2s+1)24s^2 + 1 \le 4n + 1 < 4s^2+4s+1 = (2s+1)^2. Since 4n+14n+1 is always odd, the left side can equivalently be relaxed to 4s24s^2, so this condition is equivalent to 2s≤4n+1<2s+12s \le \sqrt{4n+1} < 2s+1, that is, ⌊4n+1⌋=2s\lfloor \sqrt{4n+1}\rfloor = 2s.
  • When s<⌊ns⌋s < \left\lfloor\dfrac{n}{s}\right\rfloor, we always have s+1≤nss + 1 \le \dfrac{n}{s}, that is, s2+s≤ns^2 + s\le n. Since n<(s+1)2n < (s+1)^2, we have s2+s≤n<(s+1)2s^2 + s\le n < (s+1)^2. This is equivalent to (2s+1)2≤4n+1<4(s+1)2+1(2s+1)^2\le 4n+1 < 4(s+1)^2+1. Again using the fact that 4n+14n+1 is odd, the right side can equivalently be tightened to 4(s+1)24(s+1)^2, so this condition is equivalent to 2s+1≤4n+1<2s+22s+1\le\sqrt{4n+1} < 2s+2, that is, ⌊4n+1⌋=2s+1\lfloor \sqrt{4n+1}\rfloor = 2s+1.

Summarizing these two cases gives ∣D(n)∣=⌊4n+1⌋−1|D(n)| = \lfloor \sqrt{4n+1}\rfloor - 1.

Then, the left and right endpoints of an individual block are easy to determine.

Property 3

For d∈D(n)d\in D(n), the range of integer values of ⌊ni⌋=d\left\lfloor\dfrac{n}{i}\right\rfloor=d satisfying ii is

⌊nd+1⌋+1≤i≤⌊nd⌋.\left\lfloor\dfrac{n}{d+1}\right\rfloor + 1\le i \le \left\lfloor\dfrac{n}{d}\right\rfloor.
Proof

Since ⌊ni⌋=d\left\lfloor\dfrac{n}{i}\right\rfloor=d is equivalent to

d≤ni<d+1.d\le \dfrac{n}{i} < d+1.

This is further equivalent to

nd+1<i≤nd.\dfrac{n}{d+1} < i\le \dfrac{n}{d}.

Using i∈N+i\in\mathbf N_+, we can take the floor of this inequality, making it equivalent to

⌊nd+1⌋+1≤i≤⌊nd⌋.\left\lfloor\dfrac{n}{d+1}\right\rfloor + 1 \le i\le \left\lfloor\dfrac{n}{d}\right\rfloor.

This property also reflects the symmetry of the graph: the set of right endpoints of all blocks (the green points in the figure above) is exactly D(n)D(n). This is easy to understand because the entire graph is symmetric about the line y=xy=x.

Besides these properties, the set D(n)D(n) also has a useful recursive property:

Property 4

For m∈D(n)m\in D(n), D(m)⊆D(n)D(m)\subseteq D(n).

Proof

Let m=⌊nk⌋m = \left\lfloor\dfrac{n}{k}\right\rfloor. Then, for every i∈N+i\in\mathbf N_+,

⌊mi⌋=⌊⌊n/k⌋i⌋=⌊nki⌋∈D(n),\left\lfloor\dfrac{m}{i}\right\rfloor = \left\lfloor\dfrac{\lfloor n/k\rfloor}{i}\right\rfloor = \left\lfloor\dfrac{n}{ki}\right\rfloor \in D(n),

so D(m)⊆D(n)D(m)\subseteq D(n). The second equality uses the property of the floor function for nested fractions.

As mentioned above, D(n)D(n) is both the set of values of ⌊ni⌋\left\lfloor\dfrac{n}{i}\right\rfloor in each block and the set of right endpoints of the blocks. This means that if number-theoretic block decomposition is applied recursively (that is, the value of a function at nn depends on its values at m∈D(n)∖{n}m\in D(n)\setminus\{n\}), then the sets of values and right endpoints involved throughout the computation are both actually D(n)D(n). A typical example is the Du sieve.

Process

Using the conclusions described in the previous section, we obtain the concrete process of number-theoretic block decomposition.

To calculate

∑i=1nf(i)g(⌊ni⌋)\sum_{i=1}^nf(i)g\left(\left\lfloor\dfrac ni\right\rfloor\right)

we can divide the indices ⌊ni⌋\left\lfloor\dfrac ni\right\rfloor into blocks according to the value of i=1,2,⋯ ,ni=1,2,\cdots,n. Since the indices with the same value of ⌊ni⌋\left\lfloor\dfrac ni\right\rfloor form a consecutive integer interval [l,r][l,r], the value of the sum in that block is

(∑i=lrf(i))⋅g(⌊nl⌋).\left(\sum_{i=l}^rf(i)\right)\cdot g\left(\left\lfloor\dfrac nl\right\rfloor\right).

To calculate this sum quickly, one generally needs to calculate the sum involving ff on the left quickly. In some cases, its expression is known, so a single calculation can be completed in O(1)O(1) time; in other cases, its prefix sums can be precomputed, and a single query can still be completed in O(1)O(1) time.

When calculating the left and right endpoints of each block in sequence, the left endpoint ll of the current block is the right endpoint of the previous block plus 11, while the right endpoint of the current block is ⌊n⌊n/l⌋⌋\left\lfloor\dfrac n{\lfloor n/l\rfloor}\right\rfloor. This gives the following pseudocode:

Algorithm Sum(f,g,n):Input. n, s(k)=∑i=1kf(k), g(k).Output. S(n)=∑i=1nf(i)g(⌊n/i⌋).Method.1l←12result←03while l≤n do4r←⌊n⌊n/l⌋⌋5result←result+(s(r)−s(l−1))⋅g(⌊nl⌋)6l←r+17end while8return result\begin{array}{l} \textbf{Algorithm }\text{Sum}(f,g,n):\\ \textbf{Input. }n,~s(k)=\sum_{i=1}^kf(k),~g(k).\\ \textbf{Output. }S(n) = \sum_{i=1}^nf(i)g(\lfloor n/i\rfloor).\\ \textbf{Method.}\\ \begin{array}{ll} 1 & l \gets 1\\ 2 & \textit{result} \gets 0 \\ 3 & \textbf{while } l \leq n \textbf{ do}\\ 4 & \qquad r \gets \left\lfloor \dfrac{n}{\lfloor n/l \rfloor} \right\rfloor\\ 5 & \qquad \textit{result} \gets \textit{result} + (s(r)-s(l-1))\cdot g\left(\left\lfloor \dfrac{n}{l} \right\rfloor\right)\\ 6 & \qquad l \gets r+1\\ 7 & \textbf{end while}\\ 8 & \textbf{return }\textit{result} \end{array} \end{array}

Assuming that a single calculation of s(⋅)s(\cdot) has time complexity O(1)O(1), the time complexity of the entire process is O(n)O(\sqrt{n}).

Extensions

The previous section discussed the most common and basic form of number-theoretic block decomposition. This section further discusses its extensions.

Number-Theoretic Block Decomposition with Ceilings

Number-theoretic block decomposition can be used to calculate sums containing ceilings:

∑i=1nf(i)g(⌈ni⌉).\sum_{i=1}^nf(i)g\left(\left\lceil\dfrac ni\right\rceil\right).

Since ⌈ni⌉=⌊n−1i⌋+1\left\lceil\dfrac ni\right\rceil = \left\lfloor\dfrac {n-1}i\right\rfloor + 1, the sum can be converted to the floor case:

f(n)g(1)+∑i=1n−1f(i)g(⌊n−1i⌋+1).f(n)g(1) + \sum_{i=1}^{n-1}f(i)g\left(\left\lfloor\dfrac {n-1}i\right\rfloor + 1\right).

Note that the upper bound of the summation changes, and the term for i=ni=n is handled separately.

Multidimensional Number-Theoretic Block Decomposition

Number-theoretic block decomposition can also handle sums containing more than one floor expression:

∑i=1nf(i)g(⌊n1i⌋,⌊n2i⌋,⋯ ,⌊nmi⌋).\sum_{i=1}^{n}f(i)g\left(\left\lfloor\dfrac {n_1}i\right\rfloor,\left\lfloor\dfrac {n_2}i\right\rfloor,\cdots,\left\lfloor\dfrac {n_m}i\right\rfloor\right).

To apply the idea, it is necessary to ensure that the values of all floor expressions ⌊n1i⌋,⌊n2i⌋,⋯ ,⌊nmi⌋\left\lfloor\dfrac {n_1}i\right\rfloor,\left\lfloor\dfrac {n_2}i\right\rfloor,\cdots,\left\lfloor\dfrac {n_m}i\right\rfloor remain unchanged within each block. In other words, a multidimensional block should be the intersection of all one-dimensional blocks. Therefore, for a known left endpoint ll, the corresponding right endpoint is

min⁡{⌊n1⌊n1/l⌋⌋,⌊n2⌊n2/l⌋⌋,⋯ ,⌊nm⌊nm/l⌋⌋}.\min\left\{\left\lfloor\dfrac {n_1}{\lfloor n_1/l\rfloor}\right\rfloor,\left\lfloor\dfrac {n_2}{\lfloor n_2/l\rfloor}\right\rfloor,\cdots,\left\lfloor\dfrac {n_m}{\lfloor n_m/l\rfloor}\right\rfloor\right\}.

That is, take the minimum of the right endpoints of all one-dimensional blocks as the right endpoint of the multidimensional block. The figure below can help illustrate this:

Illustration of multidimensional number-theoretic block decomposition
Illustration of multidimensional number-theoretic block decomposition

The two-dimensional form is relatively common. In this case, replace r←⌊n⌊n/l⌋⌋r \gets \left\lfloor \dfrac{n}{\lfloor n/l \rfloor}\right\rfloor in the pseudocode above with

r←min⁡{⌊n1⌊n1/l⌋⌋,⌊n2⌊n2/l⌋⌋}.r \gets \min\left\{\left\lfloor \dfrac{n_1}{\lfloor n_1/l \rfloor}\right\rfloor,\left\lfloor \dfrac{n_2}{\lfloor n_2/l \rfloor}\right\rfloor\right\}.

Number-Theoretic Block Decomposition with Arbitrary Exponents

Number-theoretic block decomposition can be used to calculate sums containing floor expressions with arbitrary exponents:

∑i=1⌊nα/β⌋f(i)g(⌊nαiβ⌋).\sum_{i=1}^{\lfloor n^{\alpha/\beta}\rfloor}f(i)g\left(\left\lfloor\dfrac {n^\alpha}{i^\beta}\right\rfloor\right).

Here, α,β\alpha,\beta are positive real numbers. In the basic form discussed in this article, α=β=1\alpha=\beta=1.

Property

For a positive integer nn and positive real numbers α,β\alpha, \beta, let

D(n,α,β)={⌊nαiβ⌋:i=1,2,⋯ ,⌊nα/β⌋}.D(n,\alpha,\beta) = \left\{\left\lfloor\dfrac{n^\alpha}{i^\beta}\right\rfloor: i=1,2,\cdots,\lfloor n^{\alpha/\beta}\rfloor\right\}.

Then,

  1. ∣D(n,α,β)∣≤2nα/(1+β)|D(n,\alpha,\beta)|\le 2n^{\alpha/(1+\beta)}.

  2. For d∈D(n,α,β)d\in D(n,\alpha,\beta), the range of ⌊nαiβ⌋=d\left\lfloor\dfrac{n^\alpha}{i^\beta}\right\rfloor=d satisfying ii is

    ⌊nα/β(d+1)1/β⌋+1≤i≤⌊nα/βd1/β⌋.\left\lfloor\dfrac{n^{\alpha/\beta}}{(d+1)^{1/\beta}}\right\rfloor + 1\le i \le \left\lfloor\dfrac{n^{\alpha/\beta}}{d^{1/\beta}}\right\rfloor.
Proof

For the first point, consider two cases:

  • When i≤nαiβi\le \dfrac{n^\alpha}{i^\beta}, we have i≤nα/(1+β)i\le n^{\alpha/(1+\beta)}, so there are at most ⌊nαiβ⌋\left\lfloor\dfrac{n^\alpha}{i^\beta}\right\rfloor possible values of nα/(1+β)n^{\alpha/(1+\beta)}.
  • When i>nαiβi > \dfrac{n^\alpha}{i^\beta}, we have i>nα/(1+β)i> n^{\alpha/(1+\beta)}, so nαiβ<nα/(1+β)\dfrac{n^\alpha}{i^\beta} < n^{\alpha/(1+\beta)}, and there are again at most ⌊nαiβ⌋\left\lfloor\dfrac{n^\alpha}{i^\beta}\right\rfloor possible values of nα/(1+β)n^{\alpha/(1+\beta)}.

Combining the two cases gives ∣D(n,α,β)∣≤2nα/(1+β)|D(n,\alpha,\beta)|\le 2n^{\alpha/(1+\beta)}.

For the second point, ⌊nαiβ⌋=d\left\lfloor\dfrac{n^\alpha}{i^\beta}\right\rfloor=d is equivalent to

d≤nαiβ<d+1  ⟺  nα/β(d+1)1/β<i≤nα/βd1/β.d \le \dfrac{n^\alpha}{i^\beta} < d+1 \iff \dfrac{n^{\alpha/\beta}}{(d+1)^{1/\beta}} < i \le \dfrac{n^{\alpha/\beta}}{d^{1/\beta}}.

Taking floors of this inequality gives the second statement.

Using these properties, number-theoretic block decomposition for arbitrary exponents can be implemented in O(nα/(1+β))O(n^{\alpha/(1+\beta)}) time.

Example

For example, for α=β=1/2\alpha=\beta=1/2, consider the sum

∑i=1nf(i)g(⌊ni⌋),\sum_{i=1}^nf(i)g\left(\left\lfloor\sqrt{\dfrac {n}{i}}\right\rfloor\right),

which can be solved by number-theoretic block decomposition in O(n1/3)O(n^{1/3}) time. When the left endpoint of a block is ll, the right endpoint can be calculated as r=⌊n⌊n/l⌋2⌋r=\left\lfloor\dfrac{n}{\lfloor\sqrt{n/l}\rfloor^2}\right\rfloor.

Examples

UVa11526 H(n)

TT data sets are given, each containing an integer nn. For each data set, output ∑i=1n⌊ni⌋\sum_{i=1}^n\left\lfloor\dfrac ni\right\rfloor.

Solution

Based on the analysis above, all positions with the same ⌊ni⌋\left\lfloor\dfrac ni\right\rfloor can be calculated together. The time complexity is O(Tn)O(T\sqrt n).

Implementation
#include <iostream>

long long H(int n) {
  long long res = 0;  // store the result
  int l = 1, r;       // left and right endpoints of the block
  while (l <= n) {
    r = n / (n / l);  // calculate the right endpoint of the current block
    // Accumulate this block's contribution to the result. Multiplying by 1LL prevents overflow
    res += 1LL * (r - l + 1) * (n / l);
    l = r + 1;  // move the left endpoint to the next block
  }
  return res;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int t, n;
  std::cin >> t;
  while (t--) {
    std::cin >> n;
    std::cout << H(n) << '\n';
  }
  return 0;
}
Codeforces 1954E Chain Reaction

There is a row of nn monsters, each with initial health aia_i. One attack decreases the health of a contiguous segment of living monsters by kk; health no greater than 00 is considered dead. For every kk, find the number of attacks required to kill all monsters. Here n,ai≤105n,a_i\leq 10^5.

Solution

Let a0=0a_0=0. Suppose killing the first (i−1)(i-1) monsters requires T(k,i−1)T(k,i-1) attacks, and the health of the ii-th monster is aia_i. Since killing the (i−1)(i-1)-th monster requires attacking it ⌈ai−1/k⌉\lceil a_{i-1}/k\rceil times, all these attacks can extend to the ii-th monster. Therefore, to kill the ii-th monster, only max⁡{0,⌈ai/k⌉−⌈ai−1/k⌉}\max\{0,\lceil a_i/k\rceil-\lceil a_{i-1}/k\rceil\} more attacks are needed. Thus, the total number of attacks is

T(k,n)=∑i=1nmax⁡(0,⌈aik⌉−⌈ai−1k⌉).T(k,n)=\sum_{i=1}^n\max\left(0,\left\lceil\dfrac{a_i}{k}\right\rceil-\left\lceil\dfrac{a_{i-1}}{k}\right\rceil\right).

Since the n,kn,k involved in the problem can be large, calculating this sum separately for every kk is infeasible. We can maintain the sequence i=1,2,⋯ ,ni=1,2,\cdots,n for every {T(k,i)}k\{T(k,i)\}_k. Initially, let T(k,0)≡0T(k,0)\equiv 0. Suppose the sequence {T(k,i−1)}k\{T(k,i-1)\}_k is known. To obtain {T(k,i)}k\{T(k,i)\}_k, according to the analysis above, we only need to add kk to the max⁡(0,⌈aik⌉−⌈ai−1k⌉)\max\left(0,\left\lceil\dfrac{a_i}{k}\right\rceil-\left\lceil\dfrac{a_{i-1}}{k}\right\rceil\right)-th term. Using multidimensional number-theoretic block decomposition, this modification can be divided into O(ai−1+ai)O(\sqrt{a_{i-1}}+\sqrt{a_i}) interval modification operations, with a fixed value added on each interval. The resulting sequence {T(k,n)}k\{T(k,n)\}_k is the answer.

Since the problem involves a series of interval-addition operations and queries occur only after all modifications are complete, a difference sequence can be maintained for interval-addition modifications, and the required sequence can finally be obtained by taking prefix sums. The total time complexity is O(∑ai)O(\sum\sqrt{a_i}). This problem also has other solutions.

Implementation
#include <algorithm>
#include <iostream>

constexpr int N = 100009;
int n, a[N], maxn;
long long ans[N];

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i];
    maxn = std::max(maxn, a[i]);
  }
  for (int i = 0; i < n; ++i)
    for (int l = 1, r;; l = r + 1) {
      r = std::min(l < a[i] ? (a[i] - 1) / ((a[i] - 1) / l) : N,
                   l < a[i + 1] ? (a[i + 1] - 1) / ((a[i + 1] - 1) / l)
                                : N);  // two-dimensional number-theoretic block decomposition
      if (r == N) break;
      int x = (a[i + 1] - 1) / l - std::max(a[i] - 1, 0) / l;
      if (x > 0) ans[l] += x, ans[r + 1] -= x;  // accumulate contribution
    }
  ++ans[0];  // the formula ⌈a/l⌉=(a-1)/l+1 does not hold when a=0, so it needs to be corrected
  for (int i = 1; i <= maxn; ++i)
    std::cout << (ans[i] += ans[i - 1]) << " \n"[i == maxn];
  return 0;
}

Exercises

References and notes

This content comes from OI Wiki: Number-Theoretic Block Decomposition. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.