Блог пользователя pranjal.ssh

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

Hi, I am unable to solve the following problem. http://www.spoj.com/problems/COLORCAT/

I tried a few things like denoting the color change mathematically... the c1 and c2 colored cats talk to each other then their color changes to -(c1+c2) (mod 3) but it wasn't much useful.

Any hints or ideas are welcome.

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

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

Auto comment: topic has been updated by pranjal.ssh (previous revision, new revision, compare).

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

I dont remember all the details but my solution went like this:

Let's represent colors of cats as vector v. As you noticed two cats talking and changing color can be described using linear equations. So let's find matrix M such that v multiplied by M equals vector of cat colors after one step. Thus colors after k steps are v*M^k. This yield O(n^3 log k) solution but one can notice that matrix M is special. It's something like: value of every cell thats not in first 2 rows and first 2 columns is equal to value of north-west neighbouring cell. So every matrix is characterized only by its first 2 rows and first 2 columns and it turns out that their multiplication is actually just a few convolutions which can be done using FFT in O(n log n) so total complexity becomes O(n log n log k).