给定一个正整数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)