计算三元组,使得两个数字与第三个数字相加的乘积为 N
给定一个正整数N ,任务是找到三元组(A, B, C)的数量,其中A , B , C是正整数,使得两个数字与第三个数字相加的乘积为N ,即A * B + C = N。
例子:
Input: N = 3
Output: 3
Explanation:
Following are the possible triplets satisfying the given criteria:
- (1, 1, 2): The value of 1*1 + 2 = 3.
- (1, 2, 1): The value of 1*2 + 1 = 3.
- (2, 1, 1): The value of 2*1 + 1 = 3.
Therefore, the total count of such triplets is 3.
Input: N = 5
Output: 8
方法:给定的问题可以通过将方程A * B + C = N重新排列为A * B = N – C来解决。现在,满足上述方程的唯一可能值A和B是N – C的除数。例如,如果N – C = 18的值,有 6 个除数,即 1, 2, 3, 6, 9, 18。因此,满足上述等式的A、B的值是: (1, 18), ( 2, 9), (3, 6), (6, 3), (9, 2), (18, 1) 。因此,对于N – C = 18的值, A和B的可能值为6 ,即N – C(= 18)的除数数。请按照以下步骤解决给定的问题:
- 初始化一个变量,比如count为0 ,它存储可能的三元组的总数。
- 使用变量i在[1, N – 1]范围内迭代一个循环,对于每个值,我找到(N – i)的除数的总数并将其添加到变量count中。
- 完成上述步骤后,将count的值打印为三元组的结果数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the divisors of
// the number (N - i)
int countDivisors(int n)
{
// Stores the resultant count of
// divisors of (N - i)
int divisors = 0;
int i;
// Iterate over range [1, sqrt(N)]
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
divisors++;
}
}
if (i - (n / i) == 1) {
i--;
}
for (; i >= 1; i--) {
if (n % i == 0) {
divisors++;
}
}
// Return the total divisors
return divisors;
}
// Function to find the number of triplets
// such that A * B - C = N
int possibleTriplets(int N)
{
int count = 0;
// Loop to fix the value of C
for (int i = 1; i < N; i++) {
// Adding the number of
// divisors in count
count += countDivisors(N - i);
}
// Return count of triplets
return count;
}
// Driver Code
int main()
{
int N = 10;
cout << possibleTriplets(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the divisors of
// the number (N - i)
static int countDivisors(int n)
{
// Stores the resultant count of
// divisors of (N - i)
int divisors = 0;
int i;
// Iterate over range [1, sqrt(N)]
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
divisors++;
}
}
if (i - (n / i) == 1) {
i--;
}
for (; i >= 1; i--) {
if (n % i == 0) {
divisors++;
}
}
// Return the total divisors
return divisors;
}
// Function to find the number of triplets
// such that A * B - C = N
static int possibleTriplets(int N)
{
int count = 0;
// Loop to fix the value of C
for (int i = 1; i < N; i++) {
// Adding the number of
// divisors in count
count += countDivisors(N - i);
}
// Return count of triplets
return count;
}
// Driver Code
public static void main (String[] args) {
int N = 10;
System.out.println(possibleTriplets(N));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python program for the above approach
import math
# function to find the divisors of
# the number (N - i)
def countDivisors(n):
# Stores the resultant count of
# divisors of (N - i)
divisors = 0
# Iterate over range [1, sqrt(N)]
for i in range(1, math.ceil(math.sqrt(n))+1):
if n % i == 0:
divisors = divisors+1
if (i - (n / i) == 1):
i = i-1
for i in range(math.ceil(math.sqrt(n))+1, 1, -1):
if (n % i == 0):
divisors = divisors+1
# Return the total divisors
return divisors
# def to find the number of triplets
# such that A * B - C = N
def possibleTriplets(N):
count = 0
# Loop to fix the value of C
for i in range(1, N):
# Adding the number of
# divisors in count
count = count + countDivisors(N - i)
# Return count of triplets
return count
# Driver Code
# Driver Code
if __name__ == "__main__":
N = 10
print(possibleTriplets(N))
# This code is contributed by Potta Lokesh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the divisors of
// the number (N - i)
static int countDivisors(int n)
{
// Stores the resultant count of
// divisors of (N - i)
int divisors = 0;
int i;
// Iterate over range [1, sqrt(N)]
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
divisors++;
}
}
if (i - (n / i) == 1) {
i--;
}
for (; i >= 1; i--) {
if (n % i == 0) {
divisors++;
}
}
// Return the total divisors
return divisors;
}
// Function to find the number of triplets
// such that A * B - C = N
static int possibleTriplets(int N)
{
int count = 0;
// Loop to fix the value of C
for (int i = 1; i < N; i++) {
// Adding the number of
// divisors in count
count += countDivisors(N - i);
}
// Return count of triplets
return count;
}
// Driver Code
public static void Main()
{
int N = 10;
Console.Write(possibleTriplets(N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
23
时间复杂度: O(N*sqrt(N))
辅助空间: O(1)