0x0002's blog

By 0x0002, history, 12 days ago, In English

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.

  • Vote: I like it
  • +64
  • Vote: I do not like it

»
11 days ago, # |
  Vote: I like it +4 Vote: I do not like it

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

»
11 days ago, # |
  Vote: I like it +4 Vote: I do not like it

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

»
11 days ago, # |
  Vote: I like it +3 Vote: I do not like it

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

»
10 days ago, # |
  Vote: I like it +1 Vote: I do not like it

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

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
10 days ago, # |
  Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    10 days ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

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

  • »
    »
    10 days ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    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)

  • »
    »
    10 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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;
    }
    
    • »
      »
      »
      10 days ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

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

      • »
        »
        »
        »
        10 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.