-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_PaintingTheFence.cpp
90 lines (71 loc) · 1.74 KB
/
GFG_PaintingTheFence.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
https://practice.geeksforgeeks.org/problems/painting-the-fence3727/1/#
Painting the Fence
*/
// { Driver Code Starts
#include <bits/stdc++.h>
#include<stdio.h>
#include<math.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
const int mod = 1e9 + 7;
long long countWays(int n, int k){
if(n==1)
return k;
if(n==2)
return k*k;
if(n==3)
return k*k*k-k;
vector<long long int> dp(n+1, 0);
// dp[0] = 0;
dp[1] = k;
dp[2] = k*k;
for(int i=3; i<=n; i++)
{
dp[i] = ((dp[i-1] + dp[i-2])*(k-1))%mod;
}
return dp[n];
}
/*//TLE
vector<vector<vector<int>>> memo;
long long countWays(int n, int k){
memo.resize(n+1, vector<vector<int>>(k+1, vector<int>(k+1, -1)));
return solve(0, 0, 0, n, k);
}
long long solve(int idx, int last, int seclast, int n, int k)
{
// if(n==0)
// return 0;
// if(n==1)
// return k;
if(idx == n)
return memo[idx][last][seclast] = 1;
if(memo[idx][last][seclast] != -1)
memo[idx][last][seclast];
long long cnt=0;
for(int col=1; col<=k; col++)
{
if( col == last and col == seclast)
continue;
cnt+= solve(idx+1, col, last, n, k)%mod;
}
return memo[idx][last][seclast] = cnt%mod;
}
*/
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
Solution ob;
cout<<ob.countWays(n,k)<<endl;
}
return 0;
} // } Driver Code Ends