【模板】高精度

高精度的板子。
虽然是基础算法,但是已有的大多数板子都是一个光溜溜的计算,可扩展性不好。于是特地花了很长时间(两天www)整理了这个板子,需要顾及

  • 背板容易
  • 速度快
  • 能够连续计算、与普通整型混合计算
  • 大小比较
  • 有扩展性
  • 稳定的输入输出处理
  • 符合OI比赛的实际需求

另外还为C++的版本还设计了big_int大整数类,收录在FRSL中,还是比较不容易的。另外,有符号的情况还要麻烦一些,而OI场合正整数的情况较多,为此,这里提供了两个C语言风格的版本,分别对应有符号和无符号的版本。

其中,无符号版本中为了简单实现减法的符号输出,设计减法函数的返回值为结果的符号。

例程实现的是P1009阶乘之和。

无符号版

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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=36000;
struct num{
char n[MAX]; //number of int(not char!)
int len; //length
char& operator[](int i){
return n[i];
}
void clr(){
memset(n,0,sizeof(n));
len=1;
}
};

inline void add(num &a,num &b,num &ans){
num c;c.clr();
int i,jw=0,maxlen=max(a.len,b.len); //jinwei=0

for(i=0;i<maxlen;i++){
c[i]=a[i]+b[i]+jw;
jw=c[i]/10;
c[i]%=10;
}
if(jw!=0){ //the top
c[i]=jw;
i++;
}
c.len=i;
ans=c;
}

inline int numcmp(num &a,num &b){ //number compare
//a>b:1 a=b:0 a<b:-1
if(a.len>b.len)return 1;
if(a.len<b.len)return -1;

for(int i=a.len-1;i>=0;i--) //compare from high to low
if(a[i]>b[i])return 1;
else if(a[i]<b[i])return -1;
return 0;
}

inline bool sub(num &a,num &b,num &ans){ //substract answer>=0:false answer<0:true
num c;c.clr();
bool ansnega=0;
int cmp=numcmp(a,b),i,jw=0,maxlen=max(a.len,b.len);
if(cmp==0){ //==
c.len=1;
return false;
}
if(cmp<0){ //<
swap(a,b);
ansnega=true;
}

for(i=0;i<maxlen;i++){
c[i]=a[i]-jw;
jw=0;
if(c[i]<b[i]){
c[i]+=10;
jw++;
}
c[i]-=b[i];
}
c.len=i;
while(c[c.len-1]==0&&c.len>1)c.len--;
ans=c;
return ansnega;
}

inline void mul(num &a,num &b,num &ans){
int i,j,jw;
num c;c.clr();
for(i=0;i<a.len;i++){
jw=0;
for(j=0;j<b.len;j++){
c[i+j]=a[i]*b[j]+jw+c[i+j];
jw=c[i+j]/10;
c[i+j]%=10;
}
c[i+j]=jw;
}

c.len=a.len+b.len;
while(c[c.len-1]==0&&c.len>1)c.len--;
ans=c;
}

inline void split(num &a,int st,int ed,num &to){
to.clr();
to.len=0;
for(int i=ed;i>=st;i--)
to[to.len++]=a[a.len-i];
}

inline void numcpy(num &a,long long x){
a.clr();
if(x==0){a.len=1;return;}
a.len=0;
while(x!=0){
a[a.len]=x%10;
x/=10;
a.len++;
}
}

inline void gnum(num &a){ //get number
a.clr();
char t[MAX];
scanf("%s",t);
a.len=strlen(t);
int st=0;
while(t[st]=='0'&&st<a.len-1)st++;

for(int i=st;i<a.len;i++)
a[a.len-i-1]=t[i]-'0';
a.len-=st;
}

