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

Автор ppastram, история, 4 года назад, По-английски

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!

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

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

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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

updated