- Related Questions & Answers
- Average numbers in array in C Programming
- Add minimum number to an array so that the sum becomes even in C programming
- Arithmetic Mean in C programming
- C++ Program for Dijkstra’s shortest path algorithm?
- Concatenate a string given number of times in C++ programming
- Write a program to Delete a Tree in C programming
- Write a function that returns 2 for input 1 and returns 1 for 2 in C programming
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
- Surface Area and Volume of Hexagonal Prism in C programming
- Superperfect Number in C programming
- Super Prime in c programming
- Sums of ASCII values of each word in a sentence in c programming
- Sum triangle from an array in C programming
- C Programming for sum of the series 0.6, 0.06, 0.006, 0.0006, …to n terms
- C Programming for Sum of sequence 2, 22, 222, ………

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

Advertisements