trace-of-a-matrix-36

Given Two matrix A and B of some order ** RXC. Both matrix contains elements from  1 to *RC** . Matrix A contains elements in Row-major order while Matrix B contains elements in Column-major order .you are asked to answer a very simple question what is the trace of the matrix formed by the addition of A and B. 
Here, Trace of the matrix is defined as  P[1][1] + P[2][2]... + P[min(n,m)] [min(n,m)]  for any rectangular matrix P.

Input:

First line of input contains T denoting number of test cases. Each test case consists of single line only containing two integers denoting order of matrix  ** R and C **.

Output:

Output consists of T lines each containing answer to the corresponding test cases.

Constraints:

 1 ≤ T ≤ 10 ^ 5      
 1 ≤ R,C ≤ 10  ^ 6 

SAMPLE INPUT
2
3 3
1 2

SAMPLE OUTPUT
30
2

Explanation

For first input , 
the two matrices A and B will be :  

    1 2 3 
A = 4 5 6 
    7 8 9

    1 4 7
B = 2 5 8
    3 6 9

      2 6 10
A+B = 6 10 14
     10 14 18

There , the trace of (A+B) matrix is 2 + 10 + 18 = 30

Similarly , answer can be found for the second input also.

Source: hackerearth | Difficulty: EASY
Tags: Mathematics

Ad-hoc


Solutions:
'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
t=eval(input())
while t>0:
	t-=1
	r,c=input().split(" ")
	r,c=int(r),int(c)
	m=min(r,c)
	print((m*(2+(m-1)*(r+1))+(m*(2+(m-1)*(c+1))))/2)


for _ in range(eval(input())):
	R,C = list(map(int,input().split()))
	min=R
	if(min>C):
		min=C
	sum=((min)*(2+(min-1)*(R+1)))/2
	sum=sum+((min)*(2+(min-1)*(C+1)))/2
	print(sum)


def find_trace(R,C):
	n = min(R,C)
	d1 = C+1
	d2 = R+1
	sum = n*(2+(n-1)*d1)/2 + n*(2+(n-1)*d2)/2
	return sum
	
	
T = input()
T = int(T)
while(T!=0):
	test = input().split(' ')
	R= int(test[0])
	C = int(test[1])
	print(find_trace(R,C))
	T=T-1


for cases in range(int(input())):
	r, c = list(map(int, input().split()))
	n = min(r, c)
	trace1 = (n*(2+(n-1)*(r+1))) / 2
	trace2 = (n*(2+(n-1)*(c+1))) / 2
	print(trace1 + trace2)


no_of_test_cases = int(input())


for _ in range(no_of_test_cases):
	inp = input().split()
	row,col = int(inp[0]),int(inp[1])

	trace = 0
	#if row > col:
	#    row,col=col,row
	n = min(row,col)
	trace = n*(n+1) + ((row*n*(n-1))/2) + ((col*n*(n-1))/2)
	#for i in range (1,loop_till+1):
	#    trace += 2*(i+(row)*(i-1))
	print(trace)



t = int(input())
counter =0
while counter< t:
	s= input()
	L = s.split(' ')
	r = int(L[0])
	c=int(L[1])
	l = min(r,c)
	rc2 = c+r+2    
	print(((l*(l-1))/2)*(rc2) + l*2)
	counter+=1


from sys import stdin
t = stdin.readline()
a = stdin.readlines()
for i in a:
	n,m = i.strip().split()
	n = int(n); m = int(m)
	if n>m:
		t = n;n=m;m=t
	print(n*(n+1) + (n*(n-1)*(m+n))/2)


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''

tc = int(input())


def solve():
	R,C= [int(i) for i in input().split()]
	
	n = min(R,C) - 1
	
	ans = 2*(n+1) + (R+C+2)*(n*(n+1))/2
	
	return ans



for zz in range(tc):
	print((solve()))


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
t=eval(input())
while t>0:
	t-=1
	r,c=input().split(" ")
	r,c=int(r),int(c)
	m=min(r,c)
	print((m*(2+(m-1)*(r+1))+(m*(2+(m-1)*(c+1))))/2)


t=eval(input())
while t!=0:
	r,c=list(map(int,input().split()))
	if r==c:
		trace=(2+((r-1)*(c+1)))*r
	else:
		if r>c:
			r,c=c,r
		trace1=((2+((r-1)*(c+1)))*r)/2
		trace2=((2+((r-1)*(r+1)))*r)/2
		trace=trace1+trace2
	print(trace)
	t=t-1



t = eval(input())
for i in range(t):
	m,n = input().split()
	m = int(m)
	n = int(n)
	if(m<n):
		l = m
	else:
		l = n
	d = ((m+1) * (l * (l-1))/2) + l
	c = ((n+1) * (l * (l-1))/2) + l
	print(c+d)



for _ in range(int(input())):
	n,m=list(map(int,input().split()))
	p=min(n,m)
	a=(p*(2+(p-1)*(m+1)))/2
	b=(p*(2+(p-1)*(n+1)))/2
	print(a+b)


t=eval(input())
for _ in range(t):
	r,c=list(map(int,input().split()))
	n=min(r,c)
	print(n*(n+1)+(n-1)*n*(r+c)/2)


class Solution(object):
	def solve(self,r,c):
		m=min(r,c)
		return (m*(4+(m-1)*(r+c+2)))/2 
				
if __name__=="__main__":
	tc=int(input())
	while(tc>0):
		r,c=list(map(int,input().split()))
		ans=Solution()
		print(ans.solve(r,c))
		tc-=1