Given: set A = {a0, a1, ..., aN-1}
(1 ≤ N ≤ 100
), with 2 ≤ ai ≤ 500
.
Asked: Find the sum of all least common multiples (LCM) of all subsets of A
of size at least 2.
The LCM of a setB = {b0, b1, ..., bk-1}
is defined as the minimum integer Bmin
such that bi | Bmin
, for all 0 ≤ i < k
.
Example:
Let N = 3
and A = {2, 6, 7}
, then:
LCM({2, 6}) = 6
LCM({2, 7}) = 14
LCM({6, 7}) = 42
LCM({2, 6, 7}) = 42
----------------------- +
answer 104
The naive approach would be to simply calculate the LCM for all O(2N)
subsets, which is not feasible for reasonably large N
.
Solution sketch:
The problem is obtained from a competition*, which also provided a solution sketch. This is where my problem comes in: I do not understand the hinted approach.
The solution reads (modulo some small fixed grammar issues):
The solution is a bit tricky. If we observe carefully we see that the integers are between
2
and500
. So, if we prime factorize the numbers, we get the following maximum powers:
2 8
3 5
5 3
7 3
11 2
13 2
17 2
19 2
Other than this, all primes have power 1. So, we can easily calculate all possible states, using these integers, leaving
9 * 6 * 4 * 4 * 3 * 3 * 3 * 3
states, which is nearly70000
. For other integers we can make a dp like the following:dp[70000][i]
, wherei
can be0
to100
. However, asdp[i]
is dependent ondp[i-1]
, sodp[70000][2]
is enough. This leaves the complexity ton * 70000
which is feasible.
I have the following concrete questions:
- What is meant by these states?
- Does
dp
stand for dynamic programming and if so, what recurrence relation is being solved? - How is
dp[i]
computed fromdp[i-1]
? - Why do the big primes not contribute to the number of states? Each of them occurs either
0
or1
times. Should the number of states not be multiplied by2
for each of these primes (leading to a non-feasible state space again)?
*The original problem description can be found from this source (problem F). This question is a simplified version of that description.