# Connecting Towns

# Connecting Towns

stzsch + 2 comments Pretty annoying, not really a mathematics challenge, but rather a "know-how-to-deal-with-long-integers" challenge.

mike006322 + 4 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:

int product = 1 for (i<n-1) { product = product*T[i]%1234567 }

jeremydcarr + 0 comments 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.

jongod5399 + 0 comments [deleted]maheshvangala191 + 1 comment why 1234567 why not the other number be taken?

mike006322 + 1 comment "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.

carylouder007 + 0 comments i like the post, thanks for href test click here to go

[url=https://www.google.com/] click to go [/url]

www.google.com

i like the post, thanks for sharing..

cfranco + 0 comments If you're failing all but 1 of the test cases, you more than likely did not implement the modulo described in the instructions. In my opinion this problem would be much better if we didn't have to handle data overflow in such a way. In any case it's only 1 extra line of code:

var routeCount=1; for(int i=0;i<routes.Length;i++){ routeCount*=routes[i]; routeCount%=1234567; } return routeCount;

nilkun + 2 comments Snippet.

for(int j = 0; j < towns - 1; j++) { routes *= scanner.nextInt(); for(;;) { if (routes > 1234567) routes = routes - 1234567; else break; } }

Marcinho + 0 comments Awesome solution! Thank you :)

piyush1751995 + 0 comments take remainder directly

Freiling + 1 comment The modulo makes me laugh a bit... they have this story layer with Gandalf traveling between towns to make the question make sense. Then, with no explanation, the answer must be submitted as a modulo.

You weird, Gandalf.

gary_l_hewitt + 0 comments A wizard is modulo exactly what he intends to be.

Sort 108 Discussions, By:

Please Login in order to post a comment