给定正整数N ,任务是将其划分为K个唯一的部分,以使这些部分的总和等于原始数,并且所有部分的gcd最大。如果存在这样的除法,则打印最大的gcd ,否则打印-1 。
例子:
Input: N = 6, K = 3
Output: 1
Only possible division with unique
elements is (1, 2, 3) and gcd(1, 2, 3) = 1.
Input: N = 18, K = 3
Output: 3
天真的方法:找到N的所有可能除法,并计算它们的最大gcd。但是这种方法将花费指数时间和空间。
高效的方法:让N的除法为A 1 ,A 2 ……..A K
现在,已知gcd(a,b)= gcd(a,b,a + b) ,因此gcd(A 1 ,A 2 ….A K )= gcd(A 1 ,A 2 ….A K ,A 1 + A 2 …。+ A K )
但是A 1 + A 2 …。 + A K = N ,因此,除法的gcd将成为N的因素之一。
设a为N的因子,这可能是答案:
由于是GCD,所有的分裂将是的倍数,因此,对于K独特B I,
a * B 1 + a * B 2 ……。 + a * B K = N
a *(B 1 + B 2 ……. + B K )= N
由于所有的B i都是唯一的,
B 1 + B 2 ……。+ B K≥1 + 2 + 3….. + K
乙1 + B 2 ……。+ b k的序列≥K *(K + 1)/ 2
因此,互补因子大于或等于K *(K +1)/ 2的所有N个因子都可以作为可能的答案之一,而我们已考虑了所有可能的答案中的最大值。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
// Function to calculate maximum GCD
int maxGCD(int N, int K)
{
// Minimum possible sum for
// K unique positive integers
int minSum = (K * (K + 1)) / 2;
// It is not possible to divide
// N into K unique parts
if (N < minSum)
return -1;
// All the factors greater than sqrt(N)
// are complementary of the factors less
// than sqrt(N)
int i = sqrt(N);
int res = 1;
while (i >= 1) {
// If i is a factor of N
if (N % i == 0) {
if (i >= minSum)
res = max(res, N / i);
if (N / i >= minSum)
res = max(res, i);
}
i--;
}
return res;
}
// Driver code
int main()
{
int N = 18, K = 3;
cout << maxGCD(N, K);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
import java.lang.Math;
class GFG
{
// Function to calculate maximum GCD
static int maxGCD(int N, int K)
{
// Minimum possible sum for
// K unique positive integers
int minSum = (K * (K + 1)) / 2;
// It is not possible to divide
// N into K unique parts
if (N < minSum)
return -1;
// All the factors greater than sqrt(N)
// are complementary of the factors less
// than sqrt(N)
int i = (int) Math.sqrt(N);
int res = 1;
while (i >= 1)
{
// If i is a factor of N
if (N % i == 0)
{
if (i >= minSum)
res = Math.max(res, N / i);
if (N / i >= minSum)
res = Math.max(res, i);
}
i--;
}
return res;
}
// Driver code
public static void main (String[] args)
{
int N = 18, K = 3;
System.out.println(maxGCD(N, K));
}
}
// This code is contributed by ApurvaRaj
Python
# Python3 implementation of the approach
from math import sqrt,ceil,floor
# Function to calculate maximum GCD
def maxGCD(N, K):
# Minimum possible sum for
# K unique positive integers
minSum = (K * (K + 1)) / 2
# It is not possible to divide
# N into K unique parts
if (N < minSum):
return -1
# All the factors greater than sqrt(N)
# are complementary of the factors less
# than sqrt(N)
i = ceil(sqrt(N))
res = 1
while (i >= 1):
# If i is a factor of N
if (N % i == 0):
if (i >= minSum):
res = max(res, N / i)
if (N / i >= minSum):
res = max(res, i)
i-=1
return res
# Driver code
N = 18
K = 3
print(maxGCD(N, K))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to calculate maximum GCD
static int maxGCD(int N, int K)
{
// Minimum possible sum for
// K unique positive integers
int minSum = (K * (K + 1)) / 2;
// It is not possible to divide
// N into K unique parts
if (N < minSum)
return -1;
// All the factors greater than sqrt(N)
// are complementary of the factors less
// than sqrt(N)
int i = (int) Math.Sqrt(N);
int res = 1;
while (i >= 1)
{
// If i is a factor of N
if (N % i == 0)
{
if (i >= minSum)
res = Math.Max(res, N / i);
if (N / i >= minSum)
res = Math.Max(res, i);
}
i--;
}
return res;
}
// Driver code
public static void Main()
{
int N = 18, K = 3;
Console.WriteLine(maxGCD(N, K));
}
}
// This code is contributed by AnkitRai01
3