Skip to content

Problem 944: Sum of Elevisors

Problem 944: Sum of Elevisors

Given a set EE of positive integers, an element xx of EE is called an element divisor (elevisor) of EE if xx divides another element of EE.

The sum of all elevisors of EE is denoted sev(E)\operatorname{sev}(E).
For example, sev({1,2,5,6})=1+2=3\operatorname{sev}(\{1, 2, 5, 6\}) = 1 + 2 = 3.

Let S(n)S(n) be the sum of sev(E)\operatorname{sev}(E) for all subsets EE of {1,2,,n}\{1, 2, \dots, n\}.
You are given S(10)=4927S(10) = 4927.

Find S(1014)mod1234567891S(10^{14}) \bmod 1234567891.

Let S(n)S(n) be the sum of sev(E)\operatorname{sev}(E) for all subsets EE of {1,2,,n}\{1, 2, \dots, n\}. The function sev(E)\operatorname{sev}(E) is the sum of all elevisors of EE. An element xEx \in E is an elevisor if xx divides another element yEy \in E (where yxy \neq x).

We can rewrite S(n)S(n) by changing the order of summation. Instead of summing over subsets and then over elevisors in each subset, we sum over potential elevisors xx and count how many subsets make xx an elevisor:

S(n)=E{1,,n}xEx is an elevisor of Ex=x=1nx(Number of subsets E where xE and x is an elevisor of E)S(n) = \sum_{E \subseteq \{1, \dots, n\}} \sum_{\substack{x \in E \\ x \text{ is an elevisor of } E}} x = \sum_{x=1}^{n} x \cdot (\text{Number of subsets } E \text{ where } x \in E \text{ and } x \text{ is an elevisor of } E)

Let N(x)N(x) be the number of subsets E{1,,n}E \subseteq \{1, \dots, n\} such that xEx \in E and xx is an elevisor of EE. For xx to be an elevisor of EE, two conditions must be met:

  1. xEx \in E.
  2. There must exist an element yEy \in E such that yxy \neq x and xyx | y. This means y=kxy = kx for some integer k>1k > 1, and yy must also be in {1,,n}\{1, \dots, n\}, so kxnkx \le n.

Let Mx={2x,3x,,n/xx}M_x = \{2x, 3x, \dots, \lfloor n/x \rfloor x\} be the set of multiples of xx in {1,,n}\{1, \dots, n\} that are strictly greater than xx. For xx to be an elevisor in a set EE, EE must contain xx, and EE must also contain at least one element from MxM_x (i.e., EMxE \cap M_x \neq \emptyset).

The number of elements in MxM_x is mx=n/x1m_x = \lfloor n/x \rfloor - 1. If n/x=1\lfloor n/x \rfloor = 1 (which means x>n/2x > n/2), then mx=0m_x = 0. In this case, MxM_x is empty, so xx cannot be an elevisor. Thus, N(x)=0N(x)=0 for x>n/2x > n/2.

To determine N(x)N(x) for a given xn/2x \le n/2: Let U={1,,n}U = \{1, \dots, n\}. We can partition UU into three disjoint sets with respect to xx:

  • The element xx itself.
  • The set MxM_x (multiples of xx greater than xx, up to nn).
  • The set Rx=U({x}Mx)R_x = U \setminus (\{x\} \cup M_x) (all other elements). The size of RxR_x is n1mx=n1(n/x1)=nn/xn - 1 - m_x = n - 1 - (\lfloor n/x \rfloor - 1) = n - \lfloor n/x \rfloor.

A subset EE makes xx an elevisor if:

  1. xEx \in E: This is 1 choice (must include xx).
  2. EMxE \cap M_x \neq \emptyset: The number of ways to choose a non-empty subset of MxM_x is 2mx12^{m_x} - 1. (If mx=0m_x=0, this is 201=02^0-1=0 ways, correctly indicating xx cannot be an elevisor).
  3. Any elements from RxR_x can be included: The number of ways to choose a subset of RxR_x is 2Rx2^{|R_x|}.

Combining these, the number of such subsets EE is:

N(x)=1(2mx1)2Rx=(2n/x11)2nn/xN(x) = 1 \cdot (2^{m_x} - 1) \cdot 2^{|R_x|} = \left(2^{\lfloor n/x \rfloor - 1} - 1\right) \cdot 2^{n - \lfloor n/x \rfloor}

