Пожалуйста, подпишитесь на официальный канал Codeforces в Telegram по ссылке https://t.me/codeforces_official. ×

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

Автор Tuhin_RU, история, 3 года назад, По-английски

Given, two binary number A and B (A > B). Each of A and B can have at most 10^5 digits. You have to calculate (A^2 — B^2). [Here, (A^2) denotes the A power of 2].

What should be the approach to calculate A^2?

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

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

use strings in c++, may be python. going to take about a minute on modern computer.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

You can transform A from base 2 to some 2 ^ k base to make the operations more efficient.

Start from the end and divide the binary string in chunks of k bits each (the last chunk can have less than k bits). For each chunk, calculate the number it represents.

Example: 10100110101, k = 3;

=> (10)(100)(110)(101) = (2)(4)(6)(5) = 2465

And then, you can calculate A ^ 2, using simple arithmetic.

Complexity: O(N ^ 2), with the constant logk(2) ^ 2.

»
3 года назад, # |
  Проголосовать: нравится +19 Проголосовать: не нравится

Use FFT to find $$$A^2$$$. Any binary number would be $$$\sum\limits_{i=0}^{N} a_ix^i$$$, where $$$a_i = 0/1$$$ and $$$x = 2$$$. So you need to multiply this polynomial with itself and then find the value at $$$x = 2$$$. You can read more about it here

Complexity : $$$O(N*log(N))$$$, where $$$N = 10^5$$$