# Connecting Towns

# Connecting Towns

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 }

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

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**

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

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;

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.

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

driftwood + 0 comments # For C++

Perform mod after every multiplication or you'll exceed size limit

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.

manvee_2603 + 1 comment why we are using mod1234567

RSTHW + 0 comments read format output again

manvee_2603 + 0 comments why we are using mod1234567

johnlr + 0 comments `fmod`

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

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

Aakkiii + 2 comments Why is it not working?? Only Non-Hidden Test Case are working -_-

static int fact(int n) { int fact=1; while(n!=0) { fact=fact*n; n--; } return fact; }

`static int perm(int n) { return (int)fact(n)/fact(n-1); } static int connectingTowns(int n, int[] routes) { /* * Write your code here. */ int result=1; for(int i=0;i<routes.length;i++) { int per=perm(routes[i]); //System.out.println(+per); result=result*per; } return result%1234567; }`

Aakkiii + 0 comments Please Help Me!!!!!

fodorfa + 0 comments you need to use mod 1234567 in your for cycle after multiplication or else your int will become way too big.

The problem uses huge numbers and their multiplications will quickly become larger than the MAX interger any programing language uses.

moazashraf007 + 0 comments JAVA SOLUTION

import java.util.Scanner; class ConnectingTowns { public static void main(String[] args) { short testCase, routes; byte towns; int possibleRoutes; Scanner sc = new Scanner(System.in); testCase = sc.nextShort(); while(testCase-- > 0) { towns = sc.nextByte(); possibleRoutes = 1; for(byte i = 1; i < towns; i++) { routes = sc.nextShort(); possibleRoutes *= routes; possibleRoutes %= 1234567; } System.out.println(possibleRoutes); } } }

Sort 79 Discussions, By:

Please Login in order to post a comment