Have to find LCM of first N numbers where N<=1000. Help me please
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3773 |
3 | Radewoosh | 3646 |
4 | ecnerwala | 3624 |
5 | jqdai0815 | 3620 |
5 | Benq | 3620 |
7 | orzdevinwang | 3612 |
8 | Geothermal | 3569 |
8 | cnnfls_csy | 3569 |
10 | gyh20 | 3447 |
# | User | Contrib. |
---|---|---|
1 | awoo | 161 |
2 | maomao90 | 160 |
3 | nor | 157 |
4 | atcoder_official | 156 |
4 | cry | 156 |
6 | -is-this-fft- | 155 |
7 | maroonrk | 154 |
7 | adamant | 154 |
9 | Um_nik | 152 |
10 | djm03178 | 146 |
Have to find LCM of first N numbers where N<=1000. Help me please
Name |
---|
lcm(a, b) = a * b / gcd(a, b)
lcm(a, b, c) = lcm(lcm(a, b), c)
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.
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;
Good day to you!
I guess it is not optimal approach, yet it got AC:
At first, generate all primes <= 10^8. If you have "good sieve" , you can do this easily under one second.
The get all queries and sort them (so — lets do this offline)
Now let us have two pointers: One on a number, second on "query". Let us also have some answer and some "set with TODO-list"
Now some events might occur:
The number is greater than actual query: In this case, update actual query and move with query pointer
The first element of TODO-list is equal to number: Multiply answer by the value stored in TODO-list
The Number is not prime: Skip it
The number is prime: Multiply answer with it and add all its powers to TODO-list
There above points shall be processed in given order.
Lastly print all queries.
Hope it is understandable, if not then don't hesitate to ask more.
Here is mine solution.
Good Luck & Have nice day! :)
Thank you, the method is quite understandable and easy to implement.
using the same formula which CodeItLikeAidos wrote. The problem that you need to find the result of division by modulo M. For this you just need to calculate a^{m-2} mod m. It is easy to calculate this in log time.
lcm(a, b) =( a * b * gcd(a, b) * a^(m-2)) mod m
Can you briefly a bit more about the method as it is unclear to me as of now?
Also, lcm(a, b, c) = lcm((lcm(a, b), c) for which you require gcd(lcm(a, b), c) as per Erumaru's comment, but it will become difficult to calculate as the number of numbers become very large. Correct me, if I am wrong/mistaken here?
You should note the assumption that M is prime (that a^(m-2) trick uses Fermat's Little Theorem..)
You are 4 years late.
I guess you meant Fermat's Little Theorem, as well.
That was written in 4 AM, Sorry :)
Liorz
Monogorz
When I saw the notification in my mail, I immediately knew what the content would be