📌  相关文章
📜  将 N 以 (X, 2X, … , KX) 的形式分成 K 个部分以获得 X 的某个值

📅  最后修改于: 2021-09-16 11:07:05             🧑  作者: Mango

给定一个正整数NK ,任务是将N分成K部分,使得第一部分的值为X ,第二部分的值为2X ,依此类推,对于X 的某个值。如果这样的划分是不可能的,那么打印-1

例子:

方法:为了解决上面提到的问题,让我们了解它的数学实现。设除法为 X 1 , X 2 , X 3到 X K ,其中第二个整数是 X 1 * 2,第三个是 X 1 * 3,第 K 个是 X 1 * K。

所以要解决这个问题,我们必须按照下面给出的步骤:

  • 计算K * (K + 1) 的值并将2 * N除以K * (K + 1)以获得 X 1 的值。
  • 如果在上述步骤中 X 1不是整数,则打印-1,因为不可能进行这样的除法。
  • 为了得到 X 2的值,我们将 X 1乘以 2。类似地,为了得到 X K乘以 X 1K
  • 找到所有值后打印它们。

下面是上述方法的实现:

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)