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

📅  最后修改于: 2021-10-27 07:44:38             🧑  作者: Mango

给定两个正整数PQ ,任务是求不能被Q整除的P 的最大除数。

例子:

方法:最简单的方法是找到 P 的所有因数并获得这些因数中的最大者,该因数不能被Q整除。

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

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

  • 将 Q 的质因数的频率计数存储在 HashMap divisors 中
  • 初始化一个变量,比如ans ,以存储最大的数X ,这样X 可以整除P但不能被Q整除。
  • 迭代Q 的所有除数并执行以下步骤:
    • 将当前素数除数的频率计数存储在一个变量中,比如frequency
    • 将除P时的当前素数除数的频率计数存储在一个变量中,比如cur ,并将num初始化为当前素数除数 * (frequency – cur + 1)
    • 如果cur的值小于frequency ,则将变量ans更新为P并跳出循环。
    • 否则,将Pnum相除并将结果存储在一个变量中,例如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


Javascript


输出
10

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