- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Ways to paint N paintings such that adjacent paintings don’t have same colors in C++

In this problem, we are given two integers n and m, where n is the number of paintings and m is the number of colors available. Our task is to create a program that will find the total number of ways in which we can paint the paintings in such a way that no to consecutive paintings have the same color.

Let’s take an example to understand the problem,

## Input

n = 3, m =3

## Output

12

## Explanation

P1 P2 P3 C1 C2 C3 C1 C3 C2 C1 C2 C1 C1 C3 C1 C2 C1 C2 C2 C3 C2 C2 C1 C3 C2 C3 C1 C3 C1 C3 C3 C2 C3 C3 C1 C2 C3 C2 C1

To solve this problem, we can paint all n paintings with m colors, now the next paintings can be painted using n-1 color excluding the color used to paint the last painting. So, the total number of ways is,

n*(m-1)^{(n-1)}

Program to show the implementation of our solution,

## Example

#include <iostream> #define modd 1000000007 using namespace std; unsigned long calcPower(unsigned long base, unsigned long power, unsigned long p){ unsigned long result = 1; base = base % p; while (power > 0) { if (power & 1) result = (result * base) % p; power = power >> 1; base = (base * base) % p; } return result; } int colorPainting(int n, int m){ return calcPower(m - 1, n - 1, modd) * m % modd; } int main(){ int n = 5, m = 7; cout<<"The number of ways to color the given paintings is : "<<colorPainting(n, m); return 0; }

## Output

The number of ways to color the given paintings is : 9072

- Related Questions & Answers
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- What are Maandana paintings? Do they still exit?
- Handshakes That Don't Cross in C++
- Number of Ways to Paint N × 3 Grid in C++
- What are some signs that tell me that I don't have enough protein in my diet?
- Rearrange characters in a string such that no two adjacent are same in C++
- Number of Ways to Paint N × 3 Grid in C++ program
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?
- C++ code to count colors to paint elements in valid way
- Why interfaces don't have static initialization block when it can have static methods alone in java?
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent in C++
- Ways to place items in n^2 positions such that no row/column contains more than one in C++
- C++ Balanced expressions such that given positions have opening brackets

Advertisements