-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitInt.code-snippets
223 lines (223 loc) · 6.66 KB
/
BitInt.code-snippets
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
{
"Big Int Structure": {
"prefix": "BigInt",
"body": [
"struct BigInt {",
"",
" const int BASE = 1000000000;",
" vector < int > v;",
" ",
" BigInt() {}",
"",
" BigInt(const long long &val) {",
" *this = val;",
" }",
" ",
" BigInt(const string &val) {",
" *this = val;",
" ",
" }",
" ",
" int size() const { return v.size(); }",
" ",
" bool zero() const { return v.empty(); }",
" ",
" BigInt& operator = (long long val) {",
" v.clear();",
" while (val) {",
" v.push_back(val % BASE);",
" val /= BASE;",
" }",
" return *this;",
" }",
"",
" BigInt& operator = (const BigInt &a) {",
" v = a.v;",
" return *this;",
" }",
"",
" BigInt& operator = (const vector < int > &a) {",
" v = a;",
" return *this;",
" }",
"",
" BigInt& operator = (const string &s) {",
" *this = 0;",
" for (const char &ch : s)",
" *this = *this * 10 + (ch - '0');",
" return *this;",
" }",
" ",
" bool operator < (const BigInt &a) const {",
" if (a.size() != size())",
" return size() < a.size();",
" for (int i = size() - 1; i >= 0; i--)",
" if (v[i] != a.v[i])",
" return v[i] < a.v[i];",
" return false;",
" }",
"",
" bool operator > (const BigInt &a) const {",
" return a < *this;",
" }",
"",
" bool operator == (const BigInt &a) const {",
" return (!(*this < a) && !(a < *this));",
" }",
"",
" bool operator <= (const BigInt &a) const {",
" return ((*this < a) || !(a < *this));",
" }",
" ",
" ll val(){",
" ll ans = 0;",
" for (int i = 0; i < size(); i++)",
" ans = ans * 10 + v[i];",
" return ans;",
" }",
"",
" BigInt operator + (const BigInt &a) const {",
" BigInt res = *this;",
" int idx = 0, carry = 0;",
" while (idx < a.size() || carry) {",
" if (idx < a.size())",
" carry += a.v[idx];",
" if (idx == res.size())",
" res.v.push_back(0);",
" res.v[idx] += carry;",
" carry = res.v[idx] / BASE;",
" res.v[idx] %= BASE;",
" idx++;",
" }",
" return res;",
" }",
" ",
" BigInt& operator += (const BigInt &a) {",
" *this = *this + a;",
" return *this;",
" }",
" ",
" BigInt operator * (const BigInt &a) const {",
" BigInt res;",
" if (this -> zero() || a.zero())",
" return res;",
" res.v.resize(size() + a.size());",
" for (int i = 0; i < size(); i++) {",
" if (v[i] == 0)",
" continue;",
" long long carry = 0;",
" for (int j = 0; carry || j < a.size(); j++) {",
" carry += 1LL * v[i] * (j < a.size() ? a.v[j] : 0);",
" while (i + j >= res.size())",
" res.v.push_back(0);",
" carry += res.v[i + j];",
" res.v[i + j] = carry % BASE;",
" carry /= BASE;",
" }",
" }",
" while (!res.v.empty() && res.v.back() == 0)",
" res.v.pop_back();",
" return res;",
" }",
"",
" BigInt& operator *= (const BigInt &a) {",
" *this = *this * a;",
" return *this;",
" }",
"",
" BigInt& operator -= (const BigInt &b){",
" if(*this < b)",
" throw(\"UNDERFLOW\");",
" int n = this -> size(), m = b.size();",
" int i, t = 0, s;",
" for (i = 0; i < n;i++){",
" if(i < m)",
" s = this -> v[i] - b.v[i]+ t;",
" else",
" s = this -> v[i] + t;",
" if(s < 0)",
" s += 10,",
" t = -1;",
" else",
" t = 0;",
" this -> v[i] = s;",
" }",
" while(n > 1 && this -> v[n - 1] == 0)",
" this -> v.pop_back(),",
" n--;",
" return *this;",
" }",
"",
" BigInt operator - (const BigInt&b){",
" BigInt a = *this;",
" a -= b;",
" return a;",
" }",
"",
" BigInt operator -- (const int){",
" *this -= BigInt(1);",
" return *this;",
" }",
"",
" BigInt operator ++ (const int){",
" *this += BigInt(1);",
" return *this;",
" }",
"",
" BigInt& operator /=(const ll a) {",
" ll carry = 0;",
" for (int i = (int) v.size() - 1; i >= 0; i--) {",
" ll cur = v[i] + carry * BASE;",
" v[i] = cur / a;",
" carry = cur % a;",
" }",
" while (!v.empty() && v.back() == 0)",
" v.pop_back();",
" return *this;",
" }",
" ",
" BigInt operator / (const ll a) {",
" ll carry = 0;",
" vector < int > res = this -> v;",
" for (int i = (int) res.size() - 1; i >= 0; i--) {",
" ll cur = res[i] + carry * BASE;",
" res[i] = cur / a;",
" carry = cur % a;",
" }",
" BigInt ans;",
" ans = res;",
" return ans;",
" }",
" ",
" BigInt operator % (const ll a){",
" ll res = 0;",
" for (int i = (int) v.size() - 1; i >= 0; i--)",
" res = (res * 10 + v[i]) % a;",
" BigInt ans = res;",
" return ans;",
" }",
"",
" BigInt& operator %= (const ll a) {",
" *this = *this % a;",
" return *this;",
" }",
"",
" friend ostream& operator<<(ostream &out, const BigInt &a) {",
" out << (a.zero() ? 0 : a.v.back());",
" for (int i = (int) a.v.size() - 2; i >= 0; i--)",
" out << setfill('0') << setw(9) << a.v[i];",
" return out;",
" }",
"",
" friend istream& operator>>(istream &in, BigInt &a) {",
" string s;",
" in >> s;",
" a = s;",
" return in;",
" }",
"",
"};"
],
"description": "Big Int Structure"
}
}