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

Автор _Edmond_, история, 2 года назад, По-русски

Hello! Today I submitted the exact same code in C++20(64) and C++ 17. In C++ 17 it passed but wrong answer in C++20(64).

173606485 C++ 17 — ACCEPTED. 173606464 C++ 20 (64) — WRONG ANSWAR.

How to understand this?

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

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

Maybe it is because pow
I tried 'GNU C++17 (64)',and it returned WA too.

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

Since the pow function uses and returns double value, with big numbers it can have a slight error, which gave you the WA verdict (You can see in your C++20 submission, the answers are only off by 16: 101159538130177904 vs 101159538130177920). I converted the types to long double and it got accepted: 173634899

For why did the C++17 solution get accepted, I have no idea. Maybe it's because of their different ways of compiling code?

More on this, you shouldn't use the pow function for integers for the same reason: Instead of pow(a, 2), it's recommended to use a*a. Your same code using a*a: 173635181

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

    Yes. Floating point numbers work in mysterious ways in C++(32 bit). With C++(64 bit) they behave a lot more sane.

    For example submitting

        double b = pow(a, 2);
        a = b / 120;
        cout << a;
    

    gives AC with C++17(32 bit) 173799443.

    While submitting

        double b = pow(a, 2);
        pow(b, 10);
        a = b / 120;
        cout << a;
    

    gives WA with C++17(32 bit) 173799415.

    Dealing with floating point numbers in C++(32 bit) almost feels like quantum physics. Just looking at them can change the result.