0L vs 0LL in std::accumulate function in C++

Revision en1, by sky_full_of_stars, 2021-05-23 06:51:49

I was solving this.

I encountered a problem while using std::accumulate.

I noticed that accumulate(v.begin(), v.end(), 0L) behaves differently in sublimText/Ideone and Codeforces.

for example,

int main() {
vector<long long> v{1,-1000000000,1,-1000000000,1,-1000000000};
long long sum = accumulate(v.begin(), v.end(), 0L);
cout<<sum;
}

in sublimeText or Ideone: prints -2999999997 and in Codeforces prints 1294967299.

you can try running this here

AC solution

Wrong solution

however, when 0L is changed to 0LL in accumulate function, both sublime text and CF returns the right sum (-2999999997)

is this expected? can someone help me understand this weird behavior?

Tags std::accumulate, #c++ 17, #codeforces_custom_test, #weird

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English sky_full_of_stars 2021-05-23 06:51:49 1016 Initial revision (published)