0%

高精度板子

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
#include<bits/stdc++.h>
using namespace std;
#define int long long
int aqx=1,byx=1,inf=0x3f3f3f3f3f3f3f3f;
const int mod=998244353,G=3;
const int N=1e6+5;
inline void add(int &a,int b){a=(a+b)%mod;}
inline void mul(int &a,int b){a=(a*b)%mod;}
inline int ksm(int a,int b){
int ret=1;
for(;b;b>>=1,a=a*a%mod)
if(b&1)ret=ret*a%mod;
return ret;
}
inline int inv(int x){return ksm(x,mod-2);}
struct Int{
bool neg;vector<int>a;
Int(){neg=0;}
int size()const{return a.size();}
void clear(){neg=0;vector<int>b;swap(a,b);}
Int(const int &x){
clear();
int y=x;
if(y<0)neg=1,y=-y;
while(y)a.push_back(y%10),y/=10;
}
void expand(int len){if(size()<len)a.resize(len);}
void simplify(){while(size()&&!a.back())a.pop_back();}

void read(string s){
clear();
for(char ch:s){
neg^=(ch=='-');
if(isdigit(ch))a.push_back(ch-'0');
}
reverse(a.begin(),a.end());
simplify();
}
friend void read(Int &x){string s;cin>>s;x.read(s);}
void write(const char ch=0)const{
if(!size()){cout<<'0'<<ch;return;}
if(neg)cout<<'-';
for(int i=size()-1;~i;i--)cout<<a[i];
cout<<ch;
}
friend void write(const Int &x,const char ch=0){x.write(ch);}

friend bool _equal(const Int &x,const Int &y){
if(x.size()!=y.size())return 0;
for(int i=0;i<x.size();i++)if(x.a[i]!=y.a[i])return 0;
return 1;
}
friend bool _less(const Int &x,const Int &y){
if(x.size()<y.size())return 1;
if(x.size()>y.size())return 0;
for(int i=x.size()-1;~i;i--){
if(x.a[i]<y.a[i])return 1;
if(x.a[i]>y.a[i])return 0;
}
return 0;
}
friend bool _greater(const Int &x,const Int &y){
if(x.size()<y.size())return 0;
if(x.size()>y.size())return 1;
for(int i=x.size()-1;~i;i--){
if(x.a[i]<y.a[i])return 0;
if(x.a[i]>y.a[i])return 1;
}
return 0;
}
friend bool operator==(const Int &x,const Int &y){
if(x.neg!=y.neg)return 0;
return _equal(x,y);
}
friend bool operator!=(const Int &x,const Int &y){return !(x==y);}
friend bool operator<(const Int &x,const Int &y){
if(x.neg&&!y.neg)return 1;
if(!x.neg&&y.neg)return 0;
if(!x.neg)return _less(x,y);
if(x.neg)return _greater(x,y);
return 0;
}
friend bool operator>(const Int &x,const Int &y){
if(x.neg&&!y.neg)return 0;
if(!x.neg&&y.neg)return 1;
if(!x.neg)return _greater(x,y);
if(x.neg)return _less(x,y);
return 0;
}
friend bool operator<=(const Int &x,const Int &y){return x<y||x==y;}
friend bool operator>=(const Int &x,const Int &y){return x>y||x==y;}

friend Int abs(const Int &x){Int z=x;z.neg=0;return z;}

friend Int _plus(const Int &x,const Int &y){
Int z;
int w=0;vector<int>b(max(x.size(),y.size())+1);
int carry=0;
for(;w<x.size()||w<y.size()||carry;w++){
int t1=(w<x.size()?x.a[w]:0),t2=(w<y.size()?y.a[w]:0),t3=carry;
b[w]=(t1+t2+t3)%10,carry=(t1+t2+t3)/10;
}
z.a=b,z.simplify();
return z;
}
friend Int _minus(const Int &x,const Int &y){
Int z;
int w=0;vector<int>b(max(x.size(),y.size()));
int carry=0;
for(;w<x.size()||w<y.size()||carry;w++){
int t1=(w<x.size()?x.a[w]:0),t2=(w<y.size()?y.a[w]:0),t3=carry;
if(t1-t2+t3<0)t3+=10,carry=-1;
else carry=0;
b[w]=(t1-t2+t3)%10;
}
z.a=b,z.simplify();
return z;
}
void NTT(int *e,int len,int g)const{
int rev[len];memset(rev,0,sizeof(rev));
for(int i=0;i<len;i++)rev[i]=((rev[i>>1]>>1)|((i&1)*(len>>1)));
for(int i=0;i<len;i++)if(i<rev[i])swap(e[i],e[rev[i]]);
for(int i=1;i<len;i<<=1){
int wn=ksm(g,(mod-1)/(i<<1));
for(int j=0;j<len;j+=(i<<1))
for(int k=0,wk=1;k<i;k++,wk=wk*wn%mod){
int x=e[j+k],y=wk*e[j+k+i];
e[j+k]=(x+y)%mod,e[j+k+i]=(x-y)%mod;
}
}
for(int i=0;i<len;i++)add(e[i],mod);
}
friend Int _multiply(const Int &x,const Int &y){
Int z;z.expand(x.size()+y.size()-1);
if(min(x.size(),y.size())<=16){
for(int i=0;i<x.size();i++)
for(int j=0;j<y.size();j++)
add(z.a[i+j],x.a[i]*y.a[j]);
for(int i=0,carry=0;i<z.size()||carry;i++){
if(i>=z.size())z.a.push_back(0);
int bb=(z.a[i]+carry)%10;
carry=(z.a[i]+carry)/10,z.a[i]=bb;
}
z.simplify();
return z;
}
int len=1;while(len<x.size()+y.size()-1)len<<=1;
int ex[len],ey[len];memset(ex,0,sizeof(ex)),memset(ey,0,sizeof(ey));
for(int i=0;i<x.size();i++)ex[i]=x.a[i];
for(int i=0;i<y.size();i++)ey[i]=y.a[i];
z.NTT(ex,len,G),z.NTT(ey,len,G);
for(int i=0;i<len;i++)mul(ex[i],ey[i]);
z.NTT(ex,len,inv(G));
int ivlen=inv(len);
for(int i=0;i<len;i++)mul(ex[i],ivlen);
for(int i=0;i<z.size();i++)z.a[i]=ex[i];
for(int i=0,carry=0;i<len||carry;i++){
if(i>=z.size())z.a.push_back(0);
int bb=(z.a[i]+carry)%10;
carry=(z.a[i]+carry)/10,z.a[i]=bb;
}
z.simplify();
return z;
}
friend Int _divide(const Int &x,const Int &y){
Int z;
z.expand(x.size());
Int t;
for(int i=x.size()-1;~i;i--){
t.simplify(),t.a.insert(t.a.begin(),x.a[i]);
while(_less(y,t)||_equal(y,t))z.a[i]++,t=_minus(t,y);
}
z.simplify();
return z;
}
Int &operator=(const int &x){return (*this)=(Int)x;}
friend Int operator+(const Int &x,const Int &y){
Int z;
if(x.neg==y.neg)z=_plus(x,y),z.neg=x.neg;
if(!x.neg&&y.neg){
if(_less(x,y))z=_minus(y,x),z.neg=1;
else z=_minus(x,y);
}
if(x.neg&&!y.neg){
if(_less(y,x))z=_minus(x,y),z.neg=1;
else z=_minus(y,x);
}
return z;
}
Int &operator+=(const Int &x){return (*this)=(*this)+x;}
friend Int operator-(const Int &x,const Int &y){
Int z;
if(x.neg!=y.neg)z=_plus(x,y),z.neg=x.neg;
if(!x.neg&&!y.neg){
if(_less(x,y))z=_minus(y,x),z.neg=1;
else z=_minus(x,y);
}
if(x.neg&&y.neg){
if(_less(x,y))z=_minus(y,x);
else z=_minus(x,y),z.neg=1;
}
return z;
}
Int &operator-=(const Int &x){return (*this)=(*this)-x;}
friend Int operator*(const Int &x,const Int &y){
Int z;
z=_multiply(x,y);
z.neg=x.neg^y.neg;
return z;
}
Int &operator*=(const Int &x){return (*this)=(*this)*x;}
friend Int operator/(const Int &x,const Int &y){
Int z;
z=_divide(x,y);
z.neg=x.neg^y.neg;
return z;
}
Int &operator/=(const Int &x){return (*this)=(*this)/x;}
friend Int operator%(const Int &x,const Int &y){return x-(x/y)*y;}
Int &operator%=(const Int &x){return (*this)=(*this)%x;}
};
void solve(){
Int x,y;
read(x),read(y);
write(x+y,'\n');
write(x-y,'\n');
write(x*y,'\n');
write(x/y,'\n');
write(x%y,'\n');
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0);
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
for(;byx<=aqx;byx++)solve();
return 0;
}
------ Q.E.D.~~~喵~~~ ------