- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScenes.java
More file actions
Latest commit
61 lines (53 loc) · 1.82 KB
/
Copy pathScenes.java
File metadata and controls
61 lines (53 loc) · 1.82 KB
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
/**
* Solution to the Mountain Scenes problem (https://open.kattis.com/problems/scenes)
*
* <p>Solution by: William Fiset
*/
importjava.io.*;
importjava.util.*;
publicclassScenes {
staticLong[][] dp;
staticintN, W, H;
staticlongMOD = 1_000_000_007;
staticBufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
publicstaticvoidmain(String[] args) throwsIOException {
String[] ln = br.readLine().split(" ");
N = Integer.parseInt(ln[0]); // Total ribbon length
W = Integer.parseInt(ln[1]); // Width
H = Integer.parseInt(ln[2]); // Height
solution1();
}
// Recursive solution implementation
staticvoidsolution1() {
// Count the number of plain mountains. Be sure to account for having more
// ribbon than can possible be placed.
intribbonSquares = Math.min(W * H, N);
intplains = (ribbonSquares / W) + 1;
dp = newLong[W + 1][N + 1];
longans = ((f(1, N) - plains) + MOD) % MOD;
System.out.println(ans);
}
staticlongf(intw, intribbon) {
// We're trying to create a mountain scene for which we don't have enough ribbon material for.
if (ribbon < 0) {
return0;
}
// When the width value exceeds the frame width, we know we’ve finished creating a unique
// mountain scene!
if (w > W) {
return1;
}
if (dp[w][ribbon] != null) {
returndp[w][ribbon];
}
longscenes = 0L;
// Try placing all possible ribbon lengths at this column/width
for (intlen = 0; len <= H; len++) {
scenes = (scenes + f(w + 1, ribbon - len));
}
// Store and return the number of scenes for this sub-problem.
// Applying the MOD after the loop is safe since H is at most 100 and
// the MOD is 10^9+7 which cannot overflow a signed 64bit integer.
returndp[w][ribbon] = scenes % MOD;
}
}