Блог пользователя TooNewbie

Автор TooNewbie, история, 9 лет назад, По-английски

Have to find LCM of first N numbers where N<=1000. Help me please

  • Проголосовать: нравится
  • +9
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +5 Проголосовать: не нравится

lcm(a, b) = a * b / gcd(a, b)

lcm(a, b, c) = lcm(lcm(a, b), c)

»
9 лет назад, скрыть # |
Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

Sieve for all prime numbers: O(N logN)

Calculate the multiple result of all prime numbers to the power which does not exceed N: O(N/logN * logN) = O(N)

You can only consider 512*729*625... since it includes all the prime number combination that you will take for <1000.

»
9 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +6 Проголосовать: не нравится

Any hints on how to solve the same problem for harder constraints?

See this problem

Update : Here is my implementation using the method told by Morass below. It passes in 1.38 second with cin/cout with any ios::base optimisations. Using fastio, the code passed in 1.25 seconds;