给定三个整数A , B和C。任务是计算三元组(a,b,c)的数量,以使a * c> b 2 ,其中0 , 0 和0
例子:
Input: A = 3, B = 2, C = 2
Output: 6
Following triples are counted :
(1, 1, 2), (2, 1, 1), (2, 1, 2), (3, 1, 1), (3, 1, 2) and (3, 2, 2).
Input: A = 3, B = 3, C = 3
Output: 11
天真的方法:
蛮力方法是考虑所有可能的三元组(a,b,c)并计算满足约束a * c> b 2的那些三元组。
下面是给定方法的实现。
C++
// C++ implementation
#include
using namespace std;
// function to return the count
// of the valid triplets
long long countTriplets(int A, int B, int C)
{
long long ans = 0;
for (int i = 1; i <= A; i++) {
for (int j = 1; j <= B; j++) {
for (int k = 1; k <= C; k++) {
if (i * k > j * j)
ans++;
}
}
}
return ans;
}
// Driver Code
int main()
{
int A, B, C;
A = 3, B = 2, C = 2;
// function calling
cout << countTriplets(A, B, C);
}
Java
// Java implementation of above approach
import java.util.*;
class GFG
{
// function to return the count
// of the valid triplets
static long countTriplets(int A, int B, int C)
{
long ans = 0;
for (int i = 1; i <= A; i++)
{
for (int j = 1; j <= B; j++)
{
for (int k = 1; k <= C; k++)
{
if (i * k > j * j)
ans++;
}
}
}
return ans;
}
// Driver Code
public static void main (String[] args)
{
int A = 3, B = 2, C = 2;
// function calling
System.out.println(countTriplets(A, B, C));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation for above approach
# function to return the count
# of the valid triplets
def countTriplets(A, B, C):
ans = 0
for i in range(1, A + 1):
for j in range(1, B + 1):
for k in range(1, C + 1):
if (i * k > j * j):
ans += 1
return ans
# Driver Code
A = 3
B = 2
C = 2
# function calling
print(countTriplets(A, B, C))
# This code is contributed by Mohit Kumar
C#
// C# implementation of above approach
using System;
class GFG
{
// function to return the count
// of the valid triplets
static long countTriplets(int A,
int B, int C)
{
long ans = 0;
for (int i = 1; i <= A; i++)
{
for (int j = 1; j <= B; j++)
{
for (int k = 1; k <= C; k++)
{
if (i * k > j * j)
ans++;
}
}
}
return ans;
}
// Driver Code
public static void Main (String[] args)
{
int A = 3, B = 2, C = 2;
// function calling
Console.WriteLine(countTriplets(A, B, C));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ implementation
#include
using namespace std;
// Counts the number of triplets
// for a given value of b
long long getCount(int A, int B2,
int C)
{
long long count = 0;
// Count all triples in which a = i
for (int i = 1; i <= A; i++) {
// Smallest value j
// such that i*j > B2
long long j = (B2 / i) + 1;
// Count all (i, B2, x)
// such that x >= j
if (C >= j)
count = (count + C - j + 1);
// count all (x, B2, y) such
// that x >= j this counts
// all such triples in
// which a >= j
if (A >= j && C >= i)
count = (count
+ (C - i + 1)
* (A - j + 1));
// As all triples with a >= j
// have been counted reduce
// A to j - 1.
if (A >= j)
A = j - 1;
}
return count;
}
// Counts the number of triples that
// satisfy the given constraints
long long countTriplets(int A, int B,
int C)
{
long long ans = 0;
for (int i = 1; i <= B; i++) {
// GetCount of triples in which b = i
ans = (ans
+ getCount(A, i * i, C));
}
return ans;
}
// Driver Code
int main()
{
int A, B, C;
A = 3, B = 2, C = 2;
// Function calling
cout << countTriplets(A, B, C);
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Counts the number of triplets
// for a given value of b
static long getCount(int A, int B2, int C)
{
long count = 0;
// Count all triples in which a = i
for (int i = 1; i <= A; i++)
{
// Smallest value j
// such that i*j > B2
long j = (B2 / i) + 1;
// Count all (i, B2, x)
// such that x >= j
if (C >= j)
count = (count + C - j + 1);
// count all (x, B2, y) such
// that x >= j this counts
// all such triples in
// which a >= j
if (A >= j && C >= i)
count = (count + (C - i + 1) *
(A - j + 1));
// As all triples with a >= j
// have been counted reduce
// A to j - 1.
if (A >= j)
A = (int) (j - 1);
}
return count;
}
// Counts the number of triples that
// satisfy the given constraints
static long countTriplets(int A, int B, int C)
{
long ans = 0;
for (int i = 1; i <= B; i++)
{
// GetCount of triples in which b = i
ans = (ans + getCount(A, i * i, C));
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int A, B, C;
A = 3; B = 2; C = 2;
// Function calling
System.out.println(countTriplets(A, B, C));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation
# Counts the number of triplets
# for a given value of b
def getCount(A, B2, C):
count = 0
# Count all triples in which a = i
i=1
while(i B2
j = (B2 // i) + 1
# Count all (i, B2, x)
# such that x >= j
if (C >= j):
count = count + C - j + 1
# count all (x, B2, y) such
# that x >= j this counts
# all such triples in
# which a >= j
if (A>= j and C >= i):
count = count+ (C - i + 1) * (A - j + 1)
# As all triples with a >= j
# have been counted reduce
# A to j - 1.
if (A >= j):
A = j - 1
i+=1
return count
# Counts the number of triples that
# satisfy the given constraints
def countTriplets(A, B, C):
ans = 0
for i in range(1,B+1):
# GetCount of triples in which b = i
ans = (ans+ getCount(A, i * i, C))
return ans
# Driver Code
A = 3
B = 2
C = 2
# Function calling
print(countTriplets(A, B, C))
# This code is contributed by shubhamsingh10
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Counts the number of triplets
// for a given value of b
static long getCount(int A, int B2, int C)
{
long count = 0;
// Count all triples in which a = i
for (int i = 1; i <= A; i++)
{
// Smallest value j
// such that i*j > B2
long j = (B2 / i) + 1;
// Count all (i, B2, x)
// such that x >= j
if (C >= j)
count = (count + C - j + 1);
// count all (x, B2, y) such
// that x >= j this counts
// all such triples in
// which a >= j
if (A >= j && C >= i)
count = (count + (C - i + 1) *
(A - j + 1));
// As all triples with a >= j
// have been counted reduce
// A to j - 1.
if (A >= j)
A = (int) (j - 1);
}
return count;
}
// Counts the number of triples that
// satisfy the given constraints
static long countTriplets(int A, int B, int C)
{
long ans = 0;
for (int i = 1; i <= B; i++)
{
// GetCount of triples in which b = i
ans = (ans + getCount(A, i * i, C));
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int A, B, C;
A = 3; B = 2; C = 2;
// Function calling
Console.WriteLine(countTriplets(A, B, C));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
6
时间复杂度: 。
高效的方法:
让我们计算从1到B的所有k的给定值b = k的所有三胞胎。
- 对于给定的b = k,我们需要找到满足i * j> k 2的所有a = i和c = j
- 对于a = i ,找到满足条件的最小c = j 。
由于c = j满足此条件,因此c = j + 1,c = j + 2,… ,依此类推,也将满足该条件。
因此,我们可以轻松地计算a = i和b = k的所有三元组。 - 同样,如果对于某些a = i , c = j是满足给定条件的最小值,则可以观察到a = j且所有c> = i也都满足该条件。
该条件还通过a = j +1和c> = i来满足,即所有值a> = j和c> = i也都满足该条件。 - 上面的观察有助于我们轻松地计算b = k和a> = j的所有三元组。
- 现在我们需要计算b = k且i 所有三元组。
- 因此,对于给定的b = k值,我们只需要向上求a = k的平方根。
下面是上述方法的实现:
C++
// C++ implementation
#include
using namespace std;
// Counts the number of triplets
// for a given value of b
long long getCount(int A, int B2,
int C)
{
long long count = 0;
// Count all triples in which a = i
for (int i = 1; i <= A; i++) {
// Smallest value j
// such that i*j > B2
long long j = (B2 / i) + 1;
// Count all (i, B2, x)
// such that x >= j
if (C >= j)
count = (count + C - j + 1);
// count all (x, B2, y) such
// that x >= j this counts
// all such triples in
// which a >= j
if (A >= j && C >= i)
count = (count
+ (C - i + 1)
* (A - j + 1));
// As all triples with a >= j
// have been counted reduce
// A to j - 1.
if (A >= j)
A = j - 1;
}
return count;
}
// Counts the number of triples that
// satisfy the given constraints
long long countTriplets(int A, int B,
int C)
{
long long ans = 0;
for (int i = 1; i <= B; i++) {
// GetCount of triples in which b = i
ans = (ans
+ getCount(A, i * i, C));
}
return ans;
}
// Driver Code
int main()
{
int A, B, C;
A = 3, B = 2, C = 2;
// Function calling
cout << countTriplets(A, B, C);
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Counts the number of triplets
// for a given value of b
static long getCount(int A, int B2, int C)
{
long count = 0;
// Count all triples in which a = i
for (int i = 1; i <= A; i++)
{
// Smallest value j
// such that i*j > B2
long j = (B2 / i) + 1;
// Count all (i, B2, x)
// such that x >= j
if (C >= j)
count = (count + C - j + 1);
// count all (x, B2, y) such
// that x >= j this counts
// all such triples in
// which a >= j
if (A >= j && C >= i)
count = (count + (C - i + 1) *
(A - j + 1));
// As all triples with a >= j
// have been counted reduce
// A to j - 1.
if (A >= j)
A = (int) (j - 1);
}
return count;
}
// Counts the number of triples that
// satisfy the given constraints
static long countTriplets(int A, int B, int C)
{
long ans = 0;
for (int i = 1; i <= B; i++)
{
// GetCount of triples in which b = i
ans = (ans + getCount(A, i * i, C));
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
int A, B, C;
A = 3; B = 2; C = 2;
// Function calling
System.out.println(countTriplets(A, B, C));
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation
# Counts the number of triplets
# for a given value of b
def getCount(A, B2, C):
count = 0
# Count all triples in which a = i
i=1
while(i B2
j = (B2 // i) + 1
# Count all (i, B2, x)
# such that x >= j
if (C >= j):
count = count + C - j + 1
# count all (x, B2, y) such
# that x >= j this counts
# all such triples in
# which a >= j
if (A>= j and C >= i):
count = count+ (C - i + 1) * (A - j + 1)
# As all triples with a >= j
# have been counted reduce
# A to j - 1.
if (A >= j):
A = j - 1
i+=1
return count
# Counts the number of triples that
# satisfy the given constraints
def countTriplets(A, B, C):
ans = 0
for i in range(1,B+1):
# GetCount of triples in which b = i
ans = (ans+ getCount(A, i * i, C))
return ans
# Driver Code
A = 3
B = 2
C = 2
# Function calling
print(countTriplets(A, B, C))
# This code is contributed by shubhamsingh10
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Counts the number of triplets
// for a given value of b
static long getCount(int A, int B2, int C)
{
long count = 0;
// Count all triples in which a = i
for (int i = 1; i <= A; i++)
{
// Smallest value j
// such that i*j > B2
long j = (B2 / i) + 1;
// Count all (i, B2, x)
// such that x >= j
if (C >= j)
count = (count + C - j + 1);
// count all (x, B2, y) such
// that x >= j this counts
// all such triples in
// which a >= j
if (A >= j && C >= i)
count = (count + (C - i + 1) *
(A - j + 1));
// As all triples with a >= j
// have been counted reduce
// A to j - 1.
if (A >= j)
A = (int) (j - 1);
}
return count;
}
// Counts the number of triples that
// satisfy the given constraints
static long countTriplets(int A, int B, int C)
{
long ans = 0;
for (int i = 1; i <= B; i++)
{
// GetCount of triples in which b = i
ans = (ans + getCount(A, i * i, C));
}
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int A, B, C;
A = 3; B = 2; C = 2;
// Function calling
Console.WriteLine(countTriplets(A, B, C));
}
}
// This code is contributed by Princi Singh
Java脚本
输出:
6
时间复杂度:
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。