hello,
for this problem, all I could come up with is to find all divisors of 'b' that are less than or equal to 't' using O(sqrt(n)) but It will get TLE for such input limits " < (1<<54)".
any hints for a better aproach ?
thanks :D
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
hello,
for this problem, all I could come up with is to find all divisors of 'b' that are less than or equal to 't' using O(sqrt(n)) but It will get TLE for such input limits " < (1<<54)".
any hints for a better aproach ?
thanks :D
Название |
---|
The problem link is broken...
link is fixed :D
Why would it get TL? 2^27 can fit time limit of 1 second in some cases, but there I even see 3 second limit, so I think it should pass easily.
Otherwise if you want to have faster solution you can try to factorize faster. You can use pollard's pollard's factorizing algo that works in O(sqrt(least divisor)) and divides you N as multiplication of two numbers.(if I remember it right, I used it only once in my life by far)
this code gets TLE.
Because your while(x % I == 0) will never end
opss!! sorry I forgot to change it to if statement, but even though it still gives TLE :/
Note: I see in "best submissions" list people that have solved the problem in 0.0 time, so perhaps there is some much better solution, but you can still get AC with mine.
Okay, so this is indeed a factorization problem. My solution may not be the simplest, but it should teach you some tricks to finding divisors quite quickly.
Operations "/" and "%" are slow. That's just how it is. They're significantly slower than "+" or "*", so if you were to perform 2^27 of them, it's quite normal to time limit. So let's come up with a better method now — let's find only the prime divisors of the number. Now you may ask "how is that faster, it's still O(sqrtN)?" — this is true, but we can check if some number is a prime factor only if it is prime, which reduces tons of operations. So we first use the Sieve of Eratosthenes to find all primes under 2^27, and then we can relatively quickly find the prime factorization of our number.
A trick to speed it up, is each time you find a divisor — divide the number by it, this way the number will become smaller, and it's square root would also reduce, giving you less iterations in the process. Of course this wouldn't work on all numbers, since factorizating a prime would always require the same amount of operations.
Finally, now that we've got our prime factorization, we can just generate all divisors! That's right, the divisors are actually not that many at all, most likely a few millions at most. So a clever generator can generate them all in linear time.
Here is my AC code in 1.51s
Thank you so much for precuios informations.
I know all about prime factorization method along with Sieve of Eratosthenes, but I didn't know how to generate all divisors using prime factors.
I don't want to look at any AC codes until I get it my self, but if you can give any hints for generating all divisors from prime factors, it will be appreciated. :D
Well, generating them is quite simple. All you have to do is be careful not to repeat.
Now suppose you have K factors. Then the easiest way to go is to just generate all 2^K combinations, but this gives repetitions, because you might have one prime factor more than once.
Simplest way to overcome that would be to group the equal prime divisors together, and then just try all possible combinations of how much you take from each group.
Example:
Say you have number 72=2*2*2*3*3. Then you say you have 3 twos and 2 threes. Then just try taking 2^0;3^0 | 2^0;3^1 | 2^0;3^2 | 2^1;3^0 etc...
In this way you will not repeat a divisor, and therefore get a linear running time :)
well it's weird how the judges time works out with this code.
I got AC with 1.78 S.
Thank you so much :D Enchom
I sent my code again but with upper limit for Sieve equal to (1 << 20)
and surprisingly I got AC with 0.0 S :V
I don't know if it is a problem with weak test cases or if this limit is quite enough for this problem.