XDOJ1270 - Rectangle Counting

Description

The length and the width of the following figure are 2. How many rectangles are there in this figure?

There are 9 rectangles in this figure. They are 4 rectangles with each side 1*1, 2 rectangles with each side 1*2, 2 rectangles with each side 2*1 and 1 rectangle with each side 2*2.

 

 

Input

The input consists of several test cases.
   The first line of the input contains a single integer T (0 < T ≤ 20), the number of test cases.
   Then Followed by T lines, each line gives a test case which contains two positive integers N and M, which means the length and the width of the figure( 1≤ N, M ≤ 10 ).

Output

  For each test case, output an integer indicating the number of rectangles in the figure.

Sample Input

2
1 1
2 2

Sample Output

1
9

解题 思路:

因为矩形的长和宽的选择没有相互关系,那么我们可以分别统计矩形的长和宽有多少种选择,然后由乘法原理得出矩形个数。以长的选择为例:网格长为N,则共有N+1个格点,于是长的选择方式为: C(N+1,2)=N*(N+1)/2 同理,宽的选择方式为: C(M+1,2)=M*(M+1)/2 矩形个数为N*(N+1)*M*(M+1)/4

 

#include<iostream>

using namespace std;

int main()
{
    int caseN;
    cin>>caseN;
    while(caseN--)
    {
        int N,M;
        cin>>N>>M;
        cout<<N*(N+1)*M*(M+1)/4<<endl;
    }
    return 0;
}

 

最后欢迎大家访问我的个人网站: 1024s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值