-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaint_fence.java
More file actions
32 lines (30 loc) · 774 Bytes
/
Paint_fence.java
File metadata and controls
32 lines (30 loc) · 774 Bytes
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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
if(n == 1)
{
System.out.print(k+k);
return ;
}else if(n == 2)
{
int res = k+k*(k-1);
System.out.println(res);
return ;
}else{
int one = k ;
int two = k*(k-1);
for(int i = 3 ; i <= n ; i++)
{
int temp = one ;
one = two ;
two = (two + temp)*(k-1) ;
}
int ans = one + two ;
System.out.println(ans);
}
}
}