0

I was trying to solve a question on codeforces . I have to give input

1000000000 1000000000 1   

to this code

#include <iostream>
using namespace std;
int main()
{
long n,m,a;
cin >> n >> m >>a ;
long b,c;
b = (n%a==0)?(n/a):((n/a)+1);
c = (m%a==0)?(m/a):((m/a)+1);

unsigned long long  d ;
d = b*c  ;
cout<<d;

But it gave me error

Diagnostics detected issues [cpp.clang++-diagnose]: p71.cpp:12:5: runtime error: signed 
integer overflow: 1000000000 * 1000000000 cannot be represented in type 'long'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior p71.cpp:12:5 in

Then I got to know that I should add ULL suffix on a numeric literal. But how should I use ULL in this type of code;

Anmol
  • 1
  • 1
  • 2
    Did you try the simplest solution of making ALL of your variables `unsigned long long`? Unfortunately, codeforces is a just a list of random programming puzzles, and they do not offer any tutorials or learning curriculum for those who wish to learn C++ core fundamentals, in order to implement their coding puzzles correctly. You will find all the integer promotion rules fully explained and itemized only in a good C++ textbook. P.S. `ULL` does not go after any variables, only numeric literals. – Sam Varshavchik Jul 11 '21 at 14:29
  • `b` and `c` are both of type `long`, so `b*c` produces a result of type `long`. If both have value `1000000000`, that multiplication will overflow a `long`, causing undefined behaviour. The fact you do `d = b*c` doesn't change that - the multiplication still produces a `long` (with overflow) which is THEN converted to `unsigned long long` to store it in `d`. One option to fix is to do `d = (unsigned long long)b * c` which explicitly converts `b` to `unsigned long long` and implicitly promotes `c` to `unsigned long long`, so the multiplication produces a `unsigned long long` without overflow. – Peter Jul 11 '21 at 15:01

2 Answers2

1

where should I use ULL in after variables?

You cannot use ULL after variables. It is a suffix for integer literals. You should use ULL after those integer literals that you wish to be of type unsigned long long because that's what the suffix does.

All integer types have an upper (as well as lower) limit. Exact maximum values are specific to each system although the language standard does specify a lower bound as a requirement. If you know that a calculation will exceed the limits of a particular type, then you must use a larger type. If you don't know maximum result of a calculation or you know that it would exceed the limit of the largest integer type, then you must change strategy and not use plain integer types: Instead, you would need to use arbitrary precision arithmetic.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

... where should I use ULL in after variables?

ULL is not applicable to variables.


The type on the left of the = does not affect the calculation on the right.

long b, c;
...
unsigned long long  d;
d = b*c; // long multiplication and long product.

To benefit with the potential extended range of unsigned long long, ensure a long long multiplication. Various approaches.

d = b; // in 2 steps
d *= c;

d = (long long) b * c;  // Cast

d = 1LL * b * c; // let compiler form effect code 

Note that the first above results differ form the 2nd and 3rd when b or c are negative.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256