Блог пользователя myst-6

Автор myst-6, история, 5 месяцев назад, По-английски

I'm back with a strange error... I came back on codeforces recently to see that my solution for problem E in the most recent educational codeforces round had failed the system tests.

I had the correct algorithm, and I was stuck debugging my code for ages until I submitted a new piece of code which was almost exactly the same and it got accepted. This gets WA, and this gets AC. The only difference in the codes, is that in the second program, I casted a size_t to a long long before multiplying it to n.

Original: ans += s[i].size() * n;

Modified: ans += (long long) s[i].size() * n;

I wouldn't think it would be an overflow error since size_t is the same as unsigned long long (unless it isn't? — but it is on my machine). This doesn't get marked as an overflow error by the codeforces diagnostics either. What is happening here?

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

»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by myst-6 (previous revision, new revision, compare).

»
5 месяцев назад, # |
  Проголосовать: нравится +35 Проголосовать: не нравится

First of all, you seem to have mixed up "original" and "modified" in your post. From your post it may seem that the long long version failed, while in reality this is the one that passed.

Second, you are right, size_t is not the same as unsigned long long on a 32-bit compiler. You can check it yourself: in a custom invocation, run the following code.

#include <bits/stdc++.h>

using namespace std;

int main() {
    static_assert(sizeof(size_t) == sizeof(unsigned long long));
    return 0;
}

You will see that it doesn't compile due to the failed assertion. If, on the other hand, you choose the C++20 64-bit compiler, it will work fine.