ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

gcd function ( C++ )
Posted by Mesh 7 Jul 2008 04:08
I know that, at least in GNU compiler ( but it should be standard ) there is ( in cmath, i guess ) a gcd function, called __gcd. But here isn't compilingl why? What should I use instead?
Re: gcd function ( C++ )
write your own gcd() function - it's only three lines of code...
Re: gcd function ( C++ )
Posted by Mesh 7 Jul 2008 04:47
I know it's short, but I never remember those damn 3 lines ;P Ok, I'll do that do get the problem accepted, but it would be nice to have a prewritten gcd...
Re: gcd function ( C++ )
This is very easy algo - if you want to be a good programmer, you should know by heart this and more complicated algorithms. The main idea is: while a>0 & b>0 you subtract smaller number from larger (euclid algorithm), but to speed it up we use %:

int gcd (int a, int b){
 while ((a > 0) && (b > 0))
  if (a > b) a %= b;
  else b %= a;
 return a+b;
};

or, recursively (but slightly slower):

int gcd (int a, int b){
 if (b == 0) return a;
 else return gcd (b, a % b);
};

Edited by author 07.07.2008 05:06
Re: gcd function ( C++ )
Posted by Mesh 7 Jul 2008 06:23
Yep, but while I like logic and consequently math, I never really studied hard (but as a child I was a prodigy XD) :P But this october I'm gonna start university, so I promised I'll study it... Anyway, I know it's out of topic, but do you know a way to, given an element, know what's its position inside a multiset? I was solving problem 1028, and it would be very easy if I could do that...