P18115 [GeeksforGeeks] Painting the fence3727
普及
题目描述

Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive posts have the same colors. Since the answer can be large return it modulo 10^9 + 7.
Example 1:
Input:
N=3, K=2
Output: 6
Explanation:
We have following possible combinations:

Example 2:
Input:
N=2, K=4
Output: 16
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 10^{9} + 7)

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Constraints:
1 ≤ N ≤ 5000
1 ≤ K ≤ 100

输入格式

见题面

输出格式

见题面

样例输入
N=3, K=2
样例输出
6