-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlong.py
More file actions
113 lines (102 loc) · 4.95 KB
/
Copy pathlong.py
File metadata and controls
113 lines (102 loc) · 4.95 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
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
import string
class longint (object) :
def __init__(self, string) :
self.arr = []
self.minus = 0
length = len(string)
for i in reversed(xrange(0,length,1)) :
number = ord(string[i]) - 48
self.arr.append(number)
def out(self):
res = ""
length = len(self.arr)
if self.minus == 1 :
res += "-"
for i in reversed(xrange(0,length,1)) :
res += chr(self.arr[i] + 48)
return res
def balance(self) :
length = len(self.arr)
for i in reversed(xrange(1,length,1)) :
if self.arr[i] == 0:
self.a.pop()
else:
break
def __add__(self, other) : #operator +
length1 = len(self.arr)
length2 = len(other.arr)
if length1 >= length2 :
length1_alt = length1
length2_alt = length2
self_alt = self
other_alt = other
else :
length1_alt = length2
length2_alt = length1
self_alt = other
other_alt = self
r = 0;
for i in xrange(0,length1_alt,1) :
if i < length2_alt :
res = (self_alt.arr[i] + other_alt.arr[i] + r) % 10
r = (self_alt.arr[i] + other_alt.arr[i]) / 10
self_alt.arr[i] = res
else:
res = (self_alt.arr[i] + r) % 10
r = (self_alt.arr[i] + r) / 10
self_alt.arr[i] = res
if r > 0 :
self_alt.arr.append(r)
return self_alt
def __ge__(self, other) : #operator >
length1 = len(self.arr)
length2 = len(other.arr)
if length1 > length2 :
return True
elif length1 == length2 :
for i in reversed(xrange(0,length1,1)):
if self.arr[i] < other.arr[i]:
return False
elif self.arr[i] > other.arr[i]:
return True
else :
return False
return True
def __sub__(self, other) : #operator -
length1 = len(self.arr)
length2 = len(other.arr)
if self >= other :
length1_alt = length1
length2_alt = length2
self_alt = self
other_alt = other
else :
length1_alt = length2
length2_alt = length1
other_alt = self
self_alt = other
self_alt.minus = 1
for i in xrange(0,length1_alt,1):
if i < length2_alt:
res = self_alt.arr[i] - other_alt.arr[i]
else:
res = self_alt.arr[i]
if res < 0:
if (i + 1) < length1_alt:
res += 10
self_alt.arr[i + 1] -= 1
else:
self_alt.minus = 1
self_alt.arr[i] = res
self_alt.balance()
return self_alt
a = longint('11110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001')
b = longint('222200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002')
s = a + b
print a.out(), "+", b.out(), "=", s.out()
print
s = s + s
print s.out()
print
s -= a
print s.out()