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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
| /*
high precision num
log:outbit is worth improving
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cctype>
using namespace std;
template<typename T>
T Max(const T& a,const T& b){return a>b?a:b;}
template<typename T>
T Min(const T& a,const T& b){return a<b?a:b;}
const int bit=10000;
const int bitl=4;
const int maxbit=10003;
typedef int lint;
struct num
{
lint ary[maxbit],len;
bool up0;
void set0()
{
up0=true;
len=1;
memset(ary,0,sizeof(ary));
}
bool is0()
{
return len==1&&ary[len]==0;
}
num()
{
up0=true;
len=1;
set0();
//memset(ary,0,sizeof(ary));
}
num(int w)
{
set0();
len=0;
if(w==0)return;
if(w<0)w=-w,up0=false;
while(w)
{
ary[++len]=w%bit;
w/=bit;
}
}
lint& operator[](const int& pos)
{
return ary[pos];
}
void outbit(int x)
{
int tmpbit=bit/10;
while(tmpbit>x)
tmpbit/=10,putchar('0');
while(tmpbit)
{
putchar(x/tmpbit+48);
x%=tmpbit;
tmpbit/=10;
}
//putchar('\n');
}
void input()
{
set0();
char s[maxbit],r;
int lens=0;
do
{r=getchar();}
while(r!='-'&&!isdigit(r));
if(r=='-')up0=false,r=getchar();
if(!isdigit(r))
{
up0=true;
return;
}
len=0;
while(isdigit(r))
{
s[++lens]=r;
r=getchar();
}
ungetc(r,stdin);
for(int i=lens;i>=1;--i)
{
int j=Max(1,i-bitl+1);
int tmp=0;
for(int k=j;k<=i;++k)
tmp=tmp*10+s[k]-48;
ary[++len]=tmp;
i=j;
}
while(len!=1&&!ary[len])
--len;
if(len==1&&ary[1]==0)
up0=true;
}
void output()
{
if(!up0)putchar('-');
printf("%d",ary[len]);
for(int i=len-1;i>=1;--i)
outbit(ary[i]);
}
};
inline num operator-(num a)
{
if(a.len==1&&a[1]==0)
return a;
a.up0=!a.up0;
return a;
}
inline bool operator==(num a,num b)
{
if(a.len!=b.len)return false;
if(a.up0!=b.up0)return false;
for(int i=1;i<=a.len;++i)
{
if(a[i]!=b[i])
return false;
}
return true;
inline bool operator!=(num a,num b)
{
return !(a==b);
}
inline bool operator>(num a,num b)
{
if(a.up0!=b.up0)return a.up0;
if(!a.up0)
{
a.up0=true;
b.up0=true;
return !(a>b||a==b);
}
if(a.len!=b.len)return a.len>b.len;
for(int i=a.len;i>=1;--i)
{
if(a[i]!=b[i])
return a[i]>b[i];
}
return false;
}
inline bool operator>=(num a,num b)
{
return a==b||a>b;
}
inline bool operator<(num a,num b)
{
return !(a>=b);
}
inline bool operator<=(num a,num b)
{
return a<b||a==b;
}
num add(num a,num b)
{
num ans;
ans.len=Max(a.len,b.len)+1;
for(int i=1;i<=ans.len;++i)
{
ans[i]+=a[i]+b[i];
if(ans[i]>=bit)
{
ans[i+1]+=ans[i]/bit;
ans[i]%=bit;
}
}
while(!ans[ans.len]&&ans.len!=1)--ans.len;
return ans;
}
num sub(num a,num b)
{
if(a<b)return -(sub(b,a));
num ans;
if(a==b)return ans;
ans.len=Max(a.len,b.len);
for(int i=1;i<=ans.len;++i)
{
if(a[i]<b[i])
{
--a[i+1];
a[i]+=bit;
}
ans[i]=a[i]-b[i];
}
while(ans.len!=1&&!ans[ans.len])
--ans.len;
return ans;
}
num mul(num a,num b)
{
num ans;
ans.len=a.len+b.len;
for(int i=1;i<=a.len;++i)
{
for(int j=1;j<=b.len;++j)
{
ans[i+j-1]+=a[i]*b[j];
if(ans[i+j-1]>=bit)
{
ans[i+j]+=ans[i+j-1]/bit;
ans[i+j-1]%=bit;
}
}
}
while(!ans[ans.len]&&ans.len!=1)--ans.len;
return ans;
}
/*
a>0,b>0
a+b=a+b
a+(-b)=a-b
(-a)+b=b-a
(-a)+(-b)=-(a+b)
a-b=a-b;
a-(-b)=a+b
(-a)-b=-(a+b)
(-a)-(-b)=b-a
*/
num operator+(num a,num b)
{
if(a.up0&&b.up0)
return add(a,b);
if(a.up0&&!b.up0)
return sub(a,-b);
if(b.up0&&!a.up0)
return sub(b,-a);
if((!b.up0)&&(!a.up0))
return -(add(-a,-b));
}
num operator-(num a,num b)
{
if(a.up0&&b.up0)
return sub(a,b);
if(a.up0&&!b.up0)
return add(a,-b);
if(b.up0&&!a.up0)
return -add(-a,b);
if((!b.up0)&&(!a.up0))
return sub(-b,-a);
}
/*
a*b=(-a)*(-b)=a*b
a*(-b)=(-a)*b=-(a*b)
*/
num operator*(num a,num b)
{
if(a.is0())return a;
if(b.is0())return b;
if(a.up0==b.up0)
{
if(a.up0)
return mul(a,b);
else return mul(-a,-b);
}
else
{
if(a.up0)return -mul(a,-b);
if(b.up0)return -mul(-a,b);
}
}
num op(num a,char r,num b)
{
if(r=='+')return a+b;
if(r=='-')return a-b;
}
int main()
{
num x,y,ans;
while(1)
{
x.input();
y.input();
cout<<"x:";
x.output();
cout<<endl;
cout<<"y:";
y.output();
cout<<endl;
ans=x*y;
cout<<"x*y=";ans.output();
cout<<endl;
}
return 0;
}
|