📌  相关文章
📜  一个数字的最大除数不能被另一个给定数字整除

📅  最后修改于: 2021-04-26 10:04:26             🧑  作者: Mango

给定两个正整数PQ ,任务是将P的最大除数除以Q。

例子:

方法:最简单的方法是找到P的所有除数,并获得最大的除数,而Q不能将其整除。

时间复杂度: O(√P)
辅助空间: O(1)

替代方法:请按照以下步骤解决问题:

  • 将H的质数因子的频率计数存储在HashMap除数中
  • 初始化一个变量,例如ans ,以存储最大数X ,以使X除以P,但不能被Q整除。
  • 遍历Q的所有除数并执行以下步骤:
    • 将当前质数除数的频率计数存储在一个变量中,例如frequency
    • P除以当前主除数的频率计数,然后将其存储在变量中,例如cur ,并将num初始化为当前主除数*(频率– cur + 1)
    • 如果cur的值小于frequency ,则将变量ans更新为P并退出循环。
    • 否则,将P除以num并将结果存储在变量中,例如tempAns
    • 完成上述步骤后,将ans的值更新为anstempAns的最大值。
  • 完成上述步骤后,将ans的值打印为最大可能的结果。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the largest number
// X such that it divides P but is not
// divisible by Q
void findTheGreatestX(int P, int Q)
{
    // Stores the frequency count of
    // of all Prime Factors
    map divisiors;
 
    for (int i = 2; i * i <= Q; i++) {
 
        while (Q % i == 0 and Q > 1) {
 
            Q /= i;
 
            // Increment the frequency
            // of the current prime factor
            divisiors[i]++;
        }
    }
 
    // If Q is a prime factor
    if (Q > 1)
        divisiors[Q]++;
 
    // Stores the desired result
    int ans = 0;
 
    // Iterate through all divisors of Q
    for (auto i : divisiors) {
 
        int frequency = i.second;
        int temp = P;
 
        // Stores the frequency count
        // of current prime divisor on
        // dividing P
        int cur = 0;
 
        while (temp % i.first == 0) {
            temp /= i.first;
 
            // Count the frequency of the
            // current prime factor
            cur++;
        }
 
        // If cur is less than frequency
        // then P is the final result
        if (cur < frequency) {
            ans = P;
            break;
        }
 
        temp = P;
 
        // Iterate to get temporary answer
        for (int j = cur; j >= frequency; j--) {
            temp /= i.first;
        }
 
        // Update current answer
        ans = max(temp, ans);
    }
 
    // Print the desired result
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given P and Q
    int P = 10, Q = 4;
 
    // Function Call
    findTheGreatestX(P, Q);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the largest number
// X such that it divides P but is not
// divisible by Q
static void findTheGreatestX(int P, int Q)
{
     
    // Stores the frequency count of
    // of all Prime Factors
    HashMap divisiors = new HashMap<>();
 
    for(int i = 2; i * i <= Q; i++)
    {
        while (Q % i == 0 &&  Q > 1)
        {
            Q /= i;
             
            // Increment the frequency
            // of the current prime factor
            if (divisiors.containsKey(i))
            {
                divisiors.put(i, divisiors.get(i) + 1);
            }
            else
            {
                divisiors.put(i, 1);
            }
        }
    }
 
    // If Q is a prime factor
    if (Q > 1)
        if (divisiors.containsKey(Q))
        {
            divisiors.put(Q, divisiors.get(Q) + 1);
        }
        else
        {
            divisiors.put(Q, 1);
        }
 
    // Stores the desired result
    int ans = 0;
 
    // Iterate through all divisors of Q
    for(Map.Entry i : divisiors.entrySet())
    {
        int frequency = i.getValue();
        int temp = P;
         
        // Stores the frequency count
        // of current prime divisor on
        // dividing P
        int cur = 0;
 
        while (temp % i.getKey() == 0)
        {
            temp /= i.getKey();
             
            // Count the frequency of the
            // current prime factor
            cur++;
        }
 
        // If cur is less than frequency
        // then P is the final result
        if (cur < frequency)
        {
            ans = P;
            break;
        }
 
        temp = P;
 
        // Iterate to get temporary answer
        for(int j = cur; j >= frequency; j--)
        {
            temp /= i.getKey();
        }
 
        // Update current answer
        ans = Math.max(temp, ans);
    }
 
    // Print the desired result
    System.out.print(ans);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given P and Q
    int P = 10, Q = 4;
 
    // Function Call
    findTheGreatestX(P, Q);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement
# the above approach
from collections import defaultdict
 
# Function to find the largest number
# X such that it divides P but is not
# divisible by Q
def findTheGreatestX(P, Q):
 
    # Stores the frequency count of
    # of all Prime Factors
    divisiors = defaultdict(int)
 
    i = 2
    while i * i <= Q:
 
        while (Q % i == 0 and Q > 1):
 
            Q //= i
 
            # Increment the frequency
            # of the current prime factor
            divisiors[i] += 1
        i += 1
 
    # If Q is a prime factor
    if (Q > 1):
        divisiors[Q] += 1
 
    # Stores the desired result
    ans = 0
 
    # Iterate through all divisors of Q
    for i in divisiors:
 
        frequency = divisiors[i]
        temp = P
 
        # Stores the frequency count
        # of current prime divisor on
        # dividing P
        cur = 0
 
        while (temp % i == 0):
            temp //= i
 
            # Count the frequency of the
            # current prime factor
            cur += 1
 
        # If cur is less than frequency
        # then P is the final result
        if (cur < frequency):
            ans = P
            break
 
        temp = P
 
        # Iterate to get temporary answer
        for j in range(cur, frequency-1, -1):
            temp //= i
 
        # Update current answer
        ans = max(temp, ans)
 
    # Print the desired result
    print(ans)
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given P and Q
    P = 10
    Q = 4
 
    # Function Call
    findTheGreatestX(P, Q)
 
    # This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System.Collections.Generic;
using System;
using System.Linq;
 
class GFG{
 
// Function to find the largest number
// X such that it divides P but is not
// divisible by Q
static void findTheGreatestX(int P, int Q)
{
     
    // Stores the frequency count of
    // of all Prime Factors
    Dictionary divisers = new Dictionary();
                                               
    for(int i = 2; i * i <= Q; i++)
    {
        while (Q % i == 0 && Q > 1)
        {
            Q /= i;
             
            // Increment the frequency
            // of the current prime factor
            if (divisers.ContainsKey(i))
                divisers[i]++;
            else
                divisers[i] = 1;
        }
    }
 
    // If Q is a prime factor
    if (Q > 1)
    {
        if (divisers.ContainsKey(Q))
            divisers[Q]++;
        else
            divisers[Q] = 1;
    }
 
    // Stores the desired result
    int ans = 0;
    var val = divisers.Keys.ToList();
 
    // Iterate through all divisors of Q
    foreach(var key in val)
    {
        int frequency = divisers[key];
        int temp = P;
         
        // Stores the frequency count
        // of current prime divisor on
        // dividing P
        int cur = 0;
 
        while (temp % key == 0)
        {
            temp /= key;
             
            // Count the frequency of the
            // current prime factor
            cur++;
        }
 
        // If cur is less than frequency
        // then P is the final result
        if (cur < frequency)
        {
            ans = P;
            break;
        }
 
        temp = P;
 
        // Iterate to get temporary answer
        for(int j = cur; j >= frequency; j--)
        {
            temp /= key;
        }
 
        // Update current answer
        ans = Math.Max(temp, ans);
    }
 
    // Print the desired result
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given P and Q
    int P = 10, Q = 4;
 
    // Function Call
    findTheGreatestX(P, Q);
}
}
 
// This code is contributed by Stream_Cipher


输出
10

时间复杂度: O(sqrt(Q)+ log(P)* log(Q))
辅助空间: O(Q)