📜  计算删除对象的方式,以便保留恰好M个等距的对象

📅  最后修改于: 2021-06-25 10:58:24             🧑  作者: Mango

给定一个整数N ,代表彼此相邻放置的对象,任务是计算删除对象的方式数量,以使得在删除它们之后,仅剩下M个对象,并且每个相邻对象之间的距离相等。

例子:

方法:这个想法是基于这样的观察: M个具有D个相邻空间的对象的排列的长度为(M +(M – 1)* D) ,即L。对于这种安排,有(N – L + 1)个选项。因此,这个想法是遍历d0大号≤N和相应地找到的路的数目。
请按照以下步骤解决问题:

  • 如果M的值为1 ,则可能的排列数为N。因此,打印N的值。
  • 否则,请执行以下步骤:
    • 初始化两个变量,例如ans0,以存储所需布置的总数。
    • 使用变量D迭代循环。执行以下步骤:
      • D的当前值所需的总长度存储在一个变量中,比如说LM +(M – 1)*D
      • 如果L的值大于N ,则跳出循环。
      • 否则,通过将值(N – L + 1)加到变量ans来更新布置的数量。
  • 完成上述步骤后,将ans的值打印为布置的总数。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of ways of
// removing objects such that after removal,
// exactly M equidistant objects remain
void waysToRemove(int n, int m)
{
    // Store the resultant
    // number of arrangements
    int ans = 0;
 
    // Base Case: When only
    // 1 object is left
    if (m == 1) {
 
        // Print the result and return
        cout << n;
        return;
    }
 
    // Iterate until len <= n and increment
    // the distance in each iteration
    for (int d = 0; d >= 0; d++) {
 
        // Total length if adjacent
        // objects are d distance apart
        int len = m + (m - 1) * d;
 
        // If len > n
        if (len > n)
            break;
 
        // Update the number of ways
        ans += (n - len) + 1;
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    int N = 5, M = 3;
    waysToRemove(N, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to count the number of ways of
// removing objects such that after removal,
// exactly M equidistant objects remain
static void waysToRemove(int n, int m)
{
     
    // Store the resultant
    // number of arrangements
    int ans = 0;
 
    // Base Case: When only
    // 1 object is left
    if (m == 1)
    {
         
        // Print the result and return
        System.out.println(n);
        return;
    }
 
    // Iterate until len <= n and increment
    // the distance in each iteration
    for(int d = 0; d >= 0; d++)
    {
         
        // Total length if adjacent
        // objects are d distance apart
        int len = m + (m - 1) * d;
 
        // If len > n
        if (len > n)
            break;
 
        // Update the number of ways
        ans += (n - len) + 1;
    }
 
    // Print the result
    System.out.println(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 5, M = 3;
     
    waysToRemove(N, M);
}
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
 
# Function to count the number of ways of
# removing objects such that after removal,
# exactly M equidistant objects remain
def waysToRemove(n, m):
 
    # Store the resultant
    # number of arrangements
    ans = 0
 
    # Base Case: When only
    # 1 object is left
    if (m == 1):
         
        # Print the result and return
        print(n)
        return
 
    d = 0
     
    # Iterate until len <= n and increment
    # the distance in each iteration
    while d >= 0:
         
        # Total length if adjacent
        # objects are d distance apart
        length = m + (m - 1) * d
 
        # If length > n
        if (length > n):
            break
 
        # Update the number of ways
        ans += (n - length) + 1
         
        d += 1
 
    # Print the result
    print(ans)
 
# Driver Code
if __name__ == "__main__" :
 
    N = 5
    M = 3
     
    waysToRemove(N, M)
 
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
class GFG
{
     
// Function to count the number of ways of
// removing objects such that after removal,
// exactly M equidistant objects remain
static void waysToRemove(int n, int m)
{
     
    // Store the resultant
    // number of arrangements
    int ans = 0;
 
    // Base Case: When only
    // 1 object is left
    if (m == 1)
    {
         
        // Print the result and return
        Console.Write(n);
        return;
    }
 
    // Iterate until len <= n and increment
    // the distance in each iteration
    for(int d = 0; d >= 0; d++)
    {
         
        // Total length if adjacent
        // objects are d distance apart
        int len = m + (m - 1) * d;
 
        // If len > n
        if (len > n)
            break;
 
        // Update the number of ways
        ans += (n - len) + 1;
    }
 
    // Print the result
    Console.Write(ans);
}
 
 
// Driver code
static void Main()
{
    int N = 5, M = 3;
    waysToRemove(N, M);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)