This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BigTypes.cpp
189 lines (174 loc) · 4.63 KB
/
BigTypes.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include"BigTypes.h"
#include<climits>
#include<stack>
#include<utility>
#include<sstream>
template<typename type>
void swap(type &a, type &b) {
a = a ^ b; b = a ^ b; a = a ^ b;
}
void reverse(std::string &a) {
for (int i = 0; i < a.size() / 2; i++)
swap(a[i], a[a.size() - 1 - i]);
}
inline short performCarry(long long num, short &carry, short radix) {
carry = num / radix;
num %= radix;
return num;
}
BigInteger operator+(BigInteger & a, BigInteger & b) {
if (a.ST_BASE != b.ST_BASE)b.toggleBase(a.ST_BASE);
BigInteger ret;
BigInteger *less = &a, *more = &b;
if (a.data.size() > b.data.size())less = &b, more = &a;
short carry = 0;
for (int i = 0; i < less->data.size(); i++) {
long temp = less->data[i] + more->data[i] + carry;
temp = performCarry(temp, carry, a.ST_BASE);
ret.data.push_back(temp);
}
for (int i = less->data.size(); i < more->data.size(); i++) {
long temp = more->data[i] + carry;
temp = performCarry(temp, carry, a.ST_BASE);
ret.data.push_back(temp);
}
if (carry)ret.data.push_back(carry);
ret.tidy();
return ret;
}
BigInteger operator-(BigInteger & a, BigInteger & b) {
if (a.ST_BASE != b.ST_BASE)b.toggleBase(a.ST_BASE);
BigInteger ret;
BigInteger *less = &a, *more = &b;
if (a.data.size() > b.data.size())less = &b, more = &a;
short carry = 0;
for (int i = 0; i < less->data.size(); i++) {
long temp = less->data[i] - more->data[i] + carry;
temp = performCarry(temp, carry, a.ST_BASE);
ret.data.push_back(temp);
}
for (int i = less->data.size(); i < more->data.size(); i++) {
long temp = more->data[i] + carry;
temp = performCarry(temp, carry, a.ST_BASE);
ret.data.push_back(temp);
}
if (carry)ret.data.push_back(carry);
ret.tidy();
return ret;
}
BigInteger operator*(BigInteger & a, long b) {
BigInteger ret;
short carry = 0;
for (int i = 0; i < a.data.size(); i++) {
long long temp = b * a.data[i] + carry;
temp = performCarry(temp, carry, a.ST_BASE);
ret.data.push_back(temp);
}
if (carry)ret.data.push_back(carry);
ret.tidy();
return ret;
}
static std::pair<BigInteger, long>div_mod(BigInteger &a, long b) {
long long temp = 0; BigInteger ret;
ret.ST_BASE = a.ST_BASE;
std::stack<short>rts;
for (int i = a.data.size() - 1; i >= 0; i--) {
temp *= a.ST_BASE; temp += a.data[i];
rts.push(temp / b);
temp %= b;
}
while (!rts.empty()) {
ret.data.push_back(rts.top());
rts.pop();
}
ret.tidy();
return std::pair<BigInteger, long>(ret, temp);
}
BigInteger operator/(BigInteger & a, long b) {
return div_mod(a, b).first;
}
long operator%(BigInteger & a, long b) {
return div_mod(a, b).second;
}
//reversed
std::vector<short> BigInteger::toBase(short base) {
BigInteger temp = *this; std::vector<short>ret;
std::pair<BigInteger, long>rts;
while (temp.data.size()) {
rts = div_mod(temp, base);
ret.push_back(rts.second);
temp = rts.first;
}
return ret;
}
void BigInteger::fromBase(std::vector<short>& num/*reversed*/, short ORIG_BASE) {
//.....is there any method that is not as brutal as THIS
ST_BASE = SHRT_MAX;
BigInteger temp; temp.data = num;
temp.ST_BASE = ORIG_BASE;
data = temp.toBase(ST_BASE);
}
void BigInteger::toggleBase(short base) {
data = toBase(base);
}
BigInteger::BigInteger() {
ST_BASE = SHRT_MAX;
}
BigInteger::BigInteger(std::string num, int radix) {
ST_BASE = SHRT_MAX;
std::vector<short>temp;
reverse(num);
for (int i = 0; i < num.size(); i++) {
if (num[i] >= '0'&&num[i] <= '9')
temp.push_back(num[i] - '0');
else if (num[i] >= 'A'&&num[i] <= 'Z')
temp.push_back(num[i] - 'A' + 10);
else if (num[i] >= 'a'&&num[i] <= 'z')
temp.push_back(num[i] - 'a' + 10);
}
fromBase(temp, radix);
}
BigInteger::BigInteger(long num) {
ST_BASE = SHRT_MAX;
while (num != 0) {
this->data.push_back(num%ST_BASE);
num /= ST_BASE;
}
}
BigInteger::~BigInteger() {
data.clear();
}
void BigInteger::tidy() {
while (data.size() && data[data.size() - 1] == 0)
data.pop_back();
if (data.size()>1) {
int symbol = data[data.size() - 1] >= 0 ? 1 : -1;
for (int i = data.size() - 2; i >= 0; i--) {
while (data[i] * symbol < 0) {
data[i + 1] --;
data[i] += symbol * ST_BASE;
}
}
}
}
std::string BigInteger::toString(short radix) {
if (data.size() == 0)return "0";
std::vector<short>temp = toBase(radix);
std::string ret; int symbol = temp[temp.size() - 1] < 0 ? -1 : 1;
if (symbol == -1)ret += "-";
for (int i = temp.size() - 1; i >= 0; i--) {
if (symbol*temp[i] < 10)ret += (symbol*temp[i] + '0');
else if (symbol*temp[i] < 36)ret += (symbol*temp[i] + 'A' - 10);
else {
ret = "radix limit exceeded";
return ret;
}
}
return ret;
}
int BigInteger::toInt() {
int ret;
std::istringstream sin(toString());
sin >> ret;
return ret;
}