Number of ways to distribute N Paper Set among M students

Given N students and a total of M sets of question paper where M ≤ N. All the M sets are different and every sets is available in sufficient quantity. All the students are sitting in a single row. The task is to find the number of ways to distribute the question paper so that if any M consecutive students are selected then each student has a unique question paper set. The answer could be large, so print the answer modulo 109 + 7.

Example:

Input: N = 2, M = 2
Output: 2
(A, B) and (B, A) are the only possible ways.



Input: N = 15, M = 4
Output: 24

Approach: It can be observed that the number of ways are independent of N and only depend on M. First M students can be given M sets and then the same pattern can be repeated. The number of ways to distribute the question paper in this way is M!. For example,

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

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
2



Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :