Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

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

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

My code is given WA verdict on G++ 17, and G++ 14, but the same code is accepted on G++ 20 can anyone tell me how this is possible? Question Link My Answer(accepted) My Answer(WA)

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

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

It's not about C++14, C++17 vs C++20. It's about 32 bits vs 64 bits.

It seems to me that the failing test case has all bits of the sequence equal to 1. This means that the line printing the answer is this:

if(gate == 0){
    cout << s.size()*s.size() << endl;
}

Notice that the C++14 and C++17 submissions you made were on 32 bits, while the C++20 submission was on 64 bits (compare the language names: GNU C++17 vs GNU C++20 (64); here 32 bits is assumed if 64 bits isn't specified). If the submission is made on 32 bits, vectors and other containers store their sizes as an unsigned 32-bit integer (i.e. the same as an unsigned int). On 64 bits, the size of containers is stored as an unsigned 64-bit integer (i.e. the same as an unsigned long long). Thus, on 32 bits the multiplication will overflow.

There are a few ways to fix this:

  • Cast to long long (recommended)
  • Choose a 64-bit language on submission (there is a 64-bit C++17 option availible if you don't want to use C++20 for some reason)

Here are some submissions for reference: