给定一个整数N ,代表彼此相邻放置的对象,任务是计算删除对象的方式数量,以使得在删除它们之后,仅剩下M个对象,并且每个相邻对象之间的距离相等。
例子:
Input: N = 5, M = 3
Output: 4
Explanation:
Let the initial arrangement be A1 A2 A3 A4 A5.
The following arrangements are possible:
- A1 A2 A3 _ _
- _ A2 A3 A4 _
- _ _ A3 A4 A5
- A1_ A3_ A5
Therefore, the total count of possible arrangements is 4.
Input: N = 2, M = 1
Output: 2
方法:这个想法是基于这样的观察: M个具有D个相邻空间的对象的排列的长度为(M +(M – 1)* D) ,即L。对于这种安排,有(N – L + 1)个选项。因此,这个想法是遍历d从0至大号≤N和相应地找到的路的数目。
请按照以下步骤解决问题:
- 如果M的值为1 ,则可能的排列数为N。因此,打印N的值。
- 否则,请执行以下步骤:
- 初始化两个变量,例如ans到0,以存储所需布置的总数。
- 使用变量D迭代循环。执行以下步骤:
- 将D的当前值所需的总长度存储在一个变量中,比如说L为M +(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.
输出:
4
时间复杂度: O(N)
辅助空间: O(1)