给定一个整数N ,任务是计算前N 个自然数中的三元组 ( a, b, c ),使得a * b + c = N 。
例子:
Input: N = 3
Output: 3
Explanation:
Triplets of the form a * b + c = N are { (1, 1, 2), (1, 2, 1), (2, 1, 1) }
Therefore, the required output is 3.
Input: N = 100
Output: 473
方法:该问题可以基于以下观察来解决:
For every possible pairs (a, b), If a * b < N, then only c exists. Therefore, count the pairs (a, b) whose product is less than N.
请按照以下步骤解决问题:
- 初始化一个变量,比如cntTriplets ,以存储满足给定条件的前N 个自然数的三元组的计数。
- 使用变量i迭代范围[1, N – 1]并检查N % i == 0是否。如果发现为真,则更新cntTriplets += (N / i) – 1 。
- 否则,更新cntTriplet += (N / i)。
- 最后,打印cntTriplets的值。
下面是上述方法的实现。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the count of
// triplets (a, b, c) with a * b + c = N
int findCntTriplet(int N)
{
// Stores count of triplets of 1st
// N natural numbers which are of
// the form a * b + c = N
int cntTriplet = 0;
// Iterate over the range [1, N]
for (int i = 1; i < N; i++) {
// If N is divisible by i
if (N % i != 0) {
// Update cntTriplet
cntTriplet += N / i;
}
else {
// Update cntTriplet
cntTriplet += (N / i) - 1;
}
}
return cntTriplet;
}
// Driver Code
int main()
{
int N = 3;
cout << findCntTriplet(N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to find the count of
// triplets (a, b, c) with a * b + c = N
static int findCntTriplet(int N)
{
// Stores count of triplets of 1st
// N natural numbers which are of
// the form a * b + c = N
int cntTriplet = 0;
// Iterate over the range [1, N]
for (int i = 1; i < N; i++)
{
// If N is divisible by i
if (N % i != 0)
{
// Update cntTriplet
cntTriplet += N / i;
}
else
{
// Update cntTriplet
cntTriplet += (N / i) - 1;
}
}
return cntTriplet;
}
// Driver code
public static void main(String[] args)
{
int N = 3;
System.out.println(findCntTriplet(N));
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python program to implement
# the above approach
# Function to find the count of
# triplets (a, b, c) with a * b + c = N
def findCntTriplet(N):
# Stores count of triplets of 1st
# N natural numbers which are of
# the form a * b + c = N
cntTriplet = 0;
# Iterate over the range [1, N]
for i in range(1, N):
# If N is divisible by i
if (N % i != 0):
# Update cntTriplet
cntTriplet += N // i;
else:
# Update cntTriplet
cntTriplet += (N // i) - 1;
return cntTriplet;
# Driver code
if __name__ == '__main__':
N = 3;
print(findCntTriplet(N));
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to find the count of
// triplets (a, b, c) with a * b + c = N
static int findCntTriplet(int N)
{
// Stores count of triplets of 1st
// N natural numbers which are of
// the form a * b + c = N
int cntTriplet = 0;
// Iterate over the range [1, N]
for (int i = 1; i < N; i++)
{
// If N is divisible by i
if (N % i != 0)
{
// Update cntTriplet
cntTriplet += N / i;
}
else
{
// Update cntTriplet
cntTriplet += (N / i) - 1;
}
}
return cntTriplet;
}
// Driver code
public static void Main(String[] args)
{
int N = 3;
Console.WriteLine(findCntTriplet(N));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)