To solve this problem, we need to count for each prime factors, how many intervals include them.
First, let’s assume that all factors are distinct i.e. all factors only appears at one position. In this case, it’s easy to count the intervals that include them. For all primes at p, there are p⋅(n−p+1) intervals including them.
However, one prime can appear multiple times so we need to substract the repeated intervals(interval contain the current position and last position). Formally, if a prime appears at p and lastly appears at q, it adds (n−p+1)⋅q to answer.
So our strategy is calculating all primes less than 1e6 first. Go through all the numbers and find their prime factors. Record the all appearance of each factor and calculate their contributions to the answer.