Given two integers n and m, where n represent some paintings numbered from 1 to n and m represent some colours 1 to m with unlimited amount. The task is to find the number of ways to paint the paintings such that no two consecutive paintings have the same colors.
Note: Answer must be calculated in modulo 10^9 +7 as answer can be very large.
Input: n = 4, m = 2 Output: 2 Input: n = 4, m = 6 Output: 750
Asked in : National Instruments
The total number of given color is m and the total paintings are from 1 to n. As per the condition of no two adjacent painting having the same color, first painting can be painted by anyone out of m colors and the rest of any painting can be painted by any of m-1 color except the color used for the painting just preceding that. Hence if we derive the solution for total number of ways,
m * (m-1)^(n-1) is the actual answer.
Now, this can be either calculated by simple iteration or by the method of efficient power calculation in O(logn) time.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Ways to color a skewed tree such that parent and child have different colors
- Minimum colors required such that edges forming cycle do not have same color
- Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Longest subsequence such that adjacent elements have at least one common digit
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Ways to color a 3*N board using 4 colors
- Count ways to partition a string such that both parts have equal distinct characters
- Ways to arrange Balls such that adjacent balls are of different types
- Count ways to generate N-length array with 0s, 1s, and 2s such that sum of all adjacent pairwise products is K
- Rearrange numbers in an array such that no two adjacent numbers are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Replace '?' in a string such that no two adjacent characters are same
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Count the number of ways to give ranks for N students such that same ranks are possible
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Check if two numbers have same number of digits
- Check whether two convex regular polygon have same center or not
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.