给定N名学生和M题集,其中M≤N 。所有M套都不同,并且每套都有足够的数量。所有的学生都坐在一排。任务是找到分发试卷的方法,以便如果选择了M个连续的学生,则每个学生都有一个唯一的试卷集。答案可能很大,因此以10 9 + 7为模数打印答案。
例子:
Input: N = 2, M = 2
Output: 2
(A, B) and (B, A) are the only possible ways.
Input: N = 15, M = 4
Output: 24
方法:可以观察到,方法的数量与N无关,并且仅取决于M。首先可以给M个学生M套,然后可以重复相同的模式。以这种方式分发试卷的方式为M! 。例如,
N = 6, M = 3
A, B, C, A, B, C
A, C, B, A, C, B
B, C, A, B, C, A
B, A, C, B, A, C
C, A, B, C, A, B
C, B, A, C, B, A
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MOD = 1000000007;
// Function to return n! % 1000000007
int factMod(int n)
{
// To store the factorial
long fact = 1;
// Find the factorial
for (int i = 2; i <= n; i++) {
fact *= (i % MOD);
fact %= MOD;
}
return fact;
}
// Function to return the
// count of possible ways
int countWays(int n, int m)
{
return factMod(m);
}
// Driver code
int main()
{
int n = 2, m = 2;
cout << countWays(n, m);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int MOD = 1000000007;
// Function to return n! % 1000000007
static int factMod(int n)
{
// To store the factorial
long fact = 1;
// Find the factorial
for (int i = 2; i <= n; i++)
{
fact *= (i % MOD);
fact %= MOD;
}
return (int)fact;
}
// Function to return the
// count of possible ways
static int countWays(int n, int m)
{
return factMod(m);
}
// Driver code
public static void main(String args[])
{
int n = 2, m = 2;
System.out.print(countWays(n, m));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
MOD = 1000000007;
# Function to return n! % 1000000007
def factMod(n) :
# To store the factorial
fact = 1;
# Find the factorial
for i in range(2, n + 1) :
fact *= (i % MOD);
fact %= MOD;
return fact;
# Function to return the
# count of possible ways
def countWays(n, m) :
return factMod(m);
# Driver code
if __name__ == "__main__" :
n = 2; m = 2;
print(countWays(n, m));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int MOD = 1000000007;
// Function to return n! % 1000000007
static int factMod(int n)
{
// To store the factorial
int fact = 1;
// Find the factorial
for (int i = 2; i <= n; i++)
{
fact *= (i % MOD);
fact %= MOD;
}
return fact;
}
// Function to return the
// count of possible ways
static int countWays(int n, int m)
{
return factMod(m);
}
// Driver code
public static void Main()
{
int n = 2, m = 2;
Console.Write(countWays(n, m));
}
}
// This code is contributed by Sanjit Prasad
输出:
2