CONSTRAINTS OF Maximum Light up Problem

@suryaprak_adm @admin
My solution for the maximum light up in starters13(problem link : Contest Page | CodeChef) was

#include  <bits/stdc++.h>
#define ll long long
using namespace std;

int main(){

    int t;
    cin>>t;
    while(t--){
        int p,a,b,c,x,y;
        cin>>p>>a>>b>>c>>x>>y;

        int anar = p/(b+(a*x));
        int chakri = p/(c+(a*y));

        if(anar>chakri) cout<<anar<<"\n";
        else cout<<chakri<<"\n";

    }

}

This gave WA in 2nd and 3rd Task however when I submitted the exact same code apart from one change that is instead of

int p,a,b,c,x,y;

I changed it to

ll p,a,b,c,x,y;

(basically, I declared my variables in long long int instead of int)
Now the catch here is the problem clearly mentioned that these variables are
1<= P,a,b,c,x,y <= 10^9

(integers less than or equal10^9 can be stored in int right?)

I do not understand why the former code did not work when we know that all these variables do not need long long int.
What am I doing wrong here kindly explain?

but you are using multiplication here a*x which will give int overflow so use long for it.

1 Like

sorry thats where I’m confused, variable anar and chakri are equal to the multiplication a * x and a * y respectively, shouldn’t they be affected? but in this case I have to change the declaration of p,a,b,c,x and y in order for the solution to pass. if I keep the anar and chakri variables as int, the solution is accepted.

this solution is not accepted Solution: 51653124 | CodeChef
it gives WA

but this one does get accepted where I change int p,a,b,c,x,y; to ll p,a,b,c,x,y;

When you multiply int a and int x, (a * x) is calculated as an int. For large inputs, (a * x) overflows the value of int. Then the overflowed int is added to b which then divides p to give your anar and chakri values. Consider the following example:

        int a,b;
        a=1000000000;
        b=1000000000;
        long long int c = a*b;
        cout << c << endl;

        long long int d,e;
        d=1000000000;
        e=1000000000;
        long long int f = d*e;
        cout << f << endl;

Here, the first cout prints a negative value because the multiplication is done on int a and int b which gives an overflowed value which is then cast to long long int but by that time the multiplication has already overflowed, so the value of c is negative. But in the second case we are multiplying two long long ints, so the result doesn’t overflow.

2 Likes

Oh right, now I understand.
Thank you so much, I will take care of this next time :’)