-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem90.cpp
More file actions
50 lines (42 loc) · 931 Bytes
/
problem90.cpp
File metadata and controls
50 lines (42 loc) · 931 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
class AlchemistsMultiplication {
int n;
vector<long long> energy;
public:
void readInput() {
cin >> n;
energy.resize(n);
for (int i = 0; i < n; i++) {
cin >> energy[i];
}
}
void solve() {
vector<long long> left(n, 1);
vector<long long> right(n, 1);
vector<long long> result(n);
// Build prefix product
for (int i = 1; i < n; i++) {
left[i] = left[i - 1] * energy[i - 1];
}
// Build suffix product
for (int i = n - 2; i >= 0; i--) {
right[i] = right[i + 1] * energy[i + 1];
}
// Combine prefix & suffix
for (int i = 0; i < n; i++) {
result[i] = left[i] * right[i];
}
// Output
for (int i = 0; i < n; i++) {
cout << result[i] << " ";
}
cout << endl;
}
};
int main() {
AlchemistsMultiplication solver;
solver.readInput();
solver.solve();
return 0;
}