Can anyone help me how to solve this problem?
To make it easy to understand look at this image first
So ,The total for any NxN square is (N squared + N-1 squared + N-2 sqaured down to 1 when N=1.
so we need all the summation of all the sqaures 1 to N.
And For the Sum Of the First N Squares is
S = n(n+1)(2n+1)/6
[You read it about here. its just maths]
(summation - How to get to the formula for the sum of squares of first n numbers? - Mathematics Stack Exchange)
thats it .hope it helps.
My code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll t;
cin>>t;
while(t--)
{
ll N,sum=0;
cin>>N;
sum=(N * (N + 1) * (2*N + 1)) / 6 ;//sum of n squares
cout<<sum<<endl;
}
return 0;
}
1 Like
Thanks, Bro.Got it.
i was thinking abt it …
thanks bro