给定两个正整数N和X ,任务是计算给定整数X在生成的长度为N 的方阵中出现的次数,矩阵的每个元素都等于其行和列索引的乘积(基于 1索引)。
例子:
Input: N = 5, X = 6
Output: 2
Explanation:
The 2D array formed is equal to the :
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
There are 2 occurrences of the element X(= 6) in the generated array.
Input: N = 7, X = 12
Output: 4
朴素方法:最简单的方法是通过将行索引和列索引相乘来构造给定矩阵以获得每个矩阵元素。生成矩阵后,打印矩阵中X的出现次数。
时间复杂度: O(N 2 )
辅助空间: O(N 2 )
高效方法:为了优化上述方法,该想法基于观察矩阵中的每个元素都是 2 个数字的乘积。因此,通过检查X可以表示为 2 个数字的乘积的方式的数量并选择位于[1, N]范围内的那些对,给出结果。请按照以下步骤解决问题:
- 初始化一个变量,比如count ,以存储X在生成的矩阵中出现的次数。
- 使用变量i迭代范围[1, √X]并执行以下步骤:
- 如果i的值除以X,存储在一个变量除以X用i获得的商,说湾
- 如果i和b的值都在[1, N]范围内,则执行以下步骤:
- 检查i是否等于b 。如果发现为真,则表示X是一个完美的正方形,并且该行和列将出现一次。因此,将count增加1 。
- 否则,这意味着它们将出现两次,一次在一行中,另一次在列中。因此,将count增加2 。
- 完成以上步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the occurrences
// of X in the generated square matrix
int countOccurrences(int N, int X)
{
// Stores the required result
int count = 0;
//Iterate upto square root of X
for (int i = 1; i < sqrt(X); i++)
{
// Check if i divides X
if (X % i == 0)
{
// Store the quotient obtained
// on dividing X by i
int a = i;
int b = X / i;
// If both the numbers fall in
// the range, update count
if (a <= N && b <= N)
{
if (a == b)
count += 1;
else
count += 2;
}
}
}
// Return the result
return count;
}
// Driver code
int main()
{
// Given N and X
int N = 7;
int X = 12;
// Function Call
cout << countOccurrences(N, X);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to count the occurrences
// of X in the generated square matrix
static int countOccurrences(int N, int X)
{
// Stores the required result
int count = 0;
// Iterate upto square root of X
for (int i = 1; i < Math.sqrt(X); i++) {
// Check if i divides X
if (X % i == 0) {
// Store the quotient obtained
// on dividing X by i
int a = i;
int b = X / i;
// If both the numbers fall in
// the range, update count
if (a <= N && b <= N) {
if (a == b)
count += 1;
else
count += 2;
}
}
}
// Return the result
return count;
}
// Driver code
public static void main(String[] args)
{
// Given N and X
int N = 7;
int X = 12;
// Function Call
System.out.println(countOccurrences(N, X));
}
}
// This code is contributed by Kingsh.
Python3
# Python3 program for the above approach
from math import sqrt
# Function to count the occurrences
# of X in the generated square matrix
def countOccurrences(N, X):
# Stores the required result
count = 0
# Iterate upto square root of X
for i in range(1, int(sqrt(X))+1):
# Check if i divides X
if X % i == 0:
# Store the quotient obtained
# on dividing X by i
a = i
b = X//i
# If both the numbers fall in
# the range, update count
if a <= N and b <= N:
if a == b:
count += 1
else:
count += 2
# Return the result
return count
# Driver Code
if __name__ == '__main__':
# Given N and X
N = 7
X = 12
# Function Call
print(countOccurrences(N, X))
C#
// C# program for above approach
using System;
public class GFG
{
// Function to count the occurrences
// of X in the generated square matrix
static int countOccurrences(int N, int X)
{
// Stores the required result
int count = 0;
// Iterate upto square root of X
for (int i = 1; i < Math.Sqrt(X); i++) {
// Check if i divides X
if (X % i == 0) {
// Store the quotient obtained
// on dividing X by i
int a = i;
int b = X / i;
// If both the numbers fall in
// the range, update count
if (a <= N && b <= N) {
if (a == b)
count += 1;
else
count += 2;
}
}
}
// Return the result
return count;
}
// Driver code
public static void Main(String[] args)
{
// Given N and X
int N = 7;
int X = 12;
// Function Call
Console.Write(countOccurrences(N, X));
}
}
// This code is contributed by code_hunt.
Javascript
输出:
4
时间复杂度: O(√X)
辅助空间: O(1)