JMAGNUM , LOC2017 need help

factors
prime
sieve

#1

https://www.codechef.com/LOCSEP17/problems/JMAGNUM/
I got TLE on cases >10^3. Please someone help me understand the solution.


#2

3 precomputations will be needed.

  1. precompute all the primes first. O(nloglogn)

  2. then precompute the sum of digits for every prime.
    precomuptation will take O(logn) for every prime number that you have already calculated from the sieve above.

  3. then precompute your ans for every query from 0 to i… and then just print ans[r]-ans[l-1].

well written code :


[1]


  [1]: https://www.codechef.com/viewsolution/15566762

#3

You can use a sieve and for each prime, instead of just using a flag to mark it as prime or not, you can store the sum of the prime divisors. After that you can take the prefix sum to answer each test case in O(1).
My solution: https://www.codechef.com/viewsolution/15554267
In my solution, initially ans* stores the sum of digits of prime divisors of i, then I take the prefix sum in the second loop. isprime*==1 if i is prime else 0.