Ways to paint N paintings such that adjacent paintings don’t have same colors
Last Updated :
08 Jan, 2024
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 = 2
Output: 2
Input: n = 4, m = 6
Output: 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++
#include <bits/stdc++.h>
#define modd 1000000007
using namespace std;
unsigned long power(unsigned long x,
unsigned long y, unsigned long p)
{
unsigned long res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res%p * x%p) % p;
y = y >> 1;
x = (x%p * x%p) % p;
}
return res;
}
int ways( int n, int m)
{
return power(m - 1, n - 1, modd) * m % modd;
}
int main()
{
int n = 5, m = 5;
cout << ways(n, m);
return 0;
}
|
Java
class GFG
{
static final int modd = 1000000007 ;
static long power( long x, long y, long p)
{
long res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
{
res = (res%p * x%p) % p;
}
y = y >> 1 ;
x = (x%p * x%p) % p;
}
return res;
}
static int ways( int n, int m)
{
return ( int ) (power(m - 1 , n - 1 , modd)
* m % modd);
}
public static void main(String[] args)
{
int n = 5 , m = 5 ;
System.out.println(ways(n, m));
}
}
|
Python3
modd = 1000000007
def power(x, y, p):
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res % p * x % p) % p
y = y >> 1
x = (x % p * x % p) % p
return res
def ways(n, m):
return power(m - 1 , n - 1 , modd) * m % modd
n, m = 5 , 5
print (ways(n, m))
|
C#
using System;
class GFG
{
static int modd = 1000000007;
static long power( long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res%p * x%p) % p;
}
y = y >> 1;
x = (x%p * x%p) % p;
}
return res;
}
static int ways( int n, int m)
{
return ( int ) (power(m - 1, n - 1, modd)
* m % modd);
}
static public void Main ()
{
int n = 5, m = 5;
Console.WriteLine(ways(n, m));
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res % $p * $x % $p ) % $p ;
$y = $y >> 1;
$x = ( $x % $p * $x % $p ) % $p ;
}
return $res ;
}
function ways( $n , $m )
{
$modd =1000000007;
return (power( $m - 1, $n - 1,
$modd ) * $m ) % $modd ;
}
$n = 5;
$m = 5;
echo ways( $n , $m );
?>
|
Javascript
<script>
let modd = 1000000007;
function power(x, y, p)
{
let res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res%p * x%p) % p;
}
y = y >> 1;
x = (x%p * x%p) % p;
}
return res;
}
function ways(n, m)
{
return (power(m - 1, n - 1, modd) * m % modd);
}
let n = 5, m = 5;
document.write(ways(n, m));
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...