给定两个整数X和Y ,任务是查找由具有和X和乘积Y的正整数组成的最小序列的长度。如果无法生成这样的序列,请打印-1。
例子:
Input: X = 5, Y = 5
Output: 1
Explanation:
The smallest possible sequence having sum 5 and product 5 is {5}. Therefore, the required answer is length of the sequence(= 1).
Input: X = 9, Y = 8
Output: 2
Explanation:
The smallest possible sequence is {1, 8} with sum X (= 1 + 8 = 9) and product Y(= 1 * 8 = 8).
方法:想法是实施二进制搜索来解决给定问题,该问题在[1,floor(x / e)]范围内的所需大小上,其中e是欧拉常数。
- 分别将低和高两个变量初始化为1和floor(x / e) 。
- 如果X等于Y ,则序列的最大大小始终可以为1 。因此,打印它。
- 否则,迭代直到高和低之间的差超过1并每次计算中间。
- 根据每一步的边界条件重置高和低的值,并在最后打印高的最终值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define ll long long
#define pb push_back
// Function for checking valid or not
double temp(int n, int x)
{
return pow(x * 1.0 / n, n);
}
// Function for checking boundary
// of binary search
bool check(int n, int y, int x)
{
double v = temp(n, x);
return (v >= y);
}
// Function to calculate the
// minimum sequence size
// using binary search
void find(int x, int y)
{
// Initialize high and low
int high = (int)floor(x / exp(1.0));
int low = 1;
// Base case
if (x == y)
cout << 1 << endl;
// Print -1 if a sequence
// cannot be generated
else if (!check(high, y, x))
cout << -1 << endl;
// Otherwise
else {
// Iterate until difference
// between high and low exceeds 1
while (high - low > 1) {
// Calculate mid
int mid
= (high + low) / 2;
// Reset values of high
// and low accordingly
if (check(mid, y, x))
high = mid;
else
low = mid;
}
// Print the answer
cout << high << endl;
}
}
// Driver Code
int main()
{
int x = 9, y = 8;
// Function call
find(x, y);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function for checking valid or not
static double temp(int n, int x)
{
return Math.pow(x * 1.0 / n, n);
}
// Function for checking boundary
// of binary search
static boolean check(int n, int y, int x)
{
double v = temp(n, x);
return (v >= y);
}
// Function to calculate the
// minimum sequence size
// using binary search
static void find(int x, int y)
{
// Initialize high and low
int high = (int)Math.floor(x /
Math.exp(1.0));
int low = 1;
// Base case
if (x == y)
System.out.print(1 + "\n");
// Print -1 if a sequence
// cannot be generated
else if (!check(high, y, x))
System.out.print(-1 + "\n");
// Otherwise
else
{
// Iterate until difference
// between high and low exceeds 1
while (high - low > 1)
{
// Calculate mid
int mid = (high + low) / 2;
// Reset values of high
// and low accordingly
if (check(mid, y, x))
high = mid;
else
low = mid;
}
// Print the answer
System.out.print(high + "\n");
}
}
// Driver Code
public static void main(String[] args)
{
int x = 9, y = 8;
// Function call
find(x, y);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
from math import floor, exp
# Function for checking valid or not
def temp(n, x):
return pow(x * 1 / n, n)
# Function for checking boundary
# of binary search
def check(n, y, x):
v = temp(n, x)
return (v >= y)
# Function to calculate the
# minimum sequence size
# using binary search
def find(x, y):
# Initialize high and low
high = floor(x / exp(1.0))
low = 1
# Base case
if (x == y):
print(1)
# Print -1 if a sequence
# cannot be generated
elif (not check(high, y, x)):
print(-1)
# Otherwise
else:
# Iterate until difference
# between high and low exceeds 1
while (high - low > 1):
# Calculate mid
mid = (high + low) // 2
# Reset values of high
# and low accordingly
if (check(mid, y, x)):
high = mid
else:
low = mid
# Print the answer
print(high)
# Driver Code
if __name__ == '__main__':
x = 9
y = 8
# Function call
find(x, y)
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
class GFG{
// Function for checking
// valid or not
static double temp(int n,
int x)
{
return Math.Pow(x * 1.0 / n, n);
}
// Function for checking
// boundary of binary search
static bool check(int n,
int y, int x)
{
double v = temp(n, x);
return (v >= y);
}
// Function to calculate the
// minimum sequence size
// using binary search
static void find(int x, int y)
{
// Initialize high and low
int high = (int)Math.Floor(x /
Math.Exp(1.0));
int low = 1;
// Base case
if (x == y)
Console.Write(1 + "\n");
// Print -1 if a sequence
// cannot be generated
else if (!check(high, y, x))
Console.Write(-1 + "\n");
// Otherwise
else
{
// Iterate until difference
// between high and low exceeds 1
while (high - low > 1)
{
// Calculate mid
int mid = (high + low) / 2;
// Reset values of high
// and low accordingly
if (check(mid, y, x))
high = mid;
else
low = mid;
}
// Print the answer
Console.Write(high + "\n");
}
}
// Driver Code
public static void Main(String[] args)
{
int x = 9, y = 8;
// Function call
find(x, y);
}
}
// This code is contributed by Rajput-Ji
输出
2
时间复杂度: O(log 2 (X / e))
辅助空间: O(1)