-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh_precision.h
More file actions
250 lines (250 loc) · 5.04 KB
/
Copy pathhigh_precision.h
File metadata and controls
250 lines (250 loc) · 5.04 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
#include <iostream>
#ifndef HIGH_PRECISION_H_
#define HIGH_PRECISION_H_
namespace std
{
struct High_precision
{
string str;
High_precision()
{
this->str="0";
}
High_precision(const string a)
{
this->str=a;
}
High_precision(const char * a)
{
this->str=a;
}
High_precision(const long long a)
{
string ans="";
long long aa=a;
while(aa>=1)
{
ans=(char)('0'+aa%10)+ans;
aa/=10;
}
this->str=ans;
}
operator string ()
{
return this->str;
}
operator long long ()
{
string str1=this->str;
long long ans=0;
for(int i=0;i<str1.length();i++)
ans=ans*10+(str1[i]-'0');
long long ret=ans;
return ret;
}
High_precision & operator = (const char * b)
{
this->str=b;
return *this;
}
High_precision & operator = (const string b)
{
this->str=b;
return *this;
}
High_precision & operator = (const long long a)
{
long long aa=a;
this->str="";
while(aa>=1)
{
this->str=(char)('0'+aa%10)+this->str;
aa/=10;
}
return *this;
}
};
ostream & operator << (ostream & os,const High_precision & a)
{
os<<a.str;
return os;
}
istream & operator >> (istream & is,High_precision & a)
{
is>>a.str;
return is;
}
bool operator == (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
return str1==str2;
}
bool operator < (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
if(str1.length()!=str2.length())
return str1.length()<str2.length();
else
return str1<str2;
}
bool operator > (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
if(str1.length()!=str2.length())
return str1.length()>str2.length();
else
return str1>str2;
}
bool operator <= (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
if(str1.length()!=str2.length())
return str1.length()<=str2.length();
else
return str1<=str2;
}
bool operator >= (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
if(str1.length()!=str2.length())
return str1.length()>=str2.length();
else
return str1>=str2;
}
const High_precision operator + (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
int len1=str1.length(),len2=str2.length();
string ans="";
if(len1<len2)
{
for(int i=1;i<=len2-len1;i++)
str1='0'+str1;
len1=len2;
}
else
{
for(int i=1;i<=len1-len2;i++)
str2='0'+str2;
len2=len1;
}
char j=0,temp;
for(int i=max(len1,len2)-1;i>=0;i--)
{
temp=(len1>i?str1[i]-'0':0)+(len2>i?str2[i]-'0':0)+j;
j=temp/10;
ans=(char)('0'+temp%10)+ans;
}
const High_precision ret=(High_precision){ans};
return ret;
}
const High_precision operator - (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
int len1=str1.length(),len2=str2.length();
string ans="";
char j=0,temp;
for(int i=max(len1,len2)-1;i>=0;i--)
{
temp=(len1>i?str1[i]-'0':0)-(len2>i?str2[i]-'0':0)-j;
if(temp<0)
{
j=(0-temp)/10+((0-temp)%10!=0);
temp+=j*10;
}
ans=(char)('0'+temp%10)+ans;
}
while(ans[0]=='0'&&ans.length()>=2)
ans.erase(ans.begin());
const High_precision ret=(High_precision){ans};
return ret;
}
const High_precision operator * (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
int len1=str1.length(),len2=str2.length();
string ans="";
char temp;
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
temp=(str1[i]-'0')*(str2[j]-'0');
if(i+j>=ans.length())
for(int k=ans.length();k<=i+j;k++)
ans+='0';
for(int k=i+j;k>=0&&ans[k]+j<='9';k--)
{
temp=(ans[k]-'0')+temp;
ans[k]=(char)('0'+temp%10);
temp/=10;
}
if(temp>=1)
ans=(char)('0'+temp)+ans;
}
}
const High_precision ret=(High_precision){ans};
return ret;
}
const High_precision operator / (const High_precision & a,const High_precision & b)
{
string str1=a.str,str2=b.str;
int len1=str1.length(),len2=str2.length();
string ans="";
if(len2>len1)
ans="0";
else
for(int i=0;i+len2-1<len1;i++)//str1[i~i+len2-1] str2[0~len2-1]
{
int l=0,r=9,cheng=0;
string jian;
while(l<=r)
{
int mid=(l+r)/2;
string product="";
int j=0,temp;
for(int w=len2-1;w>=0;w--)
{
temp=(str2[w]-'0')*mid+j;
j=temp/10;
product=(char)('0'+temp%10)+product;
}
if(j>=1)
{
r=mid-1;
continue;
}
string str=str1.substr(i,len2);
if(product==str)
{
jian=product;
cheng=mid;
break;
}
else if(product<str)
{
jian=product;
cheng=mid;
l=mid+1;
}
else
r=mid-1;
}
ans+=(char)('0'+cheng);
int j=0,temp;
for(int w=len2-1;w>=0;w--)
{
temp=(str1[i+w]-'0')-(jian[w]-'0')-j;
if(temp<0)
{
j=(0-temp)/10+((0-temp)%10!=0);
temp+=j*10;
}
str1[i+w]=temp;
}
}
const High_precision ret=(High_precision){ans};
return ret;
}
}
#endif