📌  相关文章
📜  最小化成本,以使X和Y等于给定的增量

📅  最后修改于: 2021-04-24 04:21:18             🧑  作者: Mango

给定两个整数XY ,任务是通过多次执行以下操作来使两个整数相等:

  • X增加M倍。成本= M – 1
  • Y增加N倍。成本= N – 1

例子:

天真的方法:请按照以下步骤解决问题:

  1. 对于X的每个值,如果Y小于X ,则增加Y并更新成本。
  2. 如果X = Y ,则打印总成本。
  3. 如果X ,则增加X并更新成本。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum cost
// to make x and y equal
int minimumCost(int x, int y)
{
    int costx = 0, dup_x = x;
 
    while (true) {
 
        int costy = 0, dup_y = y;
 
        // Check if it possible
        // to make x == y
        while (dup_y != dup_x
               && dup_y < dup_x) {
            dup_y += y;
            costy++;
        }
 
        // Iif both are equal
        if (dup_x == dup_y)
            return costx + costy;
 
        // Otherwise
        else {
 
            // Increment cost of x
            // by 1 and dup_x by x
            dup_x += x;
            costx++;
        }
    }
}
 
// Driver Code
int main()
{
    int x = 5, y = 17;
 
    // Returns the required minimum cost
    cout << minimumCost(x, y) << endl;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find minimum cost
// to make x and y equal
static int minimumCost(int x, int y)
{
    int costx = 0, dup_x = x;
    while (true)
    {
        int costy = 0, dup_y = y;
 
        // Check if it possible
        // to make x == y
        while (dup_y != dup_x
               && dup_y < dup_x)
        {
            dup_y += y;
            costy++;
        }
 
        // Iif both are equal
        if (dup_x == dup_y)
            return costx + costy;
 
        // Otherwise
        else {
 
            // Increment cost of x
            // by 1 and dup_x by x
            dup_x += x;
            costx++;
        }
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int x = 5, y = 17;
 
    // Returns the required minimum cost
    System.out.print(minimumCost(x, y) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find minimum cost
# to make x and y equal
def minimumCost(x, y):
     
    costx, dup_x = 0, x
 
    while (True):
        costy, dup_y = 0, y
         
        # Check if it possible
        # to make x == y
        while (dup_y != dup_x and
               dup_y < dup_x):
            dup_y += y
            costy += 1
 
        # If both are equal
        if (dup_x == dup_y):
            return costx + costy
 
        # Otherwise
        else:
             
            # Increment cost of x
            # by 1 and dup_x by x
            dup_x += x
            costx += 1
 
# Driver Code
if __name__ == '__main__':
     
    x, y = 5, 17
 
    # Returns the required minimum cost
    print(minimumCost(x, y))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find minimum cost
  // to make x and y equal
  static int minimumCost(int x, int y)
  {
    int costx = 0, dup_x = x;
    while (true)
    {
      int costy = 0, dup_y = y;
 
      // Check if it possible
      // to make x == y
      while (dup_y != dup_x
             && dup_y < dup_x)
      {
        dup_y += y;
        costy++;
      }
 
      // Iif both are equal
      if (dup_x == dup_y)
        return costx + costy;
 
      // Otherwise
      else {
 
        // Increment cost of x
        // by 1 and dup_x by x
        dup_x += x;
        costx++;
      }
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int x = 5, y = 17;
 
    // Returns the required minimum cost
    Console.Write(minimumCost(x, y) +"\n");
  }
}
 
// This code is contributed by 29AjayKumar


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find gcd of x and y
int gcd(int x, int y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}
 
// Function to find lcm of x and y
int lcm(int x, int y)
{
    return (x * y) / gcd(x, y);
}
 
// Function to find minimum Cost
int minimumCost(int x, int y)
{
    int lcm_ = lcm(x, y);
 
    // Subtracted intial cost of x
    int costx = (lcm_ - x) / x;
 
    // Subtracted intial cost of y
    int costy = (lcm_ - y) / y;
    return costx + costy;
}
 
// Driver Code
int main()
{
    int x = 5, y = 17;
 
    // Returns the minimum cost required
    cout << minimumCost(x, y) << endl;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find gcd of x and y
static int gcd(int x, int y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}
 
// Function to find lcm of x and y
static int lcm(int x, int y)
{
    return (x * y) / gcd(x, y);
}
 
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
    int lcm_ = lcm(x, y);
 
    // Subtracted intial cost of x
    int costx = (lcm_ - x) / x;
 
    // Subtracted intial cost of y
    int costy = (lcm_ - y) / y;
    return costx + costy;
}
 
// Driver Code
public static void main(String[] args)
{
    int x = 5, y = 17;
 
    // Returns the minimum cost required
    System.out.print(minimumCost(x, y) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find gcd of x and y
def gcd(x, y):
     
    if (y == 0):
        return x
         
    return gcd(y, x % y)
 
# Function to find lcm of x and y
def lcm(x, y):
 
    return (x * y) // gcd(x, y)
 
# Function to find minimum Cost
def minimumCost(x, y):
     
    lcm_ = lcm(x, y)
 
    # Subtracted intial cost of x
    costx = (lcm_ - x) // x
 
    # Subtracted intial cost of y
    costy = (lcm_ - y) // y
     
    return costx + costy
 
# Driver Code
if __name__ == "__main__":
     
    x = 5
    y = 17
 
    # Returns the minimum cost required
    print(minimumCost(x, y))
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
class GFG
{
 
// Function to find gcd of x and y
static int gcd(int x, int y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}
 
// Function to find lcm of x and y
static int lcm(int x, int y)
{
    return (x * y) / gcd(x, y);
}
 
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
    int lcm_ = lcm(x, y);
 
    // Subtracted intial cost of x
    int costx = (lcm_ - x) / x;
 
    // Subtracted intial cost of y
    int costy = (lcm_ - y) / y;
    return costx + costy;
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 5, y = 17;
 
    // Returns the minimum cost required
    Console.Write(minimumCost(x, y) +"\n");
}
}
 
// This code is contributed by 29AjayKumar


输出:
20

时间复杂度: O((costx + costy)* Y)
辅助空间: O(1)

高效的方法:这里的想法是使用LCM的概念。制作XY的最低成本将等于其LCM。但这还不够。减去XY的初始值,以避免在计算答案时将它们相加。
请按照以下步骤解决问题:

  1. 查找X和Y的LCM。
  2. 从LCM中减去XY的值。
  3. 现在,将LCM除以XY ,然后求和它们的值之和。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find gcd of x and y
int gcd(int x, int y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}
 
// Function to find lcm of x and y
int lcm(int x, int y)
{
    return (x * y) / gcd(x, y);
}
 
// Function to find minimum Cost
int minimumCost(int x, int y)
{
    int lcm_ = lcm(x, y);
 
    // Subtracted intial cost of x
    int costx = (lcm_ - x) / x;
 
    // Subtracted intial cost of y
    int costy = (lcm_ - y) / y;
    return costx + costy;
}
 
// Driver Code
int main()
{
    int x = 5, y = 17;
 
    // Returns the minimum cost required
    cout << minimumCost(x, y) << endl;
}

Java

// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find gcd of x and y
static int gcd(int x, int y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}
 
// Function to find lcm of x and y
static int lcm(int x, int y)
{
    return (x * y) / gcd(x, y);
}
 
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
    int lcm_ = lcm(x, y);
 
    // Subtracted intial cost of x
    int costx = (lcm_ - x) / x;
 
    // Subtracted intial cost of y
    int costy = (lcm_ - y) / y;
    return costx + costy;
}
 
// Driver Code
public static void main(String[] args)
{
    int x = 5, y = 17;
 
    // Returns the minimum cost required
    System.out.print(minimumCost(x, y) +"\n");
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program for the above approach
 
# Function to find gcd of x and y
def gcd(x, y):
     
    if (y == 0):
        return x
         
    return gcd(y, x % y)
 
# Function to find lcm of x and y
def lcm(x, y):
 
    return (x * y) // gcd(x, y)
 
# Function to find minimum Cost
def minimumCost(x, y):
     
    lcm_ = lcm(x, y)
 
    # Subtracted intial cost of x
    costx = (lcm_ - x) // x
 
    # Subtracted intial cost of y
    costy = (lcm_ - y) // y
     
    return costx + costy
 
# Driver Code
if __name__ == "__main__":
     
    x = 5
    y = 17
 
    # Returns the minimum cost required
    print(minimumCost(x, y))
 
# This code is contributed by chitranayal

C#

// C# program for the above approach
using System;
class GFG
{
 
// Function to find gcd of x and y
static int gcd(int x, int y)
{
    if (y == 0)
        return x;
    return gcd(y, x % y);
}
 
// Function to find lcm of x and y
static int lcm(int x, int y)
{
    return (x * y) / gcd(x, y);
}
 
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
    int lcm_ = lcm(x, y);
 
    // Subtracted intial cost of x
    int costx = (lcm_ - x) / x;
 
    // Subtracted intial cost of y
    int costy = (lcm_ - y) / y;
    return costx + costy;
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 5, y = 17;
 
    // Returns the minimum cost required
    Console.Write(minimumCost(x, y) +"\n");
}
}
 
// This code is contributed by 29AjayKumar
输出:
20

时间复杂度: O(log(min(x,y))
辅助空间: O(1)