Squares in a Matrix

PROBLEM :

Given a MxN matrix, count the number of squares in the matrix.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two space seperated integers M and N, denoting the size of the Matrix.

Output:
For each test output a single integer denoting the total number of squares.

Constraints:
1<=T<=102
1<=M,N<=10^4

Example:
Input:
2
2 2
4 3

Output:
5
20

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--){
   int r,c ;
   cin>>r>>c ;
 
   long long num=0 ;
   while(r&&c){
       num+=r*c ;
       r-- ;
       c-- ;
   }
   cout<<num<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )