Color the boxes

You are given N boxes that are kept in a straight line. You are also given M colors such that (

MN). You cannot change the position of boxes. Determine the number of ways to color the boxes such that if you select any M consecutive boxes then the color of each box is unique.

Since the number could be large, print the answer modulo 109+7.

Example:

Input:  n = 2, m = 2
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

#define MOD 1000000007
long long fact(long long n)
{
    long long dp[n + 1];
    if (n == 1)
        return 1;
    dp[1] = 1;
    for (long long i = 2i <= ni++)
        dp[i] = ((i % MOD) * (dp[i - 1] % MOD)) % MOD;
    return dp[n] % MOD;
}
int main()
{
    long long n = 2m = 2;

    long long res;
    res = fact(m);
    cout << res << "\n";
    return 0;
}


No comments:

Post a Comment