【模板】高精度

心血来潮想新开一个板子标签,第一个想到的竟然是高精度???

既然如此,我们就第一个放高精度呗╮(╯▽╰)╭

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
truct BigInteger
{
int d[10005];
int len;

inline void clean(){while (!d[len-1]&&len>1) len--;return;}

BigInteger() {memset(d,0,sizeof(d));len=1;return;}

BigInteger(int num) {*this=num;return;}

BigInteger(char* num) {*this=num;return;}


BigInteger operator =(char* str)
{
memset(d,0,sizeof(d));
int l=strlen(str);
len=min(700,l);
for (register int i=0;i<len;i++)
{
if (isdigit(str[len-i-1]))
d[i]=str[len-i-1]-'0';
else
d[i]=str[len-i-1]-'A';
}
clean();
return *this;
}

BigInteger operator =(string str)
{
memset(d,0,sizeof(d));
int l=str.length();
len=min(700,l);
for (register int i=0;i<len;i++)
{
if (isdigit(str[len-i-1]))
d[i]=str[len-i-1]-'0';
else
d[i]=str[len-i-1]-'A'+10;
}
clean();
return *this;
}

BigInteger operator =(int num)
{
char c[10005];
sprintf(c,"%d",num);
*this=c;
return *this;
}

BigInteger operator +(const BigInteger& b)
{
register int i;
BigInteger c=*this;
for (i=0;i<b.len;i++)
{
c.d[i]+=b.d[i];
if (c.d[i]>10-1)
{
c.d[i]%=10;
c.d[i+1]++;
}
}
while (c.d[i]>10-1)
{
c.d[i++]%=10;
c.d[i]++;
}
c.len=max(len,b.len);
if (c.d[i]&&c.len<= i)
c.len++;
return c;
}
BigInteger operator -(const BigInteger& b)
{
register int i;
BigInteger c=*this;
for (i=0;i<b.len;i++)
{
c.d[i]-=b.d[i];
if (c.d[i]<0)
{
c.d[i]+=10;
c.d[i+1]--;
}
}
while (c.d[i]<0)
{
c.d[i++]+=10;
c.d[i]--;
}
c.clean();
return c;
}

BigInteger operator *(const BigInteger& b)const
{
BigInteger c;
c.len=min(1010,len+b.len);
for (register int i=0;i<b.len;i++)
for (register int j=0;j<len;j++)
c.d[i+j]+=d[j]*b.d[i];
for (register int i=0;i<c.len-1;i++)
{
c.d[i+1]+=c.d[i]/10;
c.d[i]%=10;
}
c.clean();
return c;
}

BigInteger operator /(const BigInteger& b)
{
BigInteger c=*this,a=0;
register int j;
for (register int i=len-1;i>=0;i--)
{
a=a*10+d[i];
for (j=0;j<10;j++)
if (a<b*(j+1)) break;
c.d[i]=j;
a=a-b*j;
}
c.clean();
return c;
}

BigInteger operator %(const BigInteger& b)
{
BigInteger a=0;
register int j;
for (register int i=len-1;i>=0;i--)
{
a=a*10+d[i];
for (j=0;j<10;j++)
if (a<b*(j+1))
break;
a=a-b*j;
}
return a;
}

BigInteger operator +=(const BigInteger &b)
{
*this=*this+b;
return *this;
}

BigInteger operator -=(const BigInteger &b)
{
*this=*this-b;
return *this;
}

BigInteger operator *=(const BigInteger &b)
{
*this=*this*b;
return *this;
}

BigInteger operator /=(const BigInteger &b)
{
*this=*this/b;
return *this;
}

BigInteger operator %=(const BigInteger &b)
{
*this=*this%b;
return *this;
}

inline bool operator <(const BigInteger& b) const
{
if(len!=b.len)
return len<b.len;
for(register int i=len-1;i>=0;i--)
if(d[i]!=b.d[i])
return d[i]<b.d[i];
return 0;
}

inline bool operator >(const BigInteger& b) const{return b<*this;}

inline bool operator<=(const BigInteger& b) const{return !(b<*this);}

inline bool operator>=(const BigInteger& b) const{return !(*this<b);}

inline bool operator!=(const BigInteger& b) const{return b<*this||*this<b;}

inline bool operator==(const BigInteger& b) const{return !(b<*this)&&!(b>*this);}

inline string str() const
{
char s[10005]={};
for (register int i=0;i<len;i++)
s[len-i-1]=d[i]+'0';
return s;
}
};
-------------本文结束,感谢阅读-------------
0%