给定四个整数N、X、P和Q ,任务是通过以下两个操作找到使N为1的最小成本:
- 从 N 中减去1 ,成本为P 。
- 将N 除以 X (如果 N 可被 X 整除),成本为Q 。
例子:
Input: N = 5, X = 2, P = 2, Q = 3
Output: 7
Explanation:
Operation 1: 5 – 1 -> cost = 2
Operation 2: 4 / 2 -> cost = 3
Operation 3: 2 – 1 -> cost = 2
Minimum total cost is 2 + 3 + 2 = 7.
Input: N = 6, X = 6, P = 2, Q = 1
Output: 1
Explanation:
Operation 1: 6 / 6 with cost = 1, hence that would be the minimum.
方法:这个问题可以使用贪心方法来解决。以下是观察结果:
- 如果 x = 1,则答案为(N – 1) * P。
- 否则,如果N 小于 X ,则只能将数字减少 1,因此答案是(N – 1) * P 。
- 否则,进行第一个操作,直到N 不能被 X 整除。
- 通过比较第一个和第二个操作来最优地选择第二个操作,即,如果我们可以执行第一个操作使得将N减少到 1的成本最小,否则选择第二个操作。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost
// to reduce the integer N to 1
// by the given operations
int min_cost(int n, int x, int p, int q)
{
// Check if x is 1
if (x == 1) {
// Print the answer
cout << (n - 1) * p << endl;
return 0;
}
// Prestore the answer
int ans = (n - 1) * p;
int pre = 0;
// Iterate till n exists
while (n > 1) {
// Divide N by x
int tmp = n / x;
if (tmp < 0)
break;
pre += (n - tmp * x) * p;
// Reduce n by x
n /= x;
// Add the cost
pre += q;
// Update the answer
ans = min(ans,
pre + (n - 1) * p);
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
// Initialize the variables
int n = 5, x = 2, p = 2, q = 3;
// Function call
cout << min_cost(n, x, p, q);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the minimum cost
// to reduce the integer N to 1
// by the given operations
static int min_cost(int n, int x,
int p, int q)
{
// Check if x is 1
if (x == 1)
{
// Print the answer
System.out.println((n - 1) * p);
return 0;
}
// Prestore the answer
int ans = (n - 1) * p;
int pre = 0;
// Iterate till n exists
while (n > 1)
{
// Divide N by x
int tmp = n / x;
if (tmp < 0)
break;
pre += (n - tmp * x) * p;
// Reduce n by x
n /= x;
// Add the cost
pre += q;
// Update the answer
ans = Math.min(ans,
pre + (n - 1) * p);
}
// Return the answer
return ans;
}
// Driver code
public static void main(String[] args)
{
// Initialize the variables
int n = 5, x = 2, p = 2, q = 3;
// Function call
System.out.println(min_cost(n, x, p, q));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to find the minimum cost
# to reduce the integer N to 1
# by the given operations
def min_cost(n, x, p, q):
# Check if x is 1
if (x == 1):
# Print the answer
print((n - 1) * p)
return 0
# Prestore the answer
ans = (n - 1) * p
pre = 0
# Iterate till n exists
while (n > 1):
# Divide N by x
tmp = n // x
if (tmp < 0):
break
pre += (n - tmp * x) * p
# Reduce n by x
n //= x
# Add the cost
pre += q
# Update the answer
ans = min(ans, pre + (n - 1) * p)
# Return the answer
return ans
# Driver Code
if __name__ == '__main__':
# Initialize the variables
n = 5; x = 2;
p = 2; q = 3;
# Function call
print(min_cost(n, x, p, q))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum cost
// to reduce the integer N to 1
// by the given operations
static int min_cost(int n, int x,
int p, int q)
{
// Check if x is 1
if (x == 1)
{
// Print the answer
Console.WriteLine((n - 1) * p);
return 0;
}
// Prestore the answer
int ans = (n - 1) * p;
int pre = 0;
// Iterate till n exists
while (n > 1)
{
// Divide N by x
int tmp = n / x;
if (tmp < 0)
break;
pre += (n - tmp * x) * p;
// Reduce n by x
n /= x;
// Add the cost
pre += q;
// Update the answer
ans = Math.Min(ans,
pre + (n - 1) * p);
}
// Return the answer
return ans;
}
// Driver code
public static void Main(String[] args)
{
// Initialize the variables
int n = 5, x = 2, p = 2, q = 3;
// Function call
Console.WriteLine(min_cost(n, x, p, q));
}
}
// This code is contributed by princiraj1992
输出:
7
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。