Please subscribe to the official Codeforces channel in Telegram via the link ×

I_love_penny's blog

By I_love_penny, history, 6 years ago, In English

In this problem you are given "x" amount of money in currency "s" and you want to maximise the amount in "m" steps to currency "f". cost matrix is given for transfer rate. Sounds similar to finding number of ways to reach from one node to another in k steps. So the answer to problem is (s,f) value of mth power of given cost matrix (exchange rate matrix). we need to modify matrix multiplication accordingly to get maximum cost.


But we want ans%mod , this mod will ruin my multiplication of matrix. I will not get maximum. Editorial suggest to use power of primes , can someone please explain that.
Problem Link

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
6 years ago, # |
  Vote: I like it +14 Vote: I do not like it

Multiplying values will result into overflow , since you have to multiply values up to 10^9 times. And, if you perform modulo operation within matrix expo, the properties of max value will no more be valid. So, simplest way to solve this problem is by representation of integer number in the form of prime factorization. As the largest value in the given input will be 10, so every integers can easily represented using prime factorization of 2,3,5,7 and the largest values obtained after several multiplication will ultimately bounded to these prime factorization. So, we can store power of 2,3,5,7 as a single entry of base matrix and perform matrix expo accordingly.
Now, the task is to compare after multiplication of two integers with another one. This task can be done by converting the prime factorization representation value into LOG value and doing comparison accordingly. Since, the largest integer number has greater LOG value.

Want to see code?