📌  相关文章
📜  求出最小值x使得(x%k)*(x / k)== n

📅  最后修改于: 2021-04-22 00:34:47             🧑  作者: Mango

给定两个正整数n和k。找到最小正整数x,使(x%k)*(x / k)== n,其中%是模运算符,/是整数除法运算符。
例子:

Input : n = 4, k = 6
Output :10
Explanation : (10 % 6) * (10 / 6) = (4) * (1) = 4 which is equal to n

Input : n = 5, k = 5
Output : 26

天真的解决方案:一种简单的方法是运行while循环,直到找到满足给定方程的解决方案为止,但这会非常慢。
高效解决方案:此处的关键思想是注意到(x%k)的值在[1,k – 1]范围内。 (不包括0,因为当n为零时,我们无法将其除以(x%k))。现在,我们需要找到除以n的范围内的最大可能数,因此给定的等式变为x =(n * k)/(x%k)+(x%k)。
注意: (x%k)被添加到答案中,因为对于当前模量(x%k)而言,一定不能矛盾,一方面x是除以k的余数是(x%k)另一方面,x是(n * k)/(x%k),当我们将该值除以k时,其余数仅为零。
下面是上述方法的实现。

C++
// CPP Program to find the minimum
// positive X such that the given
// equation holds true
#include 
using namespace std;
 
// This function gives the required
// answer
int minimumX(int n, int k)
{
    int ans = INT_MAX;
 
    // Iterate over all possible
    // remainders
    for (int rem = k - 1; rem > 0; rem--) {
 
        // it must divide n
        if (n % rem == 0)
            ans = min(ans, rem + (n / rem) * k);
    }
    return ans;
}
 
// Driver Code to test above function
int main()
{
    int n = 4, k = 6;
    cout << minimumX(n, k) << endl;
 
    n = 5, k = 5;
    cout << minimumX(n, k) << endl;
    return 0;
}


Java
// Java Program to find the minimum
// positive X such that the given
// equation holds true
class Solution
{
// This function gives the required
// answer
static int minimumX(int n, int k)
{
    int ans =Integer.MAX_VALUE;
  
    // Iterate over all possible
    // remainders
    for (int rem = k - 1; rem > 0; rem--) {
  
        // it must divide n
        if (n % rem == 0)
            ans = Math.min(ans, rem + (n / rem) * k);
    }
    return ans;
}
  
// Driver Code to test above function
public static void main(String args[])
{
    int n = 4, k = 6;
    System.out.println( minimumX(n, k));
  
    n = 5; k = 5;
    System.out.println(minimumX(n, k));
     
}
}
//contributed by Arnab Kundu


Python3
# Python 3 program to find the minimum positive
# x such that the given equation holds true
 
# This function gives the required answer
def minimumX(n, k):
     
     
    ans = 10 ** 18
     
    # Iterate over all possible remainders
    for i in range(k - 1, 0, -1):
        if n % i == 0:
            ans = min(ans, i + (n / i) * k)
    return ans
 
# Driver Code
n, k = 4, 6
 
print(minimumX(n, k))
 
n, k = 5, 5
 
print(minimumX(n, k))
 
# This code is contributed
# by Mohit Kumar


C#
// C# Program to find the minimum
// positive X such that the given
// equation holds true
 
using System;
 
public class GFG{
    // This function gives the required
// answer
static int minimumX(int n, int k)
{
    int ans =int.MaxValue;
 
    // Iterate over all possible
    // remainders
    for (int rem = k - 1; rem > 0; rem--) {
 
        // it must divide n
        if (n % rem == 0)
            ans = Math.Min(ans, rem + (n / rem) * k);
    }
    return ans;
}
 
// Driver Code to test above function
    static public void Main (){
        int n = 4, k = 6;
        Console.WriteLine( minimumX(n, k));
 
        n = 5; k = 5;
        Console.WriteLine(minimumX(n, k));
     
}
}
//This code is contributed by Sachin.


PHP
 0; $rem--)
    {
 
        // it must divide n
        if ($n % $rem == 0)
            $ans = min($ans, $rem +
                      ($n / $rem) * $k);
    }
    return $ans;
}
 
// Driver Code
$n = 4 ;
$k = 6 ;
 
echo minimumX($n, $k), "\n" ;
 
$n = 5 ;
$k = 5 ;
 
echo minimumX($n, $k) ;
 
// This code is contributed by Ryuga
?>


Javascript


输出:
10
26

时间复杂度: O(k),其中k是给定的正整数。