📜  N * M网格中具有给定面积的矩形数

📅  最后修改于: 2021-04-17 18:56:51             🧑  作者: Mango

给定三个正整数NMA ,任务是计算M * N网格中面积等于A的矩形的数量。

例子:

方法:可以根据以下观察结果解决问题:

请按照以下步骤解决问题:

  • [1,√A]范围内迭代。对于i迭代,在给定网格内找到矩形的长度和宽度的所有可能值,例如{i,(A / i)}{(A / i),i}
  • 遍历所有可能的长度值X和宽度Y,并将矩形的计数增加(M – X + 1)*(N – Y + 1)
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the count of rectangles
// in an M * N grid such that the area of
// the rectangles is equal to A
int count_number(int N, int M, int A)
{
 
    // Stores all possible values of length
    // and breadth whose area equal to A
    vector > v;
 
    // Calculate all divisors of A
    for (int i = 1; i * i <= A; i++) {
 
        // If N is divisible by i
        if (N % i == 0) {
 
            // Stores length of the rectangle
            int length = i;
 
            // Stores breadth of the rectangle
            int breadth = A / i;
 
            // If length of rectangle is not
            // equal to breadth of rectangle
            if (length != breadth) {
 
                // Insert { length, breadth }
                v.push_back({ length, breadth });
 
                // Insert { breadth, length }
                v.push_back({ breadth, length });
            }
            else {
 
                // Insert { length, breadth}
                // because both are equal
                v.push_back({ length, breadth });
            }
        }
    }
 
    // Stores the count of rectangles
    // in a grid whose area equal to A
    long long total = 0;
 
    // Iterate over all possible
    // values of { length, breadth }
    for (auto it : v) {
 
        // Stores total count of ways to
        // select a segment of length it.first
        // on the segment of length M
        int num1 = (max(0, M - it.first + 1));
 
        // Stores total count of ways to
        // select a segment of length it.second
        // on the segment of length N
        int num2 = (max(0, N - it.second + 1));
 
        // Update total
        total += (num1 * num2);
    }
 
    return total;
}
 
// Drivers Code
int main()
{
 
    // Input
    int N = 2, M = 2, A = 2;
 
    // Print the result
    cout << count_number(N, M, A) << endl;
}


Java
// Java program of the above approach
import java.util.*;
class GFG
{
     
static class pair
{
    int first, second;
    public pair(int first, int second) 
    {
        this.first = first;
        this.second = second;
    }   
}
   
// Function to find the count of rectangles
// in an M * N grid such that the area of
// the rectangles is equal to A
static int count_number(int N, int M, int A)
{
 
    // Stores all possible values of length
    // and breadth whose area equal to A
    Vector v = new Vector();
 
    // Calculate all divisors of A
    for (int i = 1; i * i <= A; i++)
    {
 
        // If N is divisible by i
        if (N % i == 0)
        {
 
            // Stores length of the rectangle
            int length = i;
 
            // Stores breadth of the rectangle
            int breadth = A / i;
 
            // If length of rectangle is not
            // equal to breadth of rectangle
            if (length != breadth)
            {
 
                // Insert { length, breadth }
                v.add(new pair(length, breadth));
 
                // Insert { breadth, length }
                v.add(new pair(breadth, length));
            }
            else
            {
 
                // Insert { length, breadth}
                // because both are equal
                v.add(new pair(length, breadth));
            }
        }
    }
 
    // Stores the count of rectangles
    // in a grid whose area equal to A
    int total = 0;
 
    // Iterate over all possible
    // values of { length, breadth }
    for (pair it : v)
    {
 
        // Stores total count of ways to
        // select a segment of length it.first
        // on the segment of length M
        int num1 = (Math.max(0, M - it.first + 1));
 
        // Stores total count of ways to
        // select a segment of length it.second
        // on the segment of length N
        int num2 = (Math.max(0, N - it.second + 1));
 
        // Update total
        total += (num1 * num2);
    }
    return total;
}
 
// Drivers Code
public static void main(String[] args)
{
 
    // Input
    int N = 2, M = 2, A = 2;
 
    // Print the result
    System.out.print(count_number(N, M, A) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program of the above approach
 
# Function to find the count of rectangles
# in an M * N grid such that the area of
# the rectangles is equal to A
def count_number(N, M, A):
 
    # Stores all possible values of length
    # and breadth whose area equal to A
    v = []
 
    # Calculate all divisors of A
    for i in range(1, A + 1):
 
        if i * i > A:
            break
 
        # If N is divisible by i
        if (N % i == 0):
 
            # Stores length of the rectangle
            length = i
 
            # Stores breadth of the rectangle
            breadth = A // i
 
            # If length of rectangle is not
            # equal to breadth of rectangle
            if (length != breadth):
 
                # Insert { length, breadth }
                v.append([length, breadth ])
 
                # Insert { breadth, length }
                v.append([breadth, length ])
            else:
                # Insert { length, breadth}
                # because both are equal
                v.append([length, breadth ])
 
    # Stores the count of rectangles
    # in a grid whose area equal to A
    total = 0
 
    # Iterate over all possible
    # values of { length, breadth }
    for it in v:
 
        # Stores total count of ways to
        # select a segment of length it.first
        # on the segment of length M
        num1 = (max(0, M - it[0] + 1))
 
        # Stores total count of ways to
        # select a segment of length it.second
        # on the segment of length N
        num2 = (max(0, N - it[1] + 1))
 
        # Update total
        total += (num1 * num2)
    return total
 
# Drivers Code
if __name__ == '__main__':
 
    # Input
    N, M, A = 2, 2, 2
 
    # Prthe result
    print(count_number(N, M, A))
 
# This code is contributed by mohit kumar 29.


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
 
  public class pair 
  { 
    public int first, second; 
    public pair(int first, int second) 
    { 
      this.first = first; 
      this.second = second; 
    } 
  }
 
  // Function to find the count of rectangles
  // in an M * N grid such that the area of
  // the rectangles is equal to A
  static int count_number(int N, int M, int A)
  {
 
    // Stores all possible values of length
    // and breadth whose area equal to A
    List v = new List();
 
    // Calculate all divisors of A
    for (int i = 1; i * i <= A; i++)
    {
 
      // If N is divisible by i
      if (N % i == 0)
      {
 
        // Stores length of the rectangle
        int length = i;
 
        // Stores breadth of the rectangle
        int breadth = A / i;
 
        // If length of rectangle is not
        // equal to breadth of rectangle
        if (length != breadth)
        {
 
          v.Add(new pair(length, breadth));
 
          // Insert { breadth, length }
          v.Add(new pair(breadth, length));
        }
        else
        {
 
          // Insert { length, breadth}
          // because both are equal
          v.Add(new pair(length, breadth));
        }
      }
    }
 
    // Stores the count of rectangles
    // in a grid whose area equal to A
    int total = 0;
 
    // Iterate over all possible
    // values of { length, breadth }
    foreach (pair it in v)
    {
 
      // Stores total count of ways to
      // select a segment of length it.first
      // on the segment of length M
      int num1 = (Math.Max(0, M - it.first + 1));
 
      // Stores total count of ways to
      // select a segment of length it.second
      // on the segment of length N
      int num2 = (Math.Max(0, N - it.second + 1));
 
      // Update total
      total += (num1 * num2);
    }
    return total;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    // Input
    int N = 2, M = 2, A = 2;
 
    // Print the result
    Console.Write(count_number(N, M, A) +"\n");
  }
}
 
// This code is contributed by susmitakundugoaldang


输出:
4

时间复杂度: O(sqrt(N))
辅助空间: O(sqrt(N))