- 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

In this problem, you are given N painting and we have m color that we can make paintings with and we need to find the number of ways in which we can draw the painting such that none of the same color paintings are to each other.

The program’s output can have very large values and handing these values is a bit problem so we will calculate its answer in standard modulo 10^{9} +7.

The formula to find the number ways is :

Ways = n*(m-1)(n-1)

Example to describe the problem, this will need the number of paintings n and number of colors m :

**Input**

n = 5 ,m = 6

**Output**

3750

#include <iostream> #include<math.h> #define modd 1000000007 using namespace std; unsigned long find(unsigned long x, unsigned long y, unsigned long p) { unsigned long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int ways(int n, int m) { return find(m - 1, n - 1, modd) * m % modd; } int main() { int n = 5, m = 6; cout<<"There are"<<ways(n, m)<<"ways"; return 0; }

There are 3750 ways

- Related Questions & Answers
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C++
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- Handshakes That Don't Cross in C++
- What are Maandana paintings? Do they still exit?
- Number of Ways to Paint N × 3 Grid in C++
- 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
- What are some signs that tell me that I don't have enough protein in my diet?
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Don’t Miss the Milestones – Sequence the Activities
- I have a unique startup idea, but don’t know where to start? What should I do?
- C program that won’t compile in C++
- Why don’t we create green top pitches in India?
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++

Advertisements