-
Notifications
You must be signed in to change notification settings - Fork 617
/
paint_fence.cpp
65 lines (59 loc) · 2.11 KB
/
paint_fence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem statement:
Given a fence with n posts and k colors , find out the number of ways of painting the fence
such that atmost 2 adjacent posts have the same color.
Suppose we have a single post, The number of ways of painting the fence with k colors is k
Now we consider 2 posts, Then the number of ways of painting the second fence with the same
color as the first post is k, and the number of ways of painting the second post with a different
color is k*(k-1) as we have k-1 colors that are different from the first post's color.
So the total number of ways of painting 2 fences is k*(k-1) + k. Now we consider 3 posts,
Then the number of ways of painting the third fence same as the second fence is the number of
ways of painting the second fence with a different color that is k*(k-1). The number of ways of
painting the third fence using a different color is equal to the total number of ways of
painting the second fence * (k-1). Hence the total number of ways of painting 3 fences is k*(k-1) + (k+k*(k-1))*(k-1).
This way we can find the total number of ways of painting n fences
*/
#include <bits/stdc++.h>
using namespace std;
unsigned long long MOD = 1000000007;
//paintfence function uses permutations to find the number of ways of painting fences
unsigned long long paintfence(int n, int k)
{
unsigned long long total, diff, same;
same = k;
diff = k * (k - 1);
total = k * k;
for (int i = 1; i < n - 1; i++)
{
same = diff;
diff = total * (k - 1);
total = same + diff;
}
return total%MOD;
}
int main()
{
int n, k;
cout << "Enter the number of posts" << endl;
cin >> n;
cout << "Enter the number of colors" << endl;
cin >> k;
cout << "The number of ways of painting the fence is : " << paintfence(n, k) << endl;
return 0;
}
/*
Sample I/O:
Enter the number of posts
2
Enter the number of colors
4
The number of ways of painting the fence is : 16
Enter the number of posts
3
Enter the number of colors
2
The number of ways of painting the fence is : 6
Complexity Analysis
Time complexity - O(n)
Space complexity - O(1), where n is the number of posts
*/