ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Общий форум

gcd function ( C++ )
Послано Mesh 7 июл 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++ )
Послано Vedernikoff Sergey (HSE: EconomicsForever!) 7 июл 2008 04:45
write your own gcd() function - it's only three lines of code...
Re: gcd function ( C++ )
Послано Mesh 7 июл 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++ )
Послано Vedernikoff Sergey (HSE: EconomicsForever!) 7 июл 2008 05:04
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++ )
Послано Mesh 7 июл 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...