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
#include <bits/stdc++.h>
using namespace std;

struct cmplx
{
double r, i;
cmplx operator+(cmplx b) { return {r + b.r, i + b.i}; }
cmplx operator-(cmplx b) { return {r - b.r, i - b.i}; }
cmplx operator*(cmplx b) { return {r * b.r - i * b.i, r * b.i + i * b.r}; }
};
struct bigint
{
private:
string s;
int sign;

public:
bigint() : sign(1){};
bigint(long long v) : s(to_string(v)), sign(1)
{
reverse(s.begin(), s.end());
if (s.back() == '-')
sign = -1, s.pop_back();
for (auto &i : s)
i -= '0';
}
bigint abs() const
{
bigint ans = *this;
ans.sign = 1;
return ans;
}

friend ostream &operator<<(ostream &o, bigint a)
{
if (a.sign == -1)
o << '-';
for (int i = (int)size(a.s) - 1; i >= 0; i--)
o << char(a.s[i] + '0');
return o;
}
friend istream &operator>>(istream &i, bigint &a)
{
i >> a.s;
reverse(a.s.begin(), a.s.end());
if (a.s.back() == '-')
a.sign = -1, a.s.pop_back();
for (auto &i : a.s)
{
assert(isdigit(i));
i -= '0';
}
while (a.s.back() == 0 && size(a.s) > 1)
{
a.s.pop_back();
}
return i;
}

friend bool operator==(const bigint &a, const bigint &b) { return a.sign == b.sign && a.s == b.s; }
friend bool operator!=(const bigint &a, const bigint &b) { return !(a == b); }
friend bool operator<(const bigint &a, const bigint &b)
{
if (a.sign < 0 && b.sign > 0)
return true;
if (a.sign > 0 && b.sign < 0)
return false;
if (size(a.s) != size(b.s))
return (size(a.s) < size(b.s)) && (a.sign == 1);
for (int i = (int)size(a.s) - 1; i >= 0; i--)
{
if (a.s[i] == b.s[i])
continue;
return (a.s[i] < b.s[i]) && (a.sign == 1);
}
return false;
}
friend bool operator>(const bigint &a, const bigint &b) { return b < a; }

bigint operator+() const { return *this; }
bigint operator-() const
{
bigint a = *this;
a.sign *= -1;
return a;
}

bigint &operator+=(const bigint &b)
{
size_t la = size(s), lb = size(b.s);
if (sign == b.sign)
{
s.resize(max(la, lb) + 1);
for (size_t i = 0; i < lb; i++)
{
s[i] += b.s[i];
}
for (size_t i = 0; i < size(s) - 1; i++)
{
s[i + 1] += char(s[i] / 10);
s[i] %= 10;
}
if (s.back() == 0)
s.pop_back();
return *this;
}
else
{
if (abs() == b.abs())
{
*this = bigint(0);
return *this;
}
if (abs() > b.abs())
{
for (size_t i = 0; i < lb; i++)
{
s[i] -= b.s[i];
if (s[i] < 0)
s[i] += 10, s[i + 1]--;
}
for (size_t i = lb; i < la - 1; i++)
{
if (s[i] < 0)
s[i] += 10, s[i + 1]--;
}
while (s.back() == 0 && size(s) > 1)
s.pop_back();
return *this;
}
else
{
sign *= -1;
s.resize(lb);
for (size_t i = 0; i < la; i++)
{
s[i] = b.s[i] - s[i];
if (s[i] < 0)
{
s[i] += 10;
s[i + 1]++;
if (i + 1 == la)
la++;
}
}
for (size_t i = la; i < lb; i++)
s[i] = b.s[i];
while (s.back() == 0 && size(s) > 1)
s.pop_back();
return *this;
}
}
}
bigint &operator-=(const bigint &b) { return *this += -b; }
bigint &operator*=(const bigint &b)
{
auto fft = [](vector<cmplx> &p, int g = 1) -> void
{
int n = (int)size(p), lg = __lg(n) - 1;
if (n == 1)
return;
const double pi = acos(-1);
vector<int> r(n);
for (int i = 0; i < n; i++)
{
r[i] = r[i >> 1] >> 1 | ((i & 1) << lg);
if (i < r[i])
swap(p[i], p[r[i]]);
}
for (int wid = 2; wid <= n; wid <<= 1)
{
cmplx w = {cos(2 * pi / wid * g), sin(2 * pi / wid * g)};
for (int l = 0; l < n; l += wid)
{
cmplx wi = {1, 0};
for (int i = l; i < l + wid / 2; i++, wi = w * wi)
{
cmplx x = p[i], y = p[i + wid / 2];
p[i] = x + wi * y;
p[i + wid / 2] = x - wi * y;
}
}
}
};
int n = int(size(s) + size(b.s)), lg = 0;
while (1 << lg < n)
{
lg++;
}
n = 1 << lg;
vector<cmplx> p(n);
for (int i = 0; i < n; i++)
{
p[i] = {double(i < int(size(s)) ? s[i] : 0), double(i < int(size(b.s)) ? b.s[i] : 0)};
}
fft(p);
for (int i = 0; i < n; i++)
{
p[i] = p[i] * p[i];
}
fft(p, -1);
s.resize(n);
int plus = 0;
for (int i = 0; i < n; i++)
{
int tmp = (int)round(p[i].i / (n << 1)) + plus;
s[i] = char(tmp % 10);
plus = tmp / 10;
}
while (s.back() == 0 && size(s) > 1)
s.pop_back();
sign *= b.sign;
if (s.back() == 0)
sign = 1;
return *this;
}

friend bigint operator+(const bigint &a, const bigint &b) { return bigint(a) += b; }
friend bigint operator-(const bigint &a, const bigint &b) { return bigint(a) -= b; }
friend bigint operator*(const bigint &a, const bigint &b) { return bigint(a) *= b; }
};
signed main()
{ bigint a,b;

return 0;
}