Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

ppastram's blog

By ppastram, history, 4 years ago, In English

Hi everyone, I'm solving problem A from round #575 (div 3). It's a fairly simple problem and I'm getting the right answer when I try the example on visual studio code (using c++) but when I upload my solution, I get the third case wrong (the one with big numbers). Why can this happen?

Useful info: problem: https://codeforces.com/contest/1196/problem/A my code:

include

int main() { int n = 0; std::cin >> n;

for (int i = 0; i < n; i++)
{
    long total = 0;
    long a = 0;
    long b = 0;
    long c = 0;
    std::cin >> a;
    std::cin >> b;
    std::cin >> c;
    total = a+b+c;
    if (int(total)%2==0)
        std::cout<<total/2 <<"\n";
    else
        std::cout<<(total-1)/2 <<"\n";

}
return 0;

}

example entry: 4 1 3 4 1 10 100 10000000000000000 10000000000000000 10000000000000000 23 34 45 my output on vs code (right answer): 4 55 15000000000000000 51 my output when I submit my solution: 4 55 1073741823 0

Thank you very much for your help!

  • Vote: I like it
  • -5
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Use long long instead of just long

#include<bits/stdc++.h>
using namespace std;
int main() { int n = 0; std::cin >> n;

for (int i = 0; i < n; i++)
{
    long long  total = 0;
    long long  a = 0;
    long long b = 0;
    long long c = 0;
    std::cin >> a;
    std::cin >> b;
    std::cin >> c;
    total = a+b+c;
    if ((long long)(total)%2==0)
        std::cout<<total/2 <<"\n";
    else
        std::cout<<(total-1)/2 <<"\n";

}
return 0;
}
  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Thank you very much! I just tried with long long and it worked.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Try long long instead of long. You can read more here. Also please add submission link instead of copy/pasting in the blog, or use the block format for your code:

#include <bits/stdc++.h>

Using ``` before and after it (With enter).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

This is because you are converting long to int. You shouldn't do that since the constraints are pretty big (10^16 — whereas max int can go is something of order 10^9) and then the long value will give just some garbage value. Also, you don't need to do that since modulo will work for longs as well. Check this solution of mine for a better and simpler implementation (You should take advantage of the truncating behaviour of C++ int division :)).

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Thank you very much, but I can't see the solution you're suggesting!

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Use long long instead of long. Because size of long in codeforces compiler is 4 bytes = ( 2147483648 ). try with below code.

updated