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

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

I was solving problem F of ABC just now. And I got passed with long long, but when I change the type into __int128_t, it got wrong answer in most of the cases. Why is that?

Passed with long long.

Wrong answer with int128.

Upd: It is said that __int128_t can not be the index of array. It may cause wrong answer. So it is always not a good idea to use things like #define int __int128_t.

Upd2: I just tested some other things on AtCoder about __int128_t, it seems template<typename> may work incorrectly with __int128_t. That's from my function read(). I searched for many times in Google but have not found why.

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

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

Auto comment: topic has been updated by 0x0002 (previous revision, new revision, compare).

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

Auto comment: topic has been updated by 0x0002 (previous revision, new revision, compare).

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

Может быть проблема что ты заменяешь тип int — на int128. Если я не ошибаюсь нельзя передавать в функции значении int128 . Из за этого у тебя где в функции int передается значении 32 битной. Копилятор считает что ты передаешь просто тип данных int а не int128

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

Auto comment: topic has been updated by 0x0002 (previous revision, new revision, compare).

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

Auto comment: topic has been updated by 0x0002 (previous revision, new revision, compare).

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

Auto comment: topic has been updated by 0x0002 (previous revision, new revision, compare).

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

I had some problem in problem B as well. If I use islower and isupper function to calculate lowercase and uppercase character count I get Wrong answer but passed without inbuilt function. Is it a problem with compiler?

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

    My submission using islower and the same version of compiler got AC.

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

    Google the cppreference for islower/isupper and check the output of islower('a')/isupper('A'). You'll be surprised how stupid it is.

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

    islower and isupper are old C-style functions which return int with 0 for false and non-zero for true. So you had to write your code as any C programmer: !!islower(ch)

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

    In fact I had the same problem due to this function and got a penalty. The fact is that this function does not return a bool result as we expected. Function islower() returns a non-zero value when it is a lowercase letter. You can check here for details.

    You can try the below code in custom test to see:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main()
    {
    	ios::sync_with_stdio(0), cin.tie(0);
    	cout << islower('A') << "\n"; // returns 0, it is not a lowercase letter
    	cout << islower('a') << "\n"; // returns 2
    	cout << islower('b') << "\n"; // returns 2
    	cout << islower('z') << "\n"; // returns 2
    	return 0;
    }
    
    • »
      »
      »
      2 недели назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      Thanks a lot. On my machine it prints 0, 1, 1, 1 only. Probably different compilers have different implementations?

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

        That is true because the standard only requires to return a non-zero value, but what it really returns may be different in different compilers.