📜  不能被给定质数P除以N的最大数除数的总和

📅  最后修改于: 2021-04-23 07:05:54             🧑  作者: Mango

给定一个数N和一个质数P ,任务是找到[1,N]范围内每个数的最大除数之和,该数不能被P整除。
例子:

幼稚的方法:幼稚的想法是找到[1,N]范围内每个数字的除数,并找到不能被P和那些数字整除的最大除数。打印所有最大除数的总和。
时间复杂度: O(N 3/2 )
辅助空间: O(1)
有效的方法:我们的想法是,观察到数N的最大除数不能整除以P将是N本身,如果N不是整除被P。否则,所需的除数将与N / P相同。步骤如下:

  1. 如果N不能被P整除,则最大除数将为N ,将其加到最终总和上。
  2. 如果N可被P整除,则所需的除数将与N / P相同
  3. 因此,求出所有不能被P整除的数的和,然后分别添加那些被P整除的数的除数。
  4. 总和为N *(N +1)/ 2 。通过递归调用该函数以找到N / P的总和,减去那些可被P整除的总和,并将其对应的值相加。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the sum of largest
// divisors of numbers in range 1 to N
// not divisible by prime number P
int func(int N, int P)
{
    // Total sum upto N
    int sumUptoN = (N * (N + 1) / 2);
    int sumOfMultiplesOfP;
 
    // If no multiple of P exist up to N
    if (N < P) {
        return sumUptoN;
    }
 
    // If only P itself is in the range
    // from 1 to N
    else if ((N / P) == 1) {
        return sumUptoN - P + 1;
    }
 
    // Sum of those that are divisible by P
    sumOfMultiplesOfP
        = ((N / P) * (2 * P + (N / P - 1) * P)) / 2;
 
    // Recursively function call to
    // find the sum for N/P
    return (sumUptoN
            + func(N / P, P)
            - sumOfMultiplesOfP);
}
 
// Driver Code
int main()
{
    // Given N and P
    int N = 10, P = 5;
 
    // Function Call
    cout << func(N, P) << "\n";
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the sum of largest
// divisors of numbers in range 1 to N
// not divisible by prime number P
static int func(int N, int P)
{
     
    // Total sum upto N
    int sumUptoN = (N * (N + 1) / 2);
    int sumOfMultiplesOfP;
 
    // If no multiple of P exist up to N
    if (N < P)
    {
        return sumUptoN;
    }
 
    // If only P itself is in the range
    // from 1 to N
    else if ((N / P) == 1)
    {
        return sumUptoN - P + 1;
    }
 
    // Sum of those that are divisible by P
    sumOfMultiplesOfP = ((N / P) * (2 * P +
                         (N / P - 1) * P)) / 2;
 
    // Recursively function call to
    // find the sum for N/P
    return (sumUptoN + func(N / P, P) -
            sumOfMultiplesOfP);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N and P
    int N = 10, P = 5;
 
    // Function call
    System.out.println(func(N, P));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the
# above approach
 
# Function to find the sum
# of largest divisors of
# numbers in range 1 to N
# not divisible by prime number P
def func(N, P):
   
    # Total sum upto N
    sumUptoN = (N * (N + 1) / 2);
    sumOfMultiplesOfP = 0;
 
    # If no multiple of P exist
    # up to N
    if (N < P):
        return sumUptoN;
 
    # If only P itself is
    # in the range from 1
    # to N
    elif ((N / P) == 1):
        return sumUptoN - P + 1;
 
    # Sum of those that are
    # divisible by P
    sumOfMultiplesOfP = (((N / P) *
                         (2 * P +
                         (N / P - 1) *
                          P)) / 2);
 
    # Recursively function call to
    # find the sum for N/P
    return (sumUptoN +
            func(N / P, P) -
            sumOfMultiplesOfP);
 
# Driver Code
if __name__ == '__main__':
   
    # Given N and P
    N = 10;
    P = 5;
 
    # Function call
    print(func(N, P));
 
# This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the sum of largest
// divisors of numbers in range 1 to N
// not divisible by prime number P
static int func(int N, int P)
{
     
    // Total sum upto N
    int sumUptoN = (N * (N + 1) / 2);
    int sumOfMultiplesOfP;
 
    // If no multiple of P exist up to N
    if (N < P)
    {
        return sumUptoN;
    }
 
    // If only P itself is in the range
    // from 1 to N
    else if ((N / P) == 1)
    {
        return sumUptoN - P + 1;
    }
 
    // Sum of those that are divisible by P
    sumOfMultiplesOfP = ((N / P) * (2 * P +
                         (N / P - 1) * P)) / 2;
 
    // Recursively function call to
    // find the sum for N/P
    return (sumUptoN + func(N / P, P) -
            sumOfMultiplesOfP);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given N and P
    int N = 10, P = 5;
 
    // Function call
    Console.WriteLine(func(N, P));
}
}
 
// This code is contributed by Amit Katiyar


输出:
43




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