高精算法 - 整数

记录一下高精算法模板

Addition

原理

没什么好说的,小学数学——竖式计算,位数不足补零,当然实现里为了方便起见会将原数倒序,以便进位

实现

Addition
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
string add(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] == '-') {
neg = true, a = a.substr(1), b = b.substr(1);
} else if (a[0] == '-' && b[0] != '-')
return sub(b, a.substr(1));
else if (a[0] != '-' && b[0] == '-')
return sub(a, b.substr(1));
ull maxlen = max(a.size(), b.size()), l = 0, r = a.size() - 1;
string ans(maxlen + 1, '0');
while (l < r) (swap(a[l], a[r]), l++, r--);

l = 0, r = b.size() - 1;
while (l < r) (swap(b[l], b[r]), l++, r--);
a += string(maxlen - a.size(), '0'), b += string(maxlen - b.size(), '0');
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') / 10 + '0';
ans[i] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') % 10 + '0';
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1), l = 0;
while (l < r) (swap(ans[l], ans[r]), l++, r--);
return neg ? "-" + ans : ans;
}

Subtraction

原理

A + B 竖式计算,不够借位,同样倒序实现。

实现

Subtraction
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
string sub(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (b[0] == '-') return add(a, b.substr(1));
if (a[0] == '-') return "-" + add(a.substr(1), b);
if (a.size() < b.size() || (a.size() == b.size() && a < b))
(neg = true, swap(a, b));
ull maxlen = max(a.size(), b.size()), l = 0, r = a.size() - 1;
while (l < r) (swap(a[l], a[r]), l++, r--);
l = 0, r = b.size() - 1;
while (l < r) (swap(b[l], b[r]), l++, r--);
a += string(maxlen - a.size(), '0');
b += string(maxlen - b.size(), '0');
string ans = a;
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] -= ans[i] >= b[i] ? 0 : 1;
ans[i] = (ans[i] - b[i] + 10) % 10 + '0';
}
r = maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1), l = 0;
while (l < r) (swap(ans[l], ans[r]), l++, r--);
return neg ? "-" + ans : ans;
}

Multiplication

原理

竖式计算,位数不足补零,同样倒序实现。

实现

Multiplication
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
string mul(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
ull maxlen = max(a.size(), b.size());
ull l = 0, r = a.size() - 1;
string ans(maxlen * 2, '0');
while (l < r) (swap(a[l], a[r]), l++, r--);
l = 0, r = b.size() - 1;
while (l < r) (swap(b[l], b[r]), l++, r--);
if (a.size() < b.size()) swap(a, b);
for (ull i = 0; i < b.size(); ++i)
for (ull j = 0; j < a.size(); ++j) {
ans[i + j + 1] += (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) /
10; ans[i + j] = (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) % 10
+ '0';
}
r = 2 * maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1), l = 0;
while (l < r) (swap(ans[l], ans[r]), l++, r--);
return neg && ans != "0" ? "-" + ans : ans;
}

Division

原理

竖式计算

实现

  • 仅整除,不计算小数
Division
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
string div(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
string ans;
if (a.size() < b.size())
ans = "0";
else {
string remain = a.substr(0, b.size() - 1), temp;
for (ull i = b.size() - 1; i < a.size(); ++i) {
remain += a[i], remain = mul(remain, "1");
if (remain.size() > b.size() ||
(remain.size() == b.size() && remain >= b))
for (char t = '9'; t >= '0'; --t) {
temp = mul(b, string(1, t));
if (temp.size() < remain.size() ||
(temp.size() == remain.size() && temp <= remain)) {
ans += t, remain = sub(remain, temp);
break;
}
}
else
ans += '0';
}
}
if (ans != "0") {
int i = 0;
while (ans[i] == '0') i++;
ans = ans.substr(i);
}
return neg && ans != "0" ? "-" + ans : ans;
}

