2018年上海金馬五校程式設計競賽 E題
題目
Description
Here is an n×m grid, which is made up of 1×1 lattices. You can find many rectangles with different sizes in the grid. Can you calculate the total area of all rectangles?
For example, there is a 2×4 grid in the following figure, and the answer is 80, which equals to 8×1 + 6×2 + 4×3 + 2×4 + 4×2 + 3×4 + 2×6 + 1×8. That means there are eight 1×1, six 1×2, four 1×3, two 1×4, four 2×1, three 2×2, two 2×3 and one 2×4 rectangles.
Input
There are several test cases.
Each case contains two integers n and m (1 ≤ n, m ≤ 100), denoting the height and width of the grid.
Output
For each test case, print one line containing the total area of all rectangles in the grid.
Sample Input
1 1 2 4
Sample Output
1 80
舉個3*3例子吧
9*1*1 6*1*2 3*1*3
6*2*1 4*2*2 2*2*3
3*3*1 2*3*2 1*3*3
第一行9=(3-1+1)*(3-1+1)
6=(3-1+1)*(3-1+2)
3=(3-1+1)*(3-1*3)
以此類推得到一個公式
從h*m裡面選一個a*b的
個數c=(h-a+1)*(m-b+1)------找位置
程式碼如下
PS:這道題本身不難,但是找位置,從他的幾何意義來考慮的思想很好
補充:印象中還有一個題說的是n*n的正方形被分成n*n份
問一共有多少個正方形?
這個題只有一個變數,可以考慮數列。