-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary_strll.cpp
More file actions
258 lines (255 loc) · 7.93 KB
/
Library_strll.cpp
File metadata and controls
258 lines (255 loc) · 7.93 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "auto_util_header.hpp"
namespace strll_op {
class strll {
private:
string val; // interior process is always reversed
inline string ll_to_strll(ll x) {
if (x == 0) return "0";
bool neg_flag = false;
if (x < 0) { neg_flag = true; x *= -1; }
string ret = "";
while (x > 0) {
ret += '0' + (x % 10);
x /= 10;
}
if (neg_flag) ret += '-';
return ret;
}
inline string uadd_core(const string &s, const string &t) {
int n = s.length();
int m = t.length();
string ret = "";
int v_digits = std::max(n, m) + 1;
vi v(v_digits, 0);
Loop(i, v_digits - 1) {
if (i < n) v[i] += s[i] - '0';
if (i < m) v[i] += t[i] - '0';
if (v[i] >= 10) {
v[i] -= 10;
v[i + 1] += 1;
}
}
if (v[v_digits - 1] == 0) v_digits = std::max(1, v_digits - 1);
Loop(i, v_digits) ret += '0' + v[i];
return ret;
}
inline string usub_core(const string &s, const string &t) {
int n = s.length();
int m = t.length();
string ret = "";
int v_digits = 1;
vi v(n, 0);
Loop(i, n) {
v[i] += s[i] - '0';
if (i < m) v[i] -= (t[i] - '0');
if (v[i] < 0) {
v[i] += 10;
v[i + 1] -= 1;
}
if (v[i] > 0) v_digits = i + 1;
}
Loop(i, v_digits) ret += '0' + v[i];
return ret;
}
inline string umul_core(const string &s, const string &t) {
int n = s.length();
int m = t.length();
string ret = "";
vi v(n + m, 0);
Loop(i, n) {
Loop(j, m) {
int z = (s[i] - '0') * (t[j] - '0');
v[i + j] += z % 10;
v[i + j + 1] += z / 10;
}
}
int v_digits = 1;
Loop(i, n + m - 1) {
v[i + 1] += v[i] / 10;
v[i] %= 10;
if (v[i + 1] > 0) v_digits = i + 2;
}
Loop(i, v_digits) ret += '0' + v[i];
return ret;
}
inline bool uge_core(const string &s, const string &t) const {
int n = s.length();
int m = t.length();
while (n > 1 && s[n - 1] == '0') n--;
while (m > 1 && t[m - 1] == '0') m--;
if (n > m) return true;
else if (n < m) return false;
else {
Loopr(i, n) {
if (s[i] > t[i]) return true;
if (s[i] < t[i]) return false;
}
return true;
}
}
inline string udiv_core(string s, const string &t, bool rem_flag) {
int n = s.length();
int m = t.length();
string ret = "0";
Loopr(i, n - m + 1) {
ret += '0';
string sbuf = s.substr(i, m + 1);
while (uge_core(sbuf, t)) {
sbuf = usub_core(sbuf, t);
ret.back()++;
}
Loop(j, std::min(m + 1, n)) {
s[i + j] = j < int(sbuf.size()) ? sbuf[j] : '0';
}
}
reverse(ret.begin(), ret.end());
if (rem_flag) ret = s;
while (ret.size() > 1 && ret.back() == '0') ret.pop_back();
return ret;
}
inline string add(const string &s, const string &t) {
int n = int(s.length());
int m = int(t.length());
string ret = "";
int mode = (s[n - 1] == '-' ? 0b10 : 0) + (t[m - 1] == '-' ? 0b01 : 0);
switch (mode) {
case 0b00:
ret = uadd_core(s, t);
break;
case 0b01:
if (uge_core(s.substr(0, n), t.substr(0, m - 1))) ret = usub_core(s, t.substr(0, m - 1));
else ret = usub_core(t.substr(0, m - 1), s) + '-';
break;
case 0b10:
if (uge_core(s.substr(0, n - 1), t.substr(0, m))) ret = usub_core(s.substr(0, n - 1), t) + '-';
else ret = usub_core(t, s.substr(0, n - 1));
break;
case 0b11:
ret = uadd_core(s.substr(0, n - 1), t.substr(0, m - 1)) + '-';
break;
}
if (ret == "0-") ret.pop_back();
return ret;
}
inline string sub(const string &s, const string &t) {
string ret = "";
int n = s.length();
int m = t.length();
int mode = (s[n - 1] == '-' ? 0b10 : 0) + (t[m - 1] == '-' ? 0b01 : 0);
switch (mode) {
case 0b00:
if (uge_core(s.substr(0, n), t.substr(0, m))) ret = usub_core(s, t);
else ret = usub_core(t, s) + '-';
break;
case 0b01:
ret = uadd_core(s, t.substr(0, m - 1));
break;
case 0b10:
ret = uadd_core(s.substr(0, n - 1), t) + '-';
break;
case 0b11:
if (uge_core(s.substr(0, n - 1), t.substr(0, m - 1))) ret = usub_core(s.substr(0, n - 1), t.substr(0, m - 1)) + '-';
else ret = usub_core(t.substr(0, m - 1), s.substr(0, n - 1));
break;
}
if (ret == "0-") ret.pop_back();
return ret;
}
inline string mul(const string &s, const string &t) {
string ret;
int n = s.length();
int m = t.length();
int mode = (s[n - 1] == '-' ? 0b10 : 0) + (t[m - 1] == '-' ? 0b01 : 0);
switch (mode) {
case 0b00:
ret = umul_core(s, t);
break;
case 0b01:
ret = umul_core(s, t.substr(0, m - 1)) + '-';
break;
case 0b10:
ret = umul_core(s.substr(0, n - 1), t) + '-';
break;
case 0b11:
ret = umul_core(s.substr(0, n - 1), t.substr(0, m - 1));
break;
}
if (ret == "0-") ret.pop_back();
return ret;
}
inline bool ge(const string &s, const string &t) const {
bool ret;
int n = s.length();
int m = t.length();
int mode = (s[n - 1] == '-' ? 0b10 : 0) + (t[m - 1] == '-' ? 0b01 : 0);
switch (mode) {
case 0b00:
ret = uge_core(s, t);
break;
case 0b01:
ret = true;
break;
case 0b10:
ret = false;
break;
case 0b11:
if (s == t) ret = true;
else ret = !uge_core(s.substr(0, n - 1), t.substr(0, m - 1));
break;
}
return ret;
}
inline string div(const string &s, const string &t, bool rem_flag) {
string ret;
int n = s.length();
int m = t.length();
int mode = (s[n - 1] == '-' ? 0b10 : 0) + (t[m - 1] == '-' ? 0b01 : 0);
switch (mode) {
case 0b00:
ret = udiv_core(s, t, rem_flag);
break;
case 0b01:
ret = udiv_core(s, t.substr(0, m - 1), rem_flag);
if (!rem_flag) ret += '-';
break;
case 0b10:
ret = udiv_core(s.substr(0, n - 1), t, rem_flag) + '-';
break;
case 0b11:
ret = udiv_core(s.substr(0, n - 1), t.substr(0, m - 1), rem_flag);
if (rem_flag) ret += '-';
break;
}
if (ret == "0-") ret.pop_back();
return ret;
}
public:
strll(string init = "0") { reverse(init.begin(), init.end()); val = init; return; }
strll(ll init) { val = ll_to_strll(init); return; }
strll(const strll& another) { val = another.val; return; }
inline strll& operator=(const strll &another) { val = another.val; return *this; }
inline strll operator+(const strll &x) { strll ret; ret.val = add(val, x.val); return ret; }
inline strll operator-(const strll &x) { strll ret; ret.val = sub(val, x.val); return ret; }
inline strll operator*(const strll &x) { strll ret; ret.val = mul(val, x.val); return ret; }
inline strll operator/(const strll &x) { strll ret; ret.val = div(val, x.val, false); return ret; }
inline strll operator%(const strll &x) { strll ret; ret.val = div(val, x.val, true); return ret; }
inline strll& operator+=(const strll &x) { val = add(val, x.val); return *this; }
inline strll& operator-=(const strll &x) { val = sub(val, x.val); return *this; }
inline strll& operator*=(const strll &x) { val = mul(val, x.val); return *this; }
inline strll& operator/=(const strll &x) { val = div(val, x.val, false); return *this; }
inline strll& operator%=(const strll &x) { val = div(val, x.val, true); return *this; }
inline bool operator>=(const strll &x) { return ge(val, x.val); }
inline bool operator>(const strll &x) { return ge(val, x.val) && val != x.val; }
inline bool operator<=(const strll &x) { return ge(x.val, val); }
inline bool operator<(const strll &x) { return ge(x.val, val) && val != x.val; }
inline bool operator==(const strll &x) { return val == x.val; }
inline bool operator!=(const strll &x) { return val != x.val; }
inline bool operator<(const strll &x) const { return !ge(val, x.val); }
friend inline std::istream& operator >> (std::istream &is, strll& x) { is >> x.val; reverse(x.val.begin(), x.val.end()); return is; }
friend inline std::ostream& operator << (std::ostream &os, const strll& x) { os << x.get_val(); return os; }
string get_val() const { string ret = val; reverse(ret.begin(), ret.end()); return ret; }
};
}
using namespace strll_op;
typedef vector<strll> vstrll;
typedef vector<vector<strll>> vvstrll;