Let S(n) be the sum of sev(E) for all subsets E of {1,2,…,n}.
The function sev(E) is the sum of all elevisors of E. An element x∈E is an elevisor if x divides another element y∈E (where y=x).
We can rewrite 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 x and count how many subsets make x an elevisor:
S(n)=E⊆{1,…,n}∑x∈Ex is an elevisor of E∑x=x=1∑nx⋅(Number of subsets E where x∈E and x is an elevisor of E)
Let N(x) be the number of subsets E⊆{1,…,n} such that x∈E and x is an elevisor of E.
For x to be an elevisor of E, two conditions must be met:
x∈E.
There must exist an element y∈E such that y=x and x∣y. This means y=kx for some integer k>1, and y must also be in {1,…,n}, so kx≤n.
Let Mx={2x,3x,…,⌊n/x⌋x} be the set of multiples of x in {1,…,n} that are strictly greater than x.
For x to be an elevisor in a set E, E must contain x, and E must also contain at least one element from Mx (i.e., E∩Mx=∅).
The number of elements in Mx is mx=⌊n/x⌋−1.
If ⌊n/x⌋=1 (which means x>n/2), then mx=0. In this case, Mx is empty, so x cannot be an elevisor. Thus, N(x)=0 for x>n/2.
To determine N(x) for a given x≤n/2:
Let U={1,…,n}. We can partition U into three disjoint sets with respect to x:
The element x itself.
The set Mx (multiples of x greater than x, up to n).
The set Rx=U∖({x}∪Mx) (all other elements). The size of Rx is n−1−mx=n−1−(⌊n/x⌋−1)=n−⌊n/x⌋.
A subset E makes x an elevisor if:
x∈E: This is 1 choice (must include x).
E∩Mx=∅: The number of ways to choose a non-empty subset of Mx is 2mx−1. (If mx=0, this is 20−1=0 ways, correctly indicating x cannot be an elevisor).
Any elements from Rx can be included: The number of ways to choose a subset of Rx is 2∣Rx∣.
Combining these, the number of such subsets E is:
N(x)=1⋅(2mx−1)⋅2∣Rx∣=(2⌊n/x⌋−1−1)⋅2n−⌊n/x⌋
This formula also works for x>n/2: ⌊n/x⌋=1⟹mx=0⟹21−1−1=0, so N(x)=0.
The total sum S(n) is:
S(n)=x=1∑nx⋅N(x)=x=1∑nx⋅((2⌊n/x⌋−1−1)⋅2n−⌊n/x⌋)
Since terms for x>n/2 are zero, the sum effectively runs up to x=⌊n/2⌋.
For n=1014, a direct summation of O(n) terms is too slow. We observe that the value of N(x) depends on ⌊n/x⌋. The expression ⌊n/x⌋ takes on at most 2n distinct values. This allows us to group terms in the sum.
Let y0=⌊n/x⌋. The factor C(y0)=(2y0−1−1)⋅2n−y0 is constant for all x that share the same value of y0.
The algorithm iterates through blocks of x values:
Start with current_x = 1.
While current_x <= n:
a. Let y=⌊n/current_x⌋. (This is the common value of ⌊n/i⌋ for this block).
b. If y=0 (which happens if current_x>n), break. If y=1 (which happens if current_x>n/2), the term (2y−1−1) becomes 0, so the contribution is 0. The loop can handle this naturally.
c. Determine the range of integers [current_x,z] for which ⌊n/i⌋=y. The end of this range is z=⌊n/y⌋.
d. The sum of integers in this block is ∑i=current_xzi=2(z−current_x+1)(current_x+z).
e. The contribution of this block to S(n) is (∑i=current_xzi)⋅(2y−1−1)⋅2n−y.
f. Add this contribution to the total sum S(n) (modulo 1234567891).
g. Set current_x = z + 1 for the next block.
All calculations must be performed modulo 1234567891. This modulus is a prime number.
The division by 2 in the sum of arithmetic progression requires a modular multiplicative inverse of 2. Since 1234567891 is prime, 2−1≡2MOD−2(modMOD).
Modular exponentiation ab(modMOD) is computed efficiently using the method of exponentiation by squaring. The exponents y−1 and n−y can be large.
This “summing over distinct values of ⌊n/i⌋” technique reduces the number of blocks to O(n). Each block involves a few arithmetic operations, one modular inverse, and two modular exponentiations.