# Getting WA Codeforces 642(Div. 3) Question C

This Code gives correct output -->

``````#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long int
int main(){

int t;
cin>>t;
while(t--){

int n;
cin>>n;

if(n == 1){
cout<<0<<endl;
continue;
}
ll ans = 0;
for(int i = 1; i <= n/2; i++){
ans += (i*1LL*i);
// Gives WA when write
// ans += (i*i*1LL);     Is order of 1LL matters ??
}

cout<<ans*8<<endl;

}
return 0;
}

``````

But this gives WA (Can check for sample test Cases)
sample test Case:
3
1
5
499993

``````#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long int
int main(){

int t;
cin>>t;
while(t--){

int n;
cin>>n;

if(n == 1){
cout<<0<<endl;
continue;
}
ll ans = 0;
for(int i = 1; i <= n/2; i++){
ans += (8*i*i*1LL);
}

cout<<ans<<endl;

}
return 0;
}
``````

Am unable to figure out the mistake.
Can someone help me , It will be appreciated.Thank You.

2. There’s literally only one thing this can be considering how small the change is, and it’s overflow. Be careful with `int` (it’s generally not a bad idea to never use it). Your first submission passes because the 1LL casts it to a `long long`, which can fit the numbers.
1 Like

Also getting WA when write
ans += (i * i * 1LL); Is order of 1LL matters ??
ans += (i * 1LL * i);

I’m really glad you look at your posts and see how the forum software screws up code

Yes, it does matter. In the first version, it performs the multiplication of `i*i` first, and since `i` is an `int` both times, it acts as if it’s doing multiplication with `int`s. With the second order, the `long long` datatype wins the conversion and the copy of `i` gets converted to a `long long` before the multiplication.

1 Like

I think it matters i is an integer and (i * i) will be a large value and can give integer overflow. If you write i*1LL then that will be converted to long long int so it won’t give any overflow. Hope this makes you understand.

1 Like

In case of i * i * 1LL , first both i are multiplied and multiplication of two integers result in int. So, overflow occured and now you are changing into long which is of no use because overflow has occured.

In case of i * 1LL * i first int is converted into long then multiplication is done. So no overflow

2 Likes

Thank all of you to help me to pointing out my mistake.

Yaa,really great insight .Thanks to all the replies here

#include
using namespace std;
main()
{
long long int n,sum,t,i;
cin>>t;
while(t–)
{
cin>>n;
sum=0;
if(n==1)
{
sum=0;
}
else{
for(i=1;i<=n/2;i++)
{
sum+=8*(i*i);
}
}

``````	cout<<sum<<endl;

}
``````

}
This is my code that.