Given N number of boxes arranged in a row and M number of colors. The task is to find the number of ways to paint those N boxes using M colors such that there are exactly K boxes with a color different from the color of the box on its left. Print this answer modulo 998244353.
Input: N = 3, M = 3, K = 0
Since the value of K is zero, no box can have a different color from color of the box on its left. Thus, all boxes should be painted with same color and since there are 3 types of colors, so there are total 3 ways.
Input: N = 3, M = 2, K = 1
Let’s number the colors as 1 and 2. Four possible sequences of painting 3 boxes with 1 box having different color from color of box on its left are (1 2 2), (1 1 2), (2 1 1) (2 2 1)
Prerequisites : Dynamic Programming
Approach: This problem can be solved using dynamic programming where dp[i][j] will denote the number of ways to paint i boxes using M colors such that there are exactly j boxes with a color different from the color of the box on its left. For every current box except 1st, either we can paint the same color as painted on its left box and solve for dp[i – 1][j] or we can paint it with remaining M – 1 colors and solve for dp[i – 1][j – 1] recursively.
Below is the implementation of the above approach:
- Color all boxes in line such that every M consecutive boxes are unique
- Minimum steps to color the tree with given colors
- Ways to color a skewed tree such that parent and child have different colors
- Find the number of boxes to be removed
- Program to Change RGB color model to HSV color model
- Queries to check if it is possible to join boxes in a circle
- Number of visible boxes after putting one inside another
- Total number of different staircase that can made from N boxes
- Minimum number of stacks possible using boxes of given capacities
- CSS | caret-color Property
- CSS | outline-color Property
- CSS | border-top-color Property
- CSS | border-color Property
- CSS | column-rule-color Property
- Change an HTML5 input placeholder color with CSS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.