Блог пользователя beginner_boy

Автор beginner_boy, история, 3 года назад, По-английски

Hello everyone, i want to present a very strange situation that happened with me on problem 1500B. I've tried to solve it by almost 3 hours, and cant figure out why my code was failing on test 11, and so i discovered that for some reason the problem was here:

ll lcm(ll n, ll m){
      return n * (m / __gcd(n, m));
}
...
int n, m;
ll k;
 
scanf("%d %d %lld", &n, &m, &k);
	
ll l = lcm(n, m);

the variable l (lcm value) was given overflow (negative value), but if i change the variable n and m to ll (int64) it works as expected and i got acc. Is it right ? i mean, if i declare n and m as int32, but inside lcm i expect values as int64, this should be converted, and the return should not overflow as well, right ?

Here is my WA submission 111601427, and the accepted 111601468

Can anyone tell me why this is happening ? Thanks in advance.

  • Проголосовать: нравится
  • +38
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +174 Проголосовать: не нравится

There is an inbuilt function for lcm with int 32. The function you defined has int 64 arguments. But you call the lcm function with int 32 arguments. So it's identified as a call to the inbuilt lcm with int32 arrguments

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Now it makes sense, thanks!

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится -6 Проголосовать: не нравится

      It is very simple to fix this error without changing the type of $$$n$$$ and $$$m$$$ to long long; just remove the using namespace std; statement, and add std:: explicit prefix to pair<ll,ll> and __gcd. 111637673

»
3 года назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

There is std::lcm since C++17. You can simply use one from C++ standard library. https://en.cppreference.com/w/cpp/numeric/lcm