UPDATE

  • 优化了实现,使用 reverseinsert 以取得更高的性能
implement
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
string add(string a, string b);
string sub(string a, string b);
string mul(string a, string b);
string div(string a, string b);

string add(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] == '-') {
neg = true, a = a.substr(1), b = b.substr(1);
} else if (a[0] == '-' && b[0] != '-')
return sub(b, a.substr(1));
else if (a[0] != '-' && b[0] == '-')
return sub(a, b.substr(1));
ull maxlen = max(a.size(), b.size()), r = a.size() - 1;
string ans(maxlen + 1, '0');
if (a.size() > b.size())
b.insert(0, a.size() - b.size(), '0');
else
a.insert(0, b.size() - a.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') / 10 + '0';
ans[i] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') % 10 + '0';
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg ? "-" + ans : ans;
}

string sub(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (b[0] == '-') return add(a, b.substr(1));
if (a[0] == '-') return "-" + add(a.substr(1), b);
if (a.size() < b.size() || (a.size() == b.size() && a < b))
(neg = true, swap(a, b));
ull maxlen = max(a.size(), b.size()), r = a.size() - 1;
b.insert(0, a.size() - b.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string ans = a;
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] -= ans[i] >= b[i] ? 0 : 1;
ans[i] = (ans[i] - b[i] + 10) % 10 + '0';
}
r = maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg ? "-" + ans : ans;
}

string mul(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
ull maxlen = max(a.size(), b.size());
ull r = a.size() - 1;
string ans(maxlen * 2, '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if (a.size() < b.size()) swap(a, b);
for (ull i = 0; i < b.size(); ++i)
for (ull j = 0; j < a.size(); ++j) {
ans[i + j + 1] += (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) / 10;
ans[i + j] = (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) % 10 + '0';
}
r = 2 * maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg && ans != "0" ? "-" + ans : ans;
}

string div(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
string ans;
if (a.size() < b.size())
ans = "0";
else {
string remain = a.substr(0, b.size() - 1), temp;
for (ull i = b.size() - 1; i < a.size(); ++i) {
remain += a[i], remain = mul(remain, "1");
if (remain.size() > b.size() ||
(remain.size() == b.size() && remain >= b))
for (char t = '9'; t >= '0'; --t) {
temp = mul(b, string(1, t));
if (temp.size() < remain.size() ||
(temp.size() == remain.size() && temp <= remain)) {
ans += t, remain = sub(remain, temp);
break;
}
}
else
ans += '0';
}
}
if (ans != "0") {
int i = 0;
while (ans[i] == '0') i++;
ans = ans.substr(i);
}
return neg && ans != "0" ? "-" + ans : ans;
}

BigInt 类

实现

  • UPDATE: 已改
  • 运算符重载不太会用,可能有问题,以下代码仅供参考
BigInt
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
using ll = long long;
using ull = unsigned long long;

class BigInt {
private:
string value;
static map<int, string> digitToString;
string add(string a, string b);
string sub(string a, string b);
string mul(string a, string b);
string div(string a, string b);

public:
BigInt() : value("0") {}
BigInt(ll x) {
while (x) {
value = digitToString[(int)x % 10] + value;
x /= 10;
}
}
BigInt(string number) : value(number) {}
BigInt(string& number) : value(number) {}
BigInt(const BigInt& number) : value(number.value) {}
friend ostream& operator<<(ostream& output, const BigInt& x) {
return cout << x.value;
}
BigInt operator+(const BigInt& b) { return BigInt(add(value, b.value)); }
BigInt operator-(const BigInt& b) { return BigInt(sub(value, b.value)); }
BigInt operator/(const BigInt& b) { return BigInt(div(value, b.value)); }
BigInt operator*(const BigInt& b) { return BigInt(mul(value, b.value)); }
BigInt& operator=(const BigInt& x) {
value = x.value;
return *this;
}
BigInt& operator=(const string& number) {
value = number;
return *this;
}
};

string BigInt::add(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] == '-') {
neg = true, a = a.substr(1), b = b.substr(1);
} else if (a[0] == '-' && b[0] != '-')
return sub(b, a.substr(1));
else if (a[0] != '-' && b[0] == '-')
return sub(a, b.substr(1));
ull maxlen = max(a.size(), b.size()), r = a.size() - 1;
string ans(maxlen + 1, '0');
if (a.size() > b.size())
b.insert(0, a.size() - b.size(), '0');
else
a.insert(0, b.size() - a.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') / 10 + '0';
ans[i] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') % 10 + '0';
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg ? "-" + ans : ans;
}

