给定N个保持直线的盒子和M个颜色,使得M≤N 。盒子的位置无法更改。任务是找到给盒子上色的方法的数量,这样,如果考虑了M个连续的盒子集,那么每个盒子的颜色都是唯一的。由于答案可能很大,因此以10 9 + 7为模数打印答案。
例子:
Input: N = 3, M = 2
Output: 2
If colours are c1 and c2 the only possible
ways are {c1, c2, c1} and {c2, c1, c2}.
Input: N = 13, M = 10
Output: 3628800
方法:方法的数量与N无关,并且仅取决于M。可以用给定的M个颜色对前M个盒子进行着色而无需重复,然后可以对下一组M个盒子重复相同的图案。可以对颜色的每个排列进行此操作。因此,为盒子上色的方式将为M! 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define MOD 1000000007
// Function to return (m! % MOD)
int modFact(int n, int m)
{
int result = 1;
for (int i = 1; i <= m; i++)
result = (result * i) % MOD;
return result;
}
// Driver code
int main()
{
int n = 3, m = 2;
cout << modFact(n, m);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
static final int MOD = 1000000007;
// Function to return (m! % MOD)
static int modFact(int n, int m)
{
int result = 1;
for (int i = 1; i <= m; i++)
result = (result * i) % MOD;
return result;
}
// Driver code
public static void main (String[] args)
{
int n = 3, m = 2;
System.out.println(modFact(n, m));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
MOD = 1000000007
# Function to return (m! % MOD)
def modFact(n, m) :
result = 1
for i in range(1, m + 1) :
result = (result * i) % MOD
return result
# Driver code
n = 3
m = 2
print(modFact(n, m))
# This code is contributed by
# divyamohan123
C#
// C# implementation of the above approach
using System;
class GFG
{
const int MOD = 1000000007;
// Function to return (m! % MOD)
static int modFact(int n, int m)
{
int result = 1;
for (int i = 1; i <= m; i++)
result = (result * i) % MOD;
return result;
}
// Driver code
public static void Main()
{
int n = 3, m = 2;
Console.WriteLine(modFact(n, m));
}
}
// This code is contributed by Nidhi_biet
Javascript
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。