Intolighter
Intolighter

Reputation: 180

Can't pass the lcm solution task in the Yandex Contest competition

This is my source code, which I try to pass to the least common multiplier task:

#include <iostream>
#include "algorithm"

using namespace std;
using ll = long long;
using ld = long double;

long long gcd(int a, int b) {
    while (b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}

long long lcm(int a, int b) {
    return a / gcd(a, b) * b;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int x, y;
    cin >> x >> y;
    cout << lcm(x, y);
}

I get a Wrong Answer 11: enter image description here

What's wrong with it? I tried many times. Please, help me!

Upvotes: 0

Views: 99

Answers (1)

Sakib Md Al Amin
Sakib Md Al Amin

Reputation: 334

what about negative numbers? Try calculating the GCD(abs(x), abs(y)), it's the same as GCD(x, y)

Upvotes: 1

Related Questions