string BigInt::sub(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (b[0] == '-') return add(a, b.substr(1));
if (a[0] == '-') return "-" + add(a.substr(1), b);
if (a.size() < b.size() || (a.size() == b.size() && a < b))
(neg = true, swap(a, b));
ull maxlen = max(a.size(), b.size()), r = a.size() - 1;
b.insert(0, a.size() - b.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string ans = a;
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] -= ans[i] >= b[i] ? 0 : 1;
ans[i] = (ans[i] - b[i] + 10) % 10 + '0';
}
r = maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg ? "-" + ans : ans;
}

string BigInt::mul(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
ull maxlen = max(a.size(), b.size());
ull r = a.size() - 1;
string ans(maxlen * 2, '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if (a.size() < b.size()) swap(a, b);
for (ull i = 0; i < b.size(); ++i)
for (ull j = 0; j < a.size(); ++j) {
ans[i + j + 1] += (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) / 10;
ans[i + j] = (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) % 10 + '0';
}
r = 2 * maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg && ans != "0" ? "-" + ans : ans;
}

string BigInt::div(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
string ans;
if (a.size() < b.size())
ans = "0";
else {
string remain = a.substr(0, b.size() - 1), temp;
for (ull i = b.size() - 1; i < a.size(); ++i) {
remain += a[i], remain = mul(remain, "1");
if (remain.size() > b.size() ||
(remain.size() == b.size() && remain >= b))
for (char t = '9'; t >= '0'; --t) {
temp = mul(b, string(1, t));
if (temp.size() < remain.size() ||
(temp.size() == remain.size() && temp <= remain)) {
ans += t, remain = sub(remain, temp);
break;
}
}
else
ans += '0';
}
}
if (ans != "0") {
int i = 0;
while (ans[i] == '0') i++;
ans = ans.substr(i);
}
return neg && ans != "0" ? "-" + ans : ans;
}

map<int, string> BigInt::digitToString = {
{1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"},
{6, "6"}, {7, "7"}, {8, "8"}, {9, "9"}, {0, "0"}};

实例

  • 以洛谷的P1255为例
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
#include <bits/stdc++.h>
#define mod (int)(1e9 + 7)
#define int long long
#define endl '\n'
#define pb push_back
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MAX = INT_MAX;
const ll LLMAX = LONG_LONG_MAX;
const double PI = acos(-1);
using namespace std;
class BigInt {
private:
string value;
static map<int, string> digitToString;
string add(string a, string b);
string sub(string a, string b);
string mul(string a, string b);
string div(string a, string b);

public:
BigInt() : value("0") {}
BigInt(ll x) {
while (x) {
value = digitToString[(int)x % 10] + value;
x /= 10;
}
}
BigInt(string number) : value(number) {}
BigInt(string& number) : value(number) {}
BigInt(const BigInt& number) : value(number.value) {}
friend ostream& operator<<(ostream& output, const BigInt& x) {
return cout << x.value;
}
BigInt operator+(const BigInt& b) { return BigInt(add(value, b.value)); }
BigInt operator-(const BigInt& b) { return BigInt(sub(value, b.value)); }
BigInt operator/(const BigInt& b) { return BigInt(div(value, b.value)); }
BigInt operator*(const BigInt& b) { return BigInt(mul(value, b.value)); }
BigInt& operator=(const BigInt& x) {
value = x.value;
return *this;
}
BigInt& operator=(const string& number) {
value = number;
return *this;
}
};

string BigInt::add(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] == '-') {
neg = true, a = a.substr(1), b = b.substr(1);
} else if (a[0] == '-' && b[0] != '-')
return sub(b, a.substr(1));
else if (a[0] != '-' && b[0] == '-')
return sub(a, b.substr(1));
ull maxlen = max(a.size(), b.size()), r = a.size() - 1;
string ans(maxlen + 1, '0');
if (a.size() > b.size())
b.insert(0, a.size() - b.size(), '0');
else
a.insert(0, b.size() - a.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') / 10 + '0';
ans[i] = (ans[i] - '0' + a[i] - '0' + b[i] - '0') % 10 + '0';
}
r = maxlen;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg ? "-" + ans : ans;
}

