Feynman Editorial or Solution SPOJ OR How many Different Squares are there in N*N Grid -UCS - Unleash-Coding-Skills

Saturday, 5 May 2018

Feynman Editorial or Solution SPOJ OR How many Different Squares are there in N*N Grid -UCS

The link for the problem "Feynman"  is :

http://www.spoj.com/problems/SAMER08F/

The solution is very simple to consider the following example:

let we consider the different dimension squares:
                                     Total Number of Square formed
1 * 1 square grid has -        1
2* 2  square grid has -        5
3 * 3 square grid has -       14
...
..
..
From this, we can deduce a formula for n*n square grid has
 => (n*(n+1)*(2*n+1))/6; number of squares.

The complete CPP solution for the problem "Feynman":


#include <iostream>
using namespace std;
int main() {
long long int i,j,n,ans;
while(1)
{
    cin>>n;
    if(n==0)
    break;
    ans=(n*(n+1)*(2*n+1))/6;
    cout<<ans<<endl;
}
 return 0;
}


Happy Coding............

No comments:

Post a comment