-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemnale.java
115 lines (94 loc) · 2.75 KB
/
Semnale.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Buliga Theodor Ioan
// 323 CA
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class Semnale {
static int sig_type, x, y;
static final int mod = 1000000007;
Semnale(){}
static int type1() {
// 3d matrix to store
// the number of bits (x + y + 1)
// the number of zeros (x + 1)
// and what the signal is ending with (2 -> 0 and 1)
int [][][]dp = new int[x + y + 1][x + 1][2];
// signals ending in 0
dp[1][1][0] = 1;
// signals ending in 1
dp[1][0][1] = 1;
// computing the number of signals
// iterating though the number of bits
for (int i = 2; i <= x + y; i++) {
for (int j = 1; j <= x; j++) {
// adding the bits from the anterior step
// computing the number of signals of the current iteration
// ending in 0 by adding the number of signals ending in 0
// and the ones ending in one from the anterior step
dp[i][j][0] = (dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1]) % mod;
// because I cannot have two bits of 1 next to each other
// I set it to the number of signals ending in 0 from
// the anterior step
dp[i][j][1] = (dp[i - 1][j][0]);
}
}
// returning the number of signals
return (dp[x + y][x][1] + dp[x + y][x][0]) % mod;
}
static int type2() {
//TODO Compute the number of type 2 signals.
if (y > 2 * (x + 1)) {
return 0;
}
int [][][]dp = new int[x + y + 1][x + 1][2];
// same as above, but this time, I can have
// two bits of 1 next to each other
dp[1][1][0] = 1;
dp[1][0][1] = 1;
dp[2][0][1] = 1;
// computing the number of signals exactly as above
for (int i = 2; i <= x + y; i++) {
for (int j = 1; j <= x; j++) {
dp[i][j][0] = (dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1]) % mod;
// the number of signals ending in 1 here
// is the number of signals ending in 0 from the anterior
// step added to the ones from two steps behind
dp[i][j][1] = (dp[i - 1][j][0] + dp[i - 2][j][0]) % mod;
}
}
// returning the number of signals
return (dp[x + y][x][1] + dp[x + y][x][0]) % mod;
}
public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("semnale.in"));
sig_type = sc.nextInt();
x = sc.nextInt();
y = sc.nextInt();
int ans;
switch (sig_type) {
case 1:
ans = Semnale.type1();
break;
case 2:
ans = Semnale.type2();
break;
default:
ans = -1;
System.out.println("wrong task number");
}
try {
FileWriter fw = new FileWriter("semnale.out");
fw.write(Integer.toString(ans));
fw.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
sc.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}