string BigInt::sub(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (b[0] == '-') return add(a, b.substr(1));
if (a[0] == '-') return "-" + add(a.substr(1), b);
if (a.size() < b.size() || (a.size() == b.size() && a < b))
(neg = true, swap(a, b));
ull maxlen = max(a.size(), b.size()), r = a.size() - 1;
b.insert(0, a.size() - b.size(), '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
string ans = a;
for (ull i = 0; i < maxlen; ++i) {
ans[i + 1] -= ans[i] >= b[i] ? 0 : 1;
ans[i] = (ans[i] - b[i] + 10) % 10 + '0';
}
r = maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg ? "-" + ans : ans;
}

string BigInt::mul(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
ull maxlen = max(a.size(), b.size());
ull r = a.size() - 1;
string ans(maxlen * 2, '0');
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
if (a.size() < b.size()) swap(a, b);
for (ull i = 0; i < b.size(); ++i)
for (ull j = 0; j < a.size(); ++j) {
ans[i + j + 1] += (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) / 10;
ans[i + j] = (ans[i + j] - '0' + (a[j] - '0') * (b[i] - '0')) % 10 + '0';
}
r = 2 * maxlen - 1;
while (r > 0 && ans[r] == '0') r--;
ans = ans.substr(0, r + 1);
reverse(ans.begin(), ans.end());
return neg && ans != "0" ? "-" + ans : ans;
}

string BigInt::div(string a, string b) {
bool neg = false;
if (a[0] == '+') a = a.substr(1);
if (b[0] == '+') b = b.substr(1);
if (a[0] == '-' && b[0] != '-') {
neg = true, a = a.substr(1);
} else if (a[0] != '-' && b[0] == '-') {
neg = true, b = b.substr(1);
} else if (a[0] == '-' && a[0] == b[0]) {
a = a.substr(1), b = b.substr(1);
}
string ans;
if (a.size() < b.size())
ans = "0";
else {
string remain = a.substr(0, b.size() - 1), temp;
for (ull i = b.size() - 1; i < a.size(); ++i) {
remain += a[i], remain = mul(remain, "1");
if (remain.size() > b.size() ||
(remain.size() == b.size() && remain >= b))
for (char t = '9'; t >= '0'; --t) {
temp = mul(b, string(1, t));
if (temp.size() < remain.size() ||
(temp.size() == remain.size() && temp <= remain)) {
ans += t, remain = sub(remain, temp);
break;
}
}
else
ans += '0';
}
}
if (ans != "0") {
int i = 0;
while (ans[i] == '0') i++;
ans = ans.substr(i);
}
return neg && ans != "0" ? "-" + ans : ans;
}

map<int, string> BigInt::digitToString = {
{1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"},
{6, "6"}, {7, "7"}, {8, "8"}, {9, "9"}, {0, "0"}};

void solve() {
int n;
cin >> n;
vector<BigInt> dp(n + 1);
dp[0] = dp[1] = "1";
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
cout << dp[n] << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}