forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
63 lines (53 loc) · 1.53 KB
/
B.cpp
File metadata and controls
63 lines (53 loc) · 1.53 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
62
63
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
string s;
ll leftRight() {
ll res = s[0] - '0';
for(int i = 2; i < SZ(s); i += 2) {
int cur = s[i] - '0';
if (s[i-1] == '+') {
res = res + cur;
}
else {
res = res * cur;
}
}
return res;
}
ll get(int l, int r) {
if (l == r) return s[l] - '0';
// add
for(int i = r-1; i > l; --i)
if (s[i] == '+') {
ll left = get(l, i-1);
ll right = get(i+1, r);
return left + right;
}
// mult
for(int i = r-1; i > l; --i)
if (s[i] == '*') {
ll left = get(l, i-1);
ll right = get(i+1, r);
return left * right;
}
}
int main() {
ll res;
while (cin >> s >> res) {
ll lr = leftRight();
ll m1 = get(0, SZ(s) - 1);
if (lr == res && m1 == res) cout << 'U';
else if (lr == res) cout << 'L';
else if (m1 == res) cout << 'M';
else cout << 'I';
cout << endl;
}
}