给定一个数N和一个质数P ,任务是找到[1,N]范围内每个数的最大除数之和,该数不能被P整除。
例子:
Input: N = 8, P = 2
Output: 22
Explanation: Numbers are in the range [1, 8].
Number Largest Divisor not divisible by P = 2
1 1
2 1
3 3
4 1
5 5
6 3
7 7
8 1
Sum of all divisors with given constraint = 22.
Input: N = 10, P = 5
Output: 43
Explanation: Numbers are in the range [1, 8].
Number Largest Divisor not divisible by P = 5
1 1
2 2
3 3
4 4
5 1
6 6
7 7
8 8
9 9
10 2
Sum of all divisors with given constraint = 43
幼稚的方法:幼稚的想法是找到[1,N]范围内每个数字的除数,并找到不能被P和那些数字整除的最大除数。打印所有最大除数的总和。
时间复杂度: O(N 3/2 )
辅助空间: O(1)
有效的方法:我们的想法是,观察到数N的最大除数不能整除以P将是N本身,如果N不是整除被P。否则,所需的除数将与N / P相同。步骤如下:
- 如果N不能被P整除,则最大除数将为N ,将其加到最终总和上。
- 如果N可被P整除,则所需的除数将与N / P相同。
- 因此,求出所有不能被P整除的数的和,然后分别添加那些被P整除的数的除数。
- 总和为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)