This formula also works for x>n/2x > n/2: n/x=1    mx=0    2111=0\lfloor n/x \rfloor = 1 \implies m_x = 0 \implies 2^{1-1}-1 = 0, so N(x)=0N(x)=0.

The total sum S(n)S(n) is:

S(n)=x=1nxN(x)=x=1nx((2n/x11)2nn/x)S(n) = \sum_{x=1}^{n} x \cdot N(x) = \sum_{x=1}^{n} x \cdot \left( \left(2^{\lfloor n/x \rfloor - 1} - 1\right) \cdot 2^{n - \lfloor n/x \rfloor} \right)

Since terms for x>n/2x > n/2 are zero, the sum effectively runs up to x=n/2x = \lfloor n/2 \rfloor.

For n=1014n = 10^{14}, a direct summation of O(n)O(n) terms is too slow. We observe that the value of N(x)N(x) depends on n/x\lfloor n/x \rfloor. The expression n/x\lfloor n/x \rfloor takes on at most 2n2\sqrt{n} distinct values. This allows us to group terms in the sum. Let y0=n/xy_0 = \lfloor n/x \rfloor. The factor C(y0)=(2y011)2ny0C(y_0) = (2^{y_0 - 1} - 1) \cdot 2^{n - y_0} is constant for all xx that share the same value of y0y_0.

The algorithm iterates through blocks of xx values:

  1. Start with current_x = 1.
  2. While current_x <= n: a. Let y=n/current_xy = \lfloor n / \text{current\_x} \rfloor. (This is the common value of n/i\lfloor n/i \rfloor for this block). b. If y=0y=0 (which happens if current_x >n> n), break. If y=1y=1 (which happens if current_x >n/2> n/2), the term (2y11)(2^{y-1}-1) becomes 00, so the contribution is 00. The loop can handle this naturally. c. Determine the range of integers [current_x,z][ \text{current\_x}, z ] for which n/i=y\lfloor n/i \rfloor = y. The end of this range is z=n/yz = \lfloor n/y \rfloor. d. The sum of integers in this block is i=current_xzi=(zcurrent_x+1)(current_x+z)2\sum_{i=\text{current\_x}}^{z} i = \frac{(z - \text{current\_x} + 1)(\text{current\_x} + z)}{2}. e. The contribution of this block to S(n)S(n) is (i=current_xzi)(2y11)2ny\left( \sum_{i=\text{current\_x}}^{z} i \right) \cdot (2^{y-1} - 1) \cdot 2^{n-y}. f. Add this contribution to the total sum S(n)S(n) (modulo 12345678911234567891). g. Set current_x = z + 1 for the next block.

All calculations must be performed modulo 12345678911234567891. This modulus is a prime number.

  • The division by 2 in the sum of arithmetic progression requires a modular multiplicative inverse of 2. Since 12345678911234567891 is prime, 212MOD2(modMOD)2^{-1} \equiv 2^{MOD-2} \pmod{MOD}.
  • Modular exponentiation ab(modMOD)a^b \pmod{MOD} is computed efficiently using the method of exponentiation by squaring. The exponents y1y-1 and nyn-y can be large.

This “summing over distinct values of n/i\lfloor n/i \rfloor” technique reduces the number of blocks to O(n)O(\sqrt{n}). Each block involves a few arithmetic operations, one modular inverse, and two modular exponentiations.

The problem is solved by deriving a formula for S(n)S(n) and then optimizing its computation. The formula derived is:

S(n)=x=1nx((2n/x11)2nn/x)S(n) = \sum_{x=1}^{n} x \cdot \left( (2^{\lfloor n/x \rfloor - 1} - 1) \cdot 2^{n - \lfloor n/x \rfloor} \right)

The computation uses a standard optimization for sums involving N/i\lfloor N/i \rfloor (often called the “Dirichlet hyperbola method” or “summing by blocks”).

  • The loop iterates O(N)O(\sqrt{N}) times, once for each distinct value of N/x\lfloor N/x \rfloor (or range of xx values that produce it).
  • Inside each iteration:
    • Basic arithmetic operations (+,,,/+,-,*, /): O(1)O(1).
    • Modular inverse of 2: O(logMOD)O(\log MOD) using Fermat