Q群中一网友贡献的一个模板
C++语言:
#include <cstring>
#include <cstdio>
#include <iostream>
#include <iomanip>
#define DLEN 4
//一个整数代表四位
#define MAXN 9999
//等于 10^DLEN - 1
//特别说明:只进行加减时可以改为一个整数代表9位
#define MAXSIZE 100
//用来表示大整数的数组的长度
//依赖头文件: <cstring>
class
bigint
{
//大整数类
public
:
int
a
[
MAXSIZE
];
int
len;
bigint()
{
len
=
1;
memset(
a
,
0
,
sizeof(
a
));}
bigint(
const
int);
bigint(
const
char
*);
bigint(
const
bigint
&);
bigint
&
operator
=(
const
bigint
&);
operator
int();
};
bigint
::
bigint(
const
int b)
{
int
c
,
d
= b;
len
=
0;
memset(
a
,
0
,
sizeof(
a));
while (
d
>
MAXN)
{
c
=
d
- (
d
/ (
MAXN
+
1))
* (
MAXN
+
1);
d
=
d
/ (
MAXN
+
1);
a
[
len
++
]
=
c;
}
a
[
len
++
]
=
d;
}
bigint
::
bigint(
const
char
*s)
{
//字符串大整数的格式为左端最高位
int
t
,
k
,
index
,
l
,
i
,
j;
memset(
a
,
0
,
sizeof(
a));
l
=
strlen(s)
-
1;
len
=
l
++
/
DLEN
+
1;
index
=
0;
for(
i
=
l
-
1;
i
>=
0;
i
-=
DLEN)
{
t
=
0;
k
=
i
-
DLEN
+
1
<
0
?
0
:
i
-
DLEN
+
1;
for (
j
=
k;
j
<=
i;
j
++)
t
=
t
*
10
+ s
[
j
]
-
'0';
a
[
index
++
]
=
t;
}
}
bigint
::
bigint(
const
bigint
&
T)
{
*
this
=
T;
}
bigint
&
bigint
::
operator
=(
const
bigint
& n)
{
memcpy(
a
, n
.
a
,
sizeof(
a));
len
= n
.
len;
return
*
this;
}
bigint
::
operator
int()
{
int
i
,n
=
0;
for (
i
=
len
-
1;
i
>=
0;
i
--)
{
n
*=
MAXN
+
1;
n
+=
a
[
i
];
}
return n;
}
//需使用头文件: <cstdio>
void
bigintscan(
bigint
* b)
{
char
ch
[
MAXSIZE
*
DLEN
];
scanf(
"%s"
,
ch);
*b
=
ch;
return ;
}
void
bigintprint(
bigint
&b)
{
int
i;
printf(
"%d"
,b
.
a
[b
.
len
-
1
]);
for(
i
= b
.
len
-
2 ;
i
>=
0 ;
i
--)
{
printf(
"d"
,b
.
a
[
i
]);
//修改DLEN时应同时修改此处
}
return;
}
//需使用头文件: <iostream> <iomanip>
std
::
istream
&
operator
>>(
std
::
istream
&
in
,
bigint
& b)
{
char
ch
[
MAXSIZE
*
DLEN
];
in
>>
ch;
b
=
ch;
return
in;
}
std
::
ostream
&
operator
<<(
std
::
ostream
&
out
,
bigint
& b)
{
int
i;
out
<< b
.
a
[b
.
len
-
1
];
for (
i
= b
.
len
-
2;
i
>=
0;
i
--)
{
out
<<
std
::
setw(
DLEN)
<<
std
::
setfill(
'0')
<< b
.
a
[
i
];
}
return
out;
}
//a>T返回1;a<T返回-1;相等返回0
int
comp(
bigint
const
&
a
,
bigint
const
&
T)
{
int
ln;
if (
a
.
len
>
T
.
len)
return
1;
if (
T
.
len
>
a
.
len)
return
-
1;
ln
=
a
.
len;
while (
ln
--)
{
if (
a
.
a
[
ln
]
!=
T
.
a
[
ln
])
break;
}
if (
ln
==
-
1)
return
0;
if (
a
.
a
[
ln
]
>
T
.
a
[
ln
])
return
1;
if (
a
.
a
[
ln
]
<
T
.
a
[
ln
])
return
-
1;
}
bool
operator
>(
bigint
&
a
,
bigint
&
T)
{
return (
comp(
a
,
T)
==
1);
}
bool
operator
==(
bigint
&
a
,
bigint
&
T)
{
return (
comp(
a
,
T)
==
0);
}
bool
operator
<(
bigint
&
a
,
bigint
&
T)
{
return (
comp(
a
,
T)
==-
1);
}
bool
operator
!=(
bigint
&
a
,
bigint
&
T)
{
return (
comp(
a
,
T)
!=
0);
}
bool
operator
>=(
bigint
&
a
,
bigint
&
T)
{
return (
comp(
a
,
T)
!=-
1);
}
bool
operator
<=(
bigint
&
a
,
bigint
&
T)
{
return (
comp(
a
,
T)
!=
1);
}
bool
operator
>(
bigint
&
a
,
int
t)
{
return (
comp(
a
,
bigint(
t))
==
1);
}
bool
operator
==(
bigint
&
a
,
int
t)
{
return (
comp(
a
,
bigint(
t))
==
0);
}
bool
operator
<(
bigint
&
a
,
int
t)
{
return (
comp(
a
,
bigint(
t))
==-
1);
}
bool
operator
!=(
bigint
&
a
,
int
t)
{
return (
comp(
a
,
bigint(
t))
!=
0);
}
bool
operator
>=(
bigint
&
a
,
int
t)
{
return (
comp(
a
,
bigint(
t))
!=-
1);
}
bool
operator
<=(
bigint
&
a
,
int
t)
{
return (
comp(
a
,
bigint(
t))
!=
1);
}
void
operator
+=(
bigint
&
a
,
bigint
&
T)
{
int
i
,
ln;
ln
=
T
.
len
>
a
.
len
?
T
.
len
:
a
.
len;
for (
i
=
0;
i
<
ln;
++
i)
{
a
.
a
[
i
]
+=
T
.
a
[
i
];
if (
a
.
a
[
i
]
>
MAXN)
{
++
a
.
a
[
i
+
1
];
a
.
a
[
i
]
-=
MAXN
+
1;
}
}
a
.
len
=
a
.
a
[
ln
]
!=
0
?
ln
+
1
:
ln;
return;
}
void
operator
-=(
bigint
&
a
,
bigint
&
T)
{
for (
int
i
=
0;
i
<
a
.
len;
++
i)
{
a
.
a
[
i
]
-=
T
.
a
[
i
];
if (
a
.
a
[
i
]
<
0)
{
if (
i
+
1
!=
a
.
len)
--
a
.
a
[
i
+
1
];
a
.
a
[
i
]
+=
MAXN
+
1;
}
}
if (
a
.
a
[
a
.
len
-
1
]
==
0)
a
.
len
--;
return;
}
void
operator
+=(
bigint
&
a
,
int n)
{
int
i;
for (
i
=
0; n;
i
++)
{
a
.
a
[
i
]
+= n
% (
MAXN
+
1);
n
/=
MAXN
+
1;
n
+=
a
.
a
[
i
]
/ (
MAXN
+
1);
a
.
a
[
i
]
%= (
MAXN
+
1);
}
if (
a
.
a
[
a
.
len
]
!=
0)
a
.
len
++;
return;
}
void
operator
-=(
bigint
&
a
,
int n)
{
int
i
,
k
= n;
for (
i
=
0;
k;
i
++)
{
a
.
a
[
i
]
-=
k
% (
MAXN
+
1);
k
/=
MAXN
+
1;
if (
a
.
a
[
i
]
<
0)
{
++
k;
a
.
a
[
i
]
+= (
MAXN
+
1);
}
}
if (
a
.
a
[
a
.
len
-
1
]
==
0)
a
.
len
--;
return;
}
bigint
operator
+(
bigint
&
a
,
bigint
&
T)
{
bigint
t
=
a;
t
+=
T;
return
t;
}
bigint
operator
-(
bigint
&
a
,
bigint
&
T)
{
bigint
t
=
a;
t
-=
T;
return
t;
}
bigint
operator
+(
bigint
&
a
,
int n)
{
bigint
t
=
a;
t
+= n;
return
t;
}
bigint
operator
-(
bigint
&
a
,
int n)
{
bigint
t
=
a;
t
-= n;
return
t;
}
void
operator
*=(
bigint
&
a
,
bigint
&
T)
{
int
i
,
j;
bigint
t;
memset((
void
*)
t
.
a
,
0
,
sizeof(
t
.
a));
t
.
len
=
a
.
len
+
T
.
len
-
1;
for (
i
=
0;
i
<
a
.
len;
i
++)
for (
j
=
0;
j
<
T
.
len;
j
++)
{
t
.
a
[
i
+
j
]
+=
a
.
a
[
i
]
*
T
.
a
[
j
];
if (
t
.
a
[
i
+
j
]
>
MAXN)
{
t
.
a
[
i
+
j
]
+=
t
.
a
[
i
+
j
]
/ (
MAXN
+
1);
t
.
a
[
i
+
j
]
%=
MAXN
+
1;
}
}
for (
t
.
len
+= (
t
.
a
[
t
.
len
]
>
0);
!
t
.
a
[
t
.
len
-
1
]
&&
t
.
len
>
1;
t
.
len
--)
{}
a
=
t;
}
void
operator
*=(
bigint
&
a
,
int n
){
int
i;
for (
a
.
a
[
0
]
*=n
,
i
=
1;
i
<
a
.
len;
i
++
){
a
.
a
[
i
]
*=n;
if (
a
.
a
[
i
-
1
]
>
MAXN)
a
.
a
[
i
]
+=
a
.
a
[
i
-
1
]
/
MAXN
,
a
.
a
[
i
-
1
]
%=
MAXN;
}
for (;
a
.
a
[
a
.
len
-
1
]
>
MAXN;
a
.
a
[
a
.
len
]
=
a
.
a
[
a
.
len
-
1
]
/(
MAXN
+
1
),
a
.
a
[
a
.
len
-
1
]
%=(
MAXN
+
1
),
a
.
len
++);
for (;
!
a
.
a
[
a
.
len
-
1
]
&&
a
.
len
>
1;
a
.
len
--);
return;
}
void
operator
/=(
bigint
&
a
,
int n)
{
int
r
=
0;
for (
int
i
=
a
.
len
-
1;
i
>
-
1;
--
i
){
a
.
a
[
i
]
+=
r
* (
MAXN
+
1);
r
=
a
.
a
[
i
]
% n;
a
.
a
[
i
]
/= n;
}
for (;
a
.
len
>
1
&&
a
.
a
[
a
.
len
-
1
]
==
0;
--
a
.
len);
return;
}
void
operator
%=(
bigint
&
a
,
int b)
{
int
i
,
d
=
0;
for (
i
=
a
.
len
-
1;
i
>=
0;
i
--)
{
d
= ((
d
* (
MAXN
+
1))
% b
+
a
.
a
[
i
])
% b;
}
a
=
d;
}
void
operator
^=(
bigint
&
a
,
int n)
{
if (n
<
0)
exit(
-
1);
if (n
==
0)
{
a
=
1;
return;
}
if (n
==
1)
return;
bigint
t
=
a
,
ret
=
1;
while (n
>
0)
{
if (n
&
1)
ret
*=
t;
t
*=
t;
n
>>=
1;
}
a
=
ret;
}
bigint
operator
*(
bigint
&
a
,
bigint
&
T)
{
bigint
t
=
a;
t
*=
T;
return
t;
}
bigint
operator
*(
bigint
&
a
,
int n)
{
bigint
t
=
a;
t
*= n;
return
t;
}
bigint
operator
/(
bigint
&
a
,
int n)
{
bigint
t
=
a;
t
/= n;
return
t;
}
bigint
operator
%(
bigint
&
a
,
int n)
{
bigint
t
=
a;
t
%= n;
return
t;
}
bigint
operator
^(
bigint
&
a
,
int n)
{
bigint
t
=
a;
t
^= n;
return
t;
}