where should I use ULL in after variables?

994 Views Asked by At

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;

2

There are 2 best solutions below

0
eerorika On

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.

0
chux - Reinstate Monica On

... 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.