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.

**Examples:**

Input:n = 4, m = 2Output:2Input:n = 4, m = 6Output:750

Asked in : National Instruments

**Approach:**

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:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `#define modd 1000000007 ` `using` `namespace` `std; ` ` ` `// Function for finding the power ` `unsigned ` `long` `power(unsigned ` `long` `x, ` ` ` `unsigned ` `long` `y, unsigned ` `long` `p) ` `{ ` ` ` `unsigned ` `long` `res = 1; ` `// Initialize result ` ` ` ` ` `x = x % p; ` `// Update x if it is more than or ` ` ` `// equal to p ` ` ` ` ` `while` `(y > 0) { ` ` ` ` ` `// If y is odd, multiply x with result ` ` ` `if` `(y & 1) ` ` ` `res = (res * x) % p; ` ` ` ` ` `// y must be even now ` ` ` `y = y >> 1; ` `// y = y/2 ` ` ` `x = (x * x) % p; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to calculate the number of ways ` `int` `ways(` `int` `n, ` `int` `m) ` `{ ` ` ` `// Answer must be modulo of 10^9 + 7 ` ` ` `return` `power(m - 1, n - 1, modd) * m % modd; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5, m = 5; ` ` ` `cout << ways(n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `class` `GFG ` `{ ` ` ` `static` `final` `int` `modd = ` `1000000007` `; ` ` ` ` ` `// Function for finding the power ` ` ` `static` `long` `power(` `long` `x, ` `long` `y, ` `long` `p) ` ` ` `{ ` ` ` `long` `res = ` `1` `; ` `// Initialize result ` ` ` ` ` `// Update x if it is more than or ` ` ` `// equal to p ` ` ` `x = x % p; ` ` ` ` ` `while` `(y > ` `0` `) ` ` ` `{ ` ` ` `// If y is odd, multiply x with result ` ` ` `if` `(y % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `res = (res * x) % p; ` ` ` `} ` ` ` ` ` `// y must be even now ` ` ` `y = y >> ` `1` `; ` `// y = y/2 ` ` ` `x = (x * x) % p; ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Function to calculate the number of ways ` ` ` `static` `int` `ways(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `// Answer must be modulo of 10^9 + 7 ` ` ` `return` `(` `int` `) (power(m - ` `1` `, n - ` `1` `, modd) ` ` ` `* m % modd); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `, m = ` `5` `; ` ` ` `System.out.println(ways(n, m)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` ` ` `modd ` `=` `1000000007` ` ` `# Function for finding the power ` `def` `power(x, y, p): ` ` ` ` ` `res ` `=` `1` `# Initialize result ` ` ` ` ` `x ` `=` `x ` `%` `p ` `# Update x if it is more ` ` ` `# than or equal to p ` ` ` ` ` `while` `(y > ` `0` `): ` ` ` ` ` `# If y is odd, multiply x with result ` ` ` `if` `(y & ` `1` `): ` ` ` `res ` `=` `(res ` `*` `x) ` `%` `p ` ` ` ` ` `# y must be even now ` ` ` `y ` `=` `y >> ` `1` `# y = y/2 ` ` ` `x ` `=` `(x ` `*` `x) ` `%` `p ` ` ` ` ` `return` `res ` ` ` `# Function to calculate the number of ways ` `def` `ways(n, m): ` ` ` ` ` `# Answer must be modulo of 10^9 + 7 ` ` ` `return` `power(m ` `-` `1` `, n ` `-` `1` `, modd) ` `*` `m ` `%` `modd ` ` ` `# Driver code ` `n, m ` `=` `5` `, ` `5` `print` `(ways(n, m)) ` ` ` `# This code is contributed ` `# by Mohit Kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `modd = 1000000007; ` ` ` ` ` `// Function for finding the power ` ` ` `static` `long` `power(` `long` `x, ` `long` `y, ` `long` `p) ` ` ` `{ ` ` ` `long` `res = 1; ` `// Initialize result ` ` ` ` ` `// Update x if it is more than or ` ` ` `// equal to p ` ` ` `x = x % p; ` ` ` ` ` `while` `(y > 0) ` ` ` `{ ` ` ` `// If y is odd, multiply x with result ` ` ` `if` `(y % 2 == 1) ` ` ` `{ ` ` ` `res = (res * x) % p; ` ` ` `} ` ` ` ` ` `// y must be even now ` ` ` `y = y >> 1; ` `// y = y/2 ` ` ` `x = (x * x) % p; ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Function to calculate the number of ways ` ` ` `static` `int` `ways(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `// Answer must be modulo of 10^9 + 7 ` ` ` `return` `(` `int` `) (power(m - 1, n - 1, modd) ` ` ` `* m % modd); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 5, m = 5; ` ` ` `Console.WriteLine(ways(n, m)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Iterative Function to calculate ` `// (x^y)%p in O(log y) ` `function` `power(` `$x` `, ` `$y` `, ` `$p` `) ` `{ ` ` ` `// Initialize result ` ` ` `$res` `= 1; ` ` ` ` ` `// Update x if it is more ` ` ` `// than or equal to p ` ` ` `$x` `= ` `$x` `% ` `$p` `; ` ` ` ` ` `while` `(` `$y` `> 0) ` ` ` `{ ` ` ` `// If y is odd, multiply ` ` ` `// x with result ` ` ` `if` `(` `$y` `& 1) ` ` ` `$res` `= (` `$res` `* ` `$x` `) % ` `$p` `; ` ` ` ` ` `// y must be even now ` ` ` ` ` `// y = $y/2 ` ` ` `$y` `= ` `$y` `>> 1; ` ` ` `$x` `= (` `$x` `* ` `$x` `) % ` `$p` `; ` ` ` `} ` ` ` `return` `$res` `; ` `} ` ` ` `// Function to calculate the number of ways ` `function` `ways(` `$n` `, ` `$m` `) ` `{ ` ` ` `$modd` `=1000000007; ` ` ` ` ` `// Answer must be modulo of 10^9 + 7 ` ` ` `return` `(power(` `$m` `- 1, ` `$n` `- 1, ` ` ` `$modd` `) * ` `$m` `) % ` `$modd` `; ` `} ` ` ` `// Driver code ` `$n` `= 5; ` `$m` `= 5; ` `echo` `ways(` `$n` `, ` `$m` `); ` ` ` `// This code is contributed ` `// by Arnab Kundu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1280

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.

## Recommended Posts:

- 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
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest subsequence such that adjacent elements have at least one common digit
- Count ways to partition a string such that both parts have equal distinct characters
- Ways to color a 3*N board using 4 colors
- Ways to arrange Balls such that adjacent balls are of different types
- 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
- 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
- Number of ways to form an array with distinct adjacent elements
- Minimum inversions required so that no two adjacent elements are same

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.