-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructTheArrayDP
50 lines (33 loc) · 1.07 KB
/
ConstructTheArrayDP
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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the countArray function below.
static long countArray(int n, int k, int x) {
final long MOD = 1000000007;
long[] a = new long[n];
long[] b = new long[n];
a[0] = x == 1 ? 1 : 0;
b[0] = x == 1 ? 0 : 1;
//O(n) time and space
for (int i = 1; i < n; i++) {
a[i] = b[i-1] % MOD;
b[i] = (a[i - 1] * (k - 1) + b[i - 1] * (k - 2)) % MOD;
}
return a[n-1];
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
String[] nkx = scanner.nextLine().split(" ");
int n = Integer.parseInt(nkx[0]);
int k = Integer.parseInt(nkx[1]);
int x = Integer.parseInt(nkx[2]);
long answer = countArray(n, k, x);
System.out.println(answer);
scanner.close();
}
}