大数模版

大数模板

我的模版:

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
struct BigInt{
const static int mod = 10;
int a[maxn], len;
BigInt(){
memset(a, 0, sizeof a);
len = 1;
}
BigInt(ll v){
memset(a, 0, sizeof a);
len = 0;
do{
a[len++] = v%mod;
v/=mod;
}while(v);
}
BigInt operator +(const BigInt &b)const{
BigInt res;
res.len = max(len, b.len);
for(int i = 0; i <= res.len; i++){
res.a[i] = 0;
}
for(int i = 0; i < res.len; i++){
res.a[i] += ((i < len)?a[i]:0) + ((i < b.len)?b.a[i]:0);
res.a[i+1] += res.a[i]/mod;
res.a[i] %= mod;
}
if(res.a[res.len] > 0) res.len++;
return res;
}
//保证a>=b
BigInt operator -(const BigInt &b)const{
BigInt res;
res.len = len;
for(int i = 0; i <= res.len; i++){
res.a[i] = 0;
}
for(int i = 0; i < res.len; i++){
res.a[i] += (a[i]-b.a[i]);
if(res.a[i]<0){
res.a[i]+=10;
res.a[i+1]--;
}
}
while(res.a[res.len-1]==0 && res.len > 1)res.len--;
return res;
}

BigInt operator *(const BigInt &b)const{
BigInt res;
for(int i = 0; i < len; i++){
int up = 0;
for(int j = 0; j < b.len; j++){
int temp = a[i]*b.a[j] + res.a[i+j] + up;
res.a[i+j] = temp%mod;
up = temp/mod;
}
if(up != 0)
res.a[i + b.len] = up;
}
res.len = len + b.len;
while(res.a[res.len - 1] == 0 && res.len > 1)res.len--;
return res;
}

void output(){
for(int i = len-1; i >= 0; i--)
printf("%d",a[i]);
}

void init(){
memset(a, 0, sizeof a);
len = 1;
}
};

BigInt power(BigInt a, int b){
BigInt ans = BigInt(1);
while(b){
if(b&1) ans = ans*a;
a = a*a;
b >>= 1;
}
return ans;
}

腿老师:

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
struct BigInteger {
int len, s[SIZE + 5];

BigInteger () {
memset(s, 0, sizeof(s));
len = 1;
}
BigInteger operator = (const char *num) { //字符串赋值
memset(s, 0, sizeof(s));
len = strlen(num);
for(int i = 0; i < len; i++) s[i] = num[len - i - 1] - '0';
return *this;
}
BigInteger operator = (const int num) { //int 赋值
memset(s, 0, sizeof(s));
char ss[SIZE + 5];
sprintf(ss, "%d", num);
*this = ss;
return *this;
}
BigInteger (int num) {
*this = num;
}
BigInteger (char* num) {
*this = num;
}
string str() const { //转化成string
string res = "";
for(int i = 0; i < len; i++) res = (char)(s[i] + '0') + res;
if(res == "") res = "0";
return res;
}
BigInteger clean() {
while(len > 1 && !s[len - 1]) len--;
return *this;
}

BigInteger operator + (const BigInteger& b) const {
BigInteger c;
c.len = 0;
for(int i = 0, g = 0; g || i < max(len, b.len); i++) {
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % 10;
g = x / 10;
}
return c.clean();
}

BigInteger operator - (const BigInteger& b) {
BigInteger c;
c.len = 0;
for(int i = 0, g = 0; i < len; i++) {
int x = s[i] - g;
if(i < b.len) x -= b.s[i];
if(x >= 0) g = 0;
else {
g = 1;
x += 10;
}
c.s[c.len++] = x;
}
return c.clean();
}

BigInteger operator * (const int num) const {
int c = 0, t;
BigInteger pro;
for(int i = 0; i < len; ++i) {
t = s[i] * num + c;
pro.s[i] = t % 10;
c = t / 10;
}
pro.len = len;
while(c != 0) {
pro.s[pro.len++] = c % 10;
c /= 10;
}
return pro.clean();
}

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

BigInteger operator / (const BigInteger &b) const {
BigInteger c, f;
for(int i = len - 1; i >= 0; --i) {
f = f * 10;
f.s[0] = s[i];
while(f >= b) {
f = f - b;
++c.s[i];
}
}
c.len = len;
return c.clean();
}
//高精度取模
BigInteger operator % (const BigInteger &b) const{
BigInteger r;
for(int i = len - 1; i >= 0; --i) {
r = r * 10;
r.s[0] = s[i];
while(r >= b) r = r - b;
}
r.len = len;
return r.clean();
}

bool operator < (const BigInteger& b) const {
if(len != b.len) return len < b.len;
for(int i = len - 1; i >= 0; i--)
if(s[i] != b.s[i]) return s[i] < b.s[i];
return false;
}
bool operator > (const BigInteger& b) const {
return b < *this;
}
bool operator <= (const BigInteger& b) const {
return !(b < *this);
}
bool operator == (const BigInteger& b) const {
return !(b < *this) && !(*this < b);
}
bool operator != (const BigInteger &b) const {
return !(*this == b);
}
bool operator >= (const BigInteger &b) const {
return *this > b || *this == b;
}
friend istream & operator >> (istream &in, BigInteger& x) {
string s;
in >> s;
x = s.c_str();
return in;
}
friend ostream & operator << (ostream &out, const BigInteger& x) {
out << x.str();
return out;
}
};
文章目录
  1. 1. 大数模板
|