What are some efficient ways to divide 'a' by 'b' where the range is :
-10^300 <= a,b <= +10^300
№ | Пользователь | Рейтинг |
---|---|---|
1 | jiangly | 3898 |
2 | tourist | 3840 |
3 | orzdevinwang | 3706 |
4 | ksun48 | 3691 |
5 | jqdai0815 | 3682 |
6 | ecnerwala | 3525 |
7 | gamegame | 3477 |
8 | Benq | 3468 |
9 | Ormlis | 3381 |
10 | maroonrk | 3379 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 168 |
2 | -is-this-fft- | 165 |
3 | Dominater069 | 161 |
4 | Um_nik | 159 |
4 | atcoder_official | 159 |
6 | djm03178 | 157 |
7 | adamant | 153 |
8 | luogu_official | 150 |
9 | awoo | 149 |
10 | TheScrasse | 146 |
What are some efficient ways to divide 'a' by 'b' where the range is :
-10^300 <= a,b <= +10^300
Название |
---|
Since the answer of
a/b
is between 0 and a, we could use binary_search. Complexity:log(a)
, which is around 1000.That's actually since in every iteration of binary search you need to multiply two numbers.
Fortunately there's an easier and faster way in O(n2) (hereafter n is the number of digits), assuming the numeric base d is a small constant (typically 2, 10, or 16).
Start with x = 0, then, for each i from n down to 0, keep incrementing x by di as long as xb ≤ a. This "increment and compare" operation can be done in O(n) by performing it not on x, but on the product xb — you can add bdi to any number by simply appending i zeroes to the d-ary representation of b and performing addition as usual. Running time of this algorithm is O(d·n2). For large values of d you can use binary search inside each iteration of the outer loop, instead of a simple loop which keeps adding di, so the complexity becomes .
There is a way to do it in , where N is the sum of lengths of the numbers. Link. But it's masochistic to implement it (as far as I remember).