给定整数N ,任务是找到在3个人之间分配N的总数,使得:
- 恰好一个人在这三个人中获得的物品数最多。
- 每个人至少获得1件物品。
例子:
Input: N = 5
Output: 3
Explanation:
3 way distribute the item among 3 people are {1, 1, 3}, {1, 3, 1} and {3, 1, 1}.
Distributions like {1, 2, 2} or {2, 1, 2} are not valid as two persons are getting the maximum.
Input: N = 10
Output: 33
Explanation:
For the Input N = 10 there are 33 ways of distribution.
方法:
为了解决上述问题,我们必须观察到,如果N <4 ,那么这种分布是不可能的。
对于所有N≥4的值,请按照以下步骤解决问题:
- (N – 1)*(N – 2)/ 2给出了在3个人中分配N项的总方法数。
- 初始化变量s = 0 ,该变量存储无法进行分配的方式的数量。
- 迭代两个嵌套循环,其中i介于[2,N – 3]和j之间,直至i :
- 对于每次迭代,请检查N = 2 * i + j是否为2,即最多可以接收2个元素
- 如果是这样,则将s加1 。如果N可被3整除,则将s更新3 * s + 1 。否则,更新为3 * s 。
- 最后,返回ans –作为在三个人之间分配N个项目的总数。
下面是上述方法的实现:
C++
// C++ program to find the number
// of ways to distribute N item
// among three people such
// that one person always gets
// the maximum value
#include
using namespace std;
// Function to find the number
// of ways to distribute N
// items among 3 people
int countWays(int N)
{
// No distribution
// possible
if (N < 4)
return 0;
// Total number of ways to
// distribute N items
// among 3 people
int ans = ((N - 1) * (N
- 2))
/ 2;
// Store the number of
// distributions which
// are not possible
int s = 0;
for (int i = 2; i <= N - 3;
i++) {
for (int j = 1; j < i;
j++) {
// Count possiblities
// of two persons
// receiving the
// maximum
if (N == 2 * i + j)
s++;
}
}
// If N is divisible by 3
if (N % 3 == 0)
s = 3 * s + 1;
else
s = 3 * s;
// Return the final
// count of ways
// to distribute
return ans - s;
}
// Driver Code
int main()
{
int N = 10;
cout << countWays(N);
return 0;
}
Java
// Java program to find the number
// of ways to distribute N item
// among three people such
// that one person always gets
// the maximum value
class GFG{
// Function to find the number
// of ways to distribute N
// items among 3 people
static int countWays(int N)
{
// No distribution
// possible
if (N < 4)
return 0;
// Total number of ways to
// distribute N items
// among 3 people
int ans = ((N - 1) * (N - 2)) / 2;
// Store the number of
// distributions which
// are not possible
int s = 0;
for (int i = 2; i <= N - 3; i++)
{
for (int j = 1; j < i; j++)
{
// Count possiblities
// of two persons
// receiving the
// maximum
if (N == 2 * i + j)
s++;
}
}
// If N is divisible by 3
if (N % 3 == 0)
s = 3 * s + 1;
else
s = 3 * s;
// Return the final
// count of ways
// to distribute
return ans - s;
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
System.out.println(countWays(N));
}
}
// This code is contributed by rock_cool
Python3
# Python3 program to find the number
# of ways to distribute N item
# among three people such
# that one person always gets
# the maximum value
# Function to find the number
# of ways to distribute N
# items among 3 people
def countWays(N):
# No distribution
# possible
if (N < 4):
return 0
# Total number of ways to
# distribute N items
# among 3 people
ans = ((N - 1) * (N - 2)) // 2
# Store the number of
# distributions which
# are not possible
s = 0
for i in range( 2, N - 2, 1):
for j in range( 1, i, 1):
# Count possiblities
# of two persons
# receiving the
# maximum
if (N == 2 * i + j):
s += 1
# If N is divisible by 3
if (N % 3 == 0):
s = 3 * s + 1
else:
s = 3 * s
# Return the final
# count of ways
# to distribute
return ans - s
# Driver Code
N = 10
print (countWays(N))
# This code is contributed by sanjoy_62
C#
// C# program to find the number
// of ways to distribute N item
// among three people such
// that one person always gets
// the maximum value
using System;
class GFG{
// Function to find the number
// of ways to distribute N
// items among 3 people
static int countWays(int N)
{
// No distribution
// possible
if (N < 4)
return 0;
// Total number of ways to
// distribute N items
// among 3 people
int ans = ((N - 1) * (N - 2)) / 2;
// Store the number of
// distributions which
// are not possible
int s = 0;
for (int i = 2; i <= N - 3; i++)
{
for (int j = 1; j < i; j++)
{
// Count possiblities
// of two persons
// receiving the
// maximum
if (N == 2 * i + j)
s++;
}
}
// If N is divisible by 3
if (N % 3 == 0)
s = 3 * s + 1;
else
s = 3 * s;
// Return the final
// count of ways
// to distribute
return ans - s;
}
// Driver Code
public static void Main()
{
int N = 10;
Console.Write(countWays(N));
}
}
// This code is contributed by Code_Mech
Javascript
输出:
33
时间复杂度: O(N 2 )
辅助空间: O(1)