乘积最多为 N 的已排序三元组 (a, b, c) 的计数
给定一个整数N ,任务是找到三元组 (a, b, c) 的计数,使得 a <= b <= c 和 a * b * c <= N。
例子:
Input: N = 5
Output: 6
Explanation: The triplets that follow the required conditions are (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5) and (1, 2, 2). Hence the count of value triplets is 6.
Input: N = 20
Output: 40
方法:可以根据以下观察解决给定的问题:
- 由于a <= b <=c ,可以看出a的值必须在[1, N 1/3 ]范围内。
- 同样,对于给定的 a , b的值必须在[a, (N/a) 1/2 ]范围内。
- 同样,当a和b的值固定时, c的值必须在[b, N/(a*b)]范围内。因此c的可能值的数量是[b, N/(a*b)]范围内的整数计数。因此,对于每个有效的 a和b , c的可能值的数量是N/(a*b) – b + 1 。
因此,使用下面的上述观察是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to find the count of valid
// triplets (a, b, c) such that the value
// of a * b * c <= N and a <= b <= c
long long validTriplets(int N)
{
// Stores count of triplets
long long ans = 0;
// Loop to iterate in the
// range [1, N^(1/3)]
for (long long a = 1; a * a * a <= N; a++) {
// Loop to iterate in the
// range [a, (N/a)^(1/2)]
for (long long b = a; a * b * b <= N; b++) {
// Add the count of valid
// values of c for a fixed
// value of a and b
ans += N / a / b - b + 1;
}
}
// Return Answer
return ans;
}
// Driver Code
int main()
{
int N = 5;
cout << validTriplets(N);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG{
// Function to find the count of valid
// triplets (a, b, c) such that the value
// of a * b * c <= N and a <= b <= c
static long validTriplets(int N)
{
// Stores count of triplets
long ans = 0;
// Loop to iterate in the
// range [1, N^(1/3)]
for (long a = 1; a * a * a <= N; a++) {
// Loop to iterate in the
// range [a, (N/a)^(1/2)]
for (long b = a; a * b * b <= N; b++) {
// Add the count of valid
// values of c for a fixed
// value of a and b
ans += N / a / b - b + 1;
}
}
// Return Answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
System.out.print(validTriplets(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of the above approach
# Function to find the count of valid
# triplets (a, b, c) such that the value
# of a * b * c <= N and a <= b <= c
def validTriplets(N):
# Stores count of triplets
ans = 0;
# Loop to iterate in the
# range [1, N^(1/3)]
for a in range(1, int(N ** (1 / 3)) + 1):
# Loop to iterate in the
# range [a, (N/a)^(1/2)]
b = a;
while(a * b * b <= N):
# Add the count of valid
# values of c for a fixed
# value of a and b
ans += N / a / b - b + 1;
b += 1
# Return Answer
return int(ans);
# Driver Code
N = 5;
print(validTriplets(N));
# This code is contributed by gfgking
C#
// C# implementation of the above approach
using System;
class GFG {
// Function to find the count of valid
// triplets (a, b, c) such that the value
// of a * b * c <= N and a <= b <= c
static long validTriplets(int N)
{
// Stores count of triplets
long ans = 0;
// Loop to iterate in the
// range [1, N^(1/3)]
for (long a = 1; a * a * a <= N; a++) {
// Loop to iterate in the
// range [a, (N/a)^(1/2)]
for (long b = a; a * b * b <= N; b++) {
// Add the count of valid
// values of c for a fixed
// value of a and b
ans += N / a / b - b + 1;
}
}
// Return Answer
return ans;
}
// Driver Code
public static void Main()
{
int N = 5;
Console.WriteLine(validTriplets(N));
}
}
// This code is contributed by ukasp.
Javascript
输出
6
时间复杂度: O(N 2/3 )
辅助空间: O(1)