
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
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 109 +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
Example
#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; }
Output
There are 3750 ways
- Related Articles
- 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++
- Cave paintings (France)
- Ancient Egyptian Sculptures & Paintings: Innovation & Examples
- Number of Ways to Paint N × 3 Grid in C++
- What are Maandana paintings? Do they still exit?
- Number of Ways to Paint N × 3 Grid in C++ program
- Rearrange characters in a string such that no two adjacent are same in C++
- C++ code to count colors to paint elements in valid way
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- 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++
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?

Advertisements