-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2608.py
More file actions
48 lines (38 loc) ยท 1022 Bytes
/
2608.py
File metadata and controls
48 lines (38 loc) ยท 1022 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
import sys
from collections import deque
input = sys.stdin.readline
dic = { "I" : 1, "V" : 5, "X" : 10, "L" : 50,
"C" : 100, "D" : 500, "M" : 1000 ,
"IV" : 4, "IX" : 9, "XL" : 40,
"XC" : 90, "CD" : 400, "CM" : 900}
rev = {}
for key, val in dic.items() :
rev[val] = key
def to_num(a) :
q = deque(a)
result = 0
while q :
cur = q.popleft()
if q and dic[cur] < dic[q[0]] :
cur += q.popleft()
result += dic[cur]
return result
dp = [0, [1], [1, 1], [1, 1, 1], [4], [5], [5, 1], [5, 1, 1], [5,1,1,1], [9]]
def to_string(n) :
answer = ''
if n >= 1000 :
answer= 'M'*(n//1000)
n %= 1000
for i in range(2, -1, -1) :
tmp = n // 10**i
cur = dp[tmp]
if cur == 0 : continue
for c in cur :
answer += rev[c*(10**i)]
n = n - c*(10**i)
return answer
A = input().strip()
B = input().strip()
a, b = to_num(A), to_num(B)
print(a+b)
print(to_string(a+b))