# Connecting Towns

# Connecting Towns

- S
stzsch + 1 comment 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 }

- JC
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.

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

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

- PG
piyush1751995 + 0 comments shouldn't there be product = product%123456 after the loop.

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

**mod C**

nilkun + 2 comments Snippet.

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

- MO
Marcinho + 0 comments Awesome solution! Thank you :)

- PG
piyush1751995 + 0 comments take remainder directly

c650Alpha + 0 comments Those were some biga$$ numbers.

- AS
ROCKET_RONALDO + 1 comment easy question

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int t; cin>>t; while(t--) { int n; cin>>n; long long sum=1; for(int i=0;i<n-1;i++) { long long x; cin>>x; sum=((sum%1234567)*(x%1234567))%1234567; } cout<<sum<<endl; } return 0; }

gbekosa + 1 comment Why are you using mod 1234567 3 times? You only need to use it once after multiplying sum by x.

sum = (sum * x) % 1234567;

- LL
Learner_College + 1 comment BCoz it is a modulo property. (a*b)%c=((a%c)*(b%c))%c

AdarkTheCodr + 0 comments That is the property, yes, but you can remove the first two modulos because a)

`x`

is between 1 and 1000, so`x modulo 1234567`

is pointless, and b) modulo-ing`sum`

before it is used in the equation*and*before assigning the result back to`sum`

is redundant.

ankitarun + 4 comments while multiplying all the elements in a list and printing the output, how is

for i in list: ans *= i print (int(fmod(ans,1234567)))

and

print (functools.reduce(op.mul, s, 1) % 1234567)

different. As, in one case solution is being accepted while in another it just passes only one testcase.

- MS
manvee_2603 + 1 comment why we are using mod1234567

RSTHW + 0 comments read format output again

- MS
manvee_2603 + 0 comments why we are using mod1234567

- J
johnlr + 0 comments `fmod`

returns a float, so you will print extra`.0`

on each term - JL
quick_dudley + 0 comments Forgetting to output mod some number is the #1 mistake I make on hackerrank problems.

- SD
sairajd044 + 0 comments Note the problem asks the total route mod 1234567 and not the total route itself .

def connectingTowns(n, routes): res = 1 for i in routes: res *= 1 return res % 1234677

- PB
15bit003 + 0 comments what's wrong with this code?

`static int connectingTowns(int n, int[] routes) { int mul=1; for(int i = 0;i<n-1;i++){ if(routes[i]==1) continue; else mul*=routes[i]; } return mul%1234567; }`

shibaayanm + 0 comments Anyone can use this in python3

def connectingTowns(n, routes): result = 1 for i in range(len(routes)): result = result*routes[i]%1234567 return result

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;

Sort 72 Discussions, By:

Please Login in order to post a comment