inline void pnum(num &a){ //put number
for(int i=a.len-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}

int main(){
int n;
num ans,f,now;
numcpy(ans,0);
numcpy(f,1);
scanf("%d",&n);

for(int i=1;i<=n;i++){
numcpy(f,1);
for(int j=1;j<=i;j++){
numcpy(now,j);
mul(f,now,f);
}
add(ans,f,ans);
}

pnum(ans);
return 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
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX=36000;
struct num{
char n[MAX]; //number of int(not char!)
int len; //length
bool nega;
};

void add(num &a,num &b,num &ans);
bool sub(num &a,num &b,num &ans);

inline void add(num &a,num &b,num &ans){
if(a.nega^b.nega){
num ta=a,tb=b;
if(b.nega){
tb.nega^=1;
sub(ta,tb,ans);
return;
}
else {
ta=a;
ta.nega^=1;
sub(tb,ta,ans);
return;
}
}

num c;
memset(c.n,0,sizeof(c.n));
int i,jw=0,maxlen; //jinwei=0

maxlen=max(a.len,b.len);

for(i=0;i<maxlen;i++){
c.n[i]=a.n[i]+b.n[i]+jw;
jw=c.n[i]/10;
c.n[i]%=10;
}
if(jw!=0){ //the top
c.n[i]=jw;
i++;
}
c.len=i;
c.nega=a.nega;
ans=c;
}

inline int numcmp(num &a,num &b){ //number compare
//a>b:1 a=b:0 a<b:-1
if(a.nega&&(!b.nega))return -1;
if((!a.nega)&&b.nega)return 1;

bool nega=a.nega;

if(a.len>b.len){
if(nega)return -1;
else return 1;
}
if(a.len<b.len){
if(nega)return 1;
else return -1;
}
//if a.len==b.len:
for(int i=a.len-1;i>=0;i--) //compare from high to low
if(nega){
if(a.n[i]<b.n[i])return 1;
else if(a.n[i]>b.n[i])return -1;
}
else{
if(a.n[i]<b.n[i])return -1;
else if(a.n[i]>b.n[i])return 1;
}
return 0;
}

inline bool sub(num &a,num &b,num &ans){ //substract answer>=0:false answer<0:true
num ta=a,tb=b;
if(a.nega^b.nega){
if(b.nega){
tb.nega^=1;
add(ta,tb,ans);
return 0;
}
else{
tb=b;
tb.nega^=1;
add(ta,tb,ans);
return 1;
}
}

num c;
memset(c.n,0,sizeof(c.n));
bool ansnega=0;

if(a.nega){
ta.nega^=1;
tb.nega^=1;
}
int cmp=numcmp(ta,tb),i,jw=0,maxlen;
if(cmp==0){ //==
c.len=1;
c.nega=0;
ans=c;
return false;
}
if(cmp<0){ //<
swap(a,b);
ansnega=true;
}
maxlen=max(a.len,b.len);
for(i=0;i<maxlen;i++){
c.n[i]=a.n[i]-jw;
jw=0;
if(c.n[i]<b.n[i]){
c.n[i]+=10;
jw++;
}
c.n[i]-=b.n[i];
}
c.len=i;
while(c.n[c.len-1]==0&&c.len>1)c.len--;
c.nega=ansnega^a.nega;
ans=c;
return ansnega;
}

inline void mul(num &a,num &b,num &ans){
int i,j,jw;
num c;
memset(c.n,0,sizeof(c.n));
bool ansnega=a.nega^b.nega;
for(i=0;i<a.len;i++){
jw=0;
for(j=0;j<b.len;j++){
c.n[i+j]=a.n[i]*b.n[j]+jw+c.n[i+j];
jw=c.n[i+j]/10;
c.n[i+j]%=10;
}
c.n[i+j]=jw;
}

c.len=a.len+b.len;
while(c.n[c.len-1]==0&&c.len>1)c.len--;
c.nega=ansnega;
ans=c;
}

inline void numcpy(num &a,long long x){
memset(a.n,0,sizeof(a.n));
if(x==0){
a.len=1;
return;
}
a.len=0;
while(x!=0){
a.n[a.len]=x%10;
x/=10;
a.len++;
}
}

inline void gnum(num &a){ //get number
memset(a.n,0,sizeof(a.n));
char t[MAX];

scanf("%s",t);
a.len=strlen(t);
int st=0;

if(t[st]=='-'){ //
a.nega=true;
st++;
}
else a.nega=false;

while(t[st]=='0'&&st<a.len-1)st++;

for(int i=st;i<a.len;i++)
a.n[a.len-i-1]=t[i]-'0';
a.len-=st;

if(a.n[a.len-1]==0)a.nega=false; //
}

inline void pnum(num &a){ //put number
if(a.nega)putchar('-'); //
for(int i=a.len-1;i>=0;i--)
printf("%d",a.n[i]);
printf("\n");
}

int main(){
int n;
num a,b,c;
gnum(a);
gnum(b);

add(a,b,c);
pnum(c);

sub(a,b,c);
pnum(c);
mul(a,b,c);
pnum(c);
return 0;
}