HDOJ(HDU) 2524 矩形A + B(推导公式、)

Problem Description

给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格.

Input

第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100).

Output

每行输出网格中有多少个矩形.

Sample Input

2

1 2

2 4

Sample Output

3

30

此方格其实就是求其中所有格子数,如果按宽度来算的话,1,2,3,…m,种情况,对每一种情况,有(1+2+3+…+n)个,所以归纳起来应该是(1+2+3+…+m)*(1+2+3+…+n)个,所以有了上面的代码,属于简单的数论题

有n行和m列。

如果只看一行的话,它有多少个矩形呢?单个地数有m个,两个地数有m-1个……,m个地数有1个。

每一行就有:1+2+3+……+m个=m * (m + 1) / 2。

我们把每一行抽象成一个矩形,也就只剩一列了。一列的话,有:1+2+……+n=n * (n + 1) / 2个。

总结起来,就有:(1+m)* m/2 * (1+n)*n/2那么多个了。

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int n =sc.nextInt();
            int m =sc.nextInt();
            System.out.println(n*m*(n+1)*(m+1)/4);
        }

    }

}
时间: 2024-07-31 14:25:58

HDOJ(HDU) 2524 矩形A + B(推导公式、)的相关文章

hdu 2524 矩形 A+B

http://acm.hdu.edu.cn/showproblem.php?pid=2524 提示: 当只有一行的时候,矩形的个数是m+(m-1)+-+1=m*(m-1)/2; 所以当有n行的时候就是n个m行所以就是m*(m-1)/2*n*(n-1)/2; #include <iostream> using namespace std; int main() { int t,n,m; cin>>t; while(t--) { cin>>m>>n; cout&

HDU 2524 矩形A + B 解题心得

原题: Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格.  Input 第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100). Output 每行输出网格中有多少个矩形. Sample Input 2 1 2 2 4 Sample Output 3 30 我的代码: #include<iostream> #include<cstdio>

HDU -2524 矩形A + B

找规律题,这种题目比较巧妙,要仔细观察找出规律 1. 假设只有一行,一共有n列,那么由一个小矩形构成的矩形个数为n, 由两个小矩形构成的矩形个数为 n - 1个 .... 由 n 个小矩形构成的矩形个数为1个,所以所有的矩形个数就是 n + (n - 1) + (n - 2) + ...+ 2 + 1 = n * (n + 1) / 2 2. 同理假设只有一列, 可以构成的矩形数目也是 n * (n + 1) / 2 所以, 总的矩形个数,就是行构成的矩形总数 * 列构成的矩形总数 代码如下 1

HDOJ/HDU 1297 Children’s Queue(推导~大数)

Problem Description There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

bnu 34985 Elegant String(矩阵快速幂+dp推导公式)

Elegant String Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory      2-SAT     Articulation/Bridge/Biconnected Component      Cy

HDOJ/HDU 2140 Michael Scofield&#39;s letter(字符转换~)

Problem Description I believe many people are the fans of prison break. How clever Michael is!! In order that the message won't be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the he

HDOJ/HDU 1161 Eddy&#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

向量点乘叉乘推导公式

点乘 推导公式1: a•b = ax*bx + ay*by =  (|a|*sinθ1) * (|b| * sinθ2) +   (|a| * cosθ1) * (|b| * cosθ2) = |a||b|(sinθ1*sinθ2 + cosθ1*cosθ2) =|a||b|(cos(θ1-θ2)) = |a||b|cosθ 推导公式2: 几何意义是:是一条边向另一条边的投影乘以另一条边的长度 叉乘: 原文地址:https://www.cnblogs.com/honghong87/p/11517