给定正整数N和K ,任务是将N分成K个部分,使得第一部分的值为X ,第二部分的值为2X ,以此类推,对于X的某个值。如果无法进行这种划分,则打印-1 。
例子:
Input: N = 10, K = 4
Output: 1 2 3 4
Explanation:
If we take 1 as first number second number will be 2 third number will be 3 times first which is 3 and the last number will be 4 times third number so last number is 4. We can note that sum=1+2+3+4=10 which is the required sum.
Input N = 10, K = 3
Output: -1
Explanation:
Distributing N in 3 parts with given constraint is not possible.
方法:为了解决上述问题,让我们了解它的数学实现。让划分为X 1,X 2,X 3到X K,其中所述第二整数为X 1 * 2,第三个是X 1 * 3和第K个一个是X 1 * K.
We know that,
=>
=>
=>
=> , where ( 1 + 2 + 3 + … + K) =
=>
因此,要解决该问题,我们必须遵循以下步骤:
- 计算K *(K +1)的值,然后将2 * N除以K *(K +1) ,以获得X 1的值。
- 如果在上述步骤中X 1不是整数,则打印-1,因为不可能进行这种除法。
- 要获得X 2的值,我们将X 1乘以2。类似地,要获得X K将X 1乘以K。
- 找到所有值后,将它们打印出来。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
typedef long long int ll;
// Function to find the division
void solve(int n, int k)
{
int x1, d;
// Calculating value of x1
d = k * (k + 1);
// Print -1 if division
// is not possible
if ((2 * n) % d != 0) {
cout << "-1";
return;
}
x1 = 2 * n / d;
// Get the first number ie x1
// then successively multiply
// it by x1 k times by index number
// to get the required answer
for (int i = 1; i <= k; i++) {
cout << x1 * i << " ";
}
cout << endl;
}
// Driver Code
int main()
{
// Given N and K
int n = 10, k = 4;
// Function Call
solve(n, k);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the division
static void solve(int n, int k)
{
int x1, d;
// Calculating value of x1
d = k * (k + 1);
// Print -1 if division
// is not possible
if ((2 * n) % d != 0)
{
System.out.print("-1");
return;
}
x1 = 2 * n / d;
// Get the first number ie x1
// then successively multiply
// it by x1 k times by index number
// to get the required answer
for (int i = 1; i <= k; i++)
{
System.out.print(x1 * i+ " ");
}
System.out.println();
}
// Driver Code
public static void main(String[] args)
{
// Given N and K
int n = 10, k = 4;
// Function Call
solve(n, k);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find the division
def solve(n, k):
# Calculating value of x1
d = k * (k + 1);
# Print -1 if division
# is not possible
if ((2 * n) % d != 0):
print("-1");
return;
x1 = 2 * n // d;
# Get the first number ie x1
# then successively multiply
# it by x1 k times by index number
# to get the required answer
for i in range(1, k + 1):
print(x1 * i, end = " ");
# Driver Code
# Given N and K
n = 10; k = 4;
# Function Call
solve(n, k);
# This code is contributed by Code_Mech
C#
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the division
static void solve(int n, int k)
{
int x1, d;
// Calculating value of x1
d = k * (k + 1);
// Print -1 if division
// is not possible
if ((2 * n) % d != 0)
{
System.out.print("-1");
return;
}
x1 = 2 * n / d;
// Get the first number ie x1
// then successively multiply
// it by x1 k times by index number
// to get the required answer
for (int i = 1; i <= k; i++)
{
System.out.print(x1 * i+ " ");
}
System.out.println();
}
// Driver Code
public static void main(String[] args)
{
// Given N and K
int n = 10, k = 4;
// Function Call
solve(n, k);
}
}
// This code is contributed by 29AjayKumar
1 2 3 4
时间复杂度: O(K)
辅助空间: O(1)