We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

The way modular arithmatic works, you can take the mod of each component of the product before you calculate the product and get the same result as if you were to mod the poduct after multiplying. My code had something like:

Thanks for that explanation. I was wondering why my solution of doing it after the multiplication was not working. I figured it was overflow causing issues, but I didn't think to solve that like this.

taking mod at each value helps in keeping the product in the range of 1234567 so computation will be easy.
concept is (a*b*c) mod 1234567==
(a mod 1234567) * (b mod 1234567) * (c mod 1234567)

It's because in this case you know B is already mod C, so you calculate (A mod C * B) mod C. In fact, mike's code started with 1, so it's actually ((1 * A) mod C * B) mod C

## Connecting Towns

You are viewing a single comment's thread. Return to all comments →

The way modular arithmatic works, you can take the mod of each component of the product before you calculate the product and get the same result as if you were to mod the poduct after multiplying. My code had something like:

Thanks for that explanation. I was wondering why my solution of doing it after the multiplication was not working. I figured it was overflow causing issues, but I didn't think to solve that like this.

why 1234567 why not the other number be taken?

"Output Format Total number of routes from T1 to Tn modulo

1234567"The problem says to output mod 1234567. That is where that number comes from.

but he didnt clearly mention that we have to take mod of each value

taking mod at each value helps in keeping the product in the range of 1234567 so computation will be easy. concept is (a*b*c) mod 1234567== (a mod 1234567) * (b mod 1234567) * (c mod 1234567)

okay..thanks

shouldn't there be product = product%123456 after the loop.

I have studied (A * B) mod C = (A mod C * B mod C)

mod CIt's because in this case you know B is already mod C, so you calculate (A mod C * B) mod C. In fact, mike's code started with 1, so it's actually ((1 * A) mod C * B) mod C