You are given N boxes that are kept in a straight line. You are also given M colors such that (
). You cannot change the position of boxes. Determine the number of ways to color the boxes such that if you select any consecutive boxes then the color of each box is unique.
Since the number could be large, print the answer modulo .
Example:
Input: n = 2, m = 2
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;#define MOD 1000000007long long fact(long long n){long long dp[n + 1];if (n == 1)return 1;dp[1] = 1;for (long long i = 2; i <= n; i++)dp[i] = ((i % MOD) * (dp[i - 1] % MOD)) % MOD;return dp[n] % MOD;}int main(){long long n = 2, m = 2;long long res;res = fact(m);cout << res << "\n";return 0;}
No comments:
Post a Comment