实用版本
#include <bits/stdc++.h>
using namespace std;
bool cmp(vector<int> A, vector<int> B) {
if (A.size() != B.size()) return A.size() > B.size();
for (int i = A.size() - 1; i >= 0; i--)
if (A[i] != B[i])
return A[i] > B[i];
return true;
}
vector<int> add(vector<int> A, vector<int> B) {
int t = 0;
vector<int> C;
for (int i = 0; i < A.size() || i < B.size(); i++) {
if (i < A.size()) t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t != 0) C.push_back(1);
return C;
}
vector<int> sub(vector<int> A, vector<int> B) {
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i++) {
t += A[i];
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t >= 0) t = 0;
else t = -1;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
vector<int> mul(vector<int> A, int b) {
int t = 0;
vector<int> C;
for (int i = 0; i < A.size() || t; i++) {
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
vector<int> MOD(string a, string b) {
bool neg = false;
if (a[0] == '-') {
neg = true;
a = a.substr(1);
}
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
while (true) {
int d = max(0, (int)(A.size() - B.size() - 1));
vector<int> C(d, 0);
for (int i : B) C.push_back(i);
while (cmp(A, C)) A = sub(A, C);
if (d == 0) {
if (neg) A = sub(C, A);
break;
}
}
reverse(A.begin(), A.end());
return A;
}
vector<int> MUL(string a, string b, bool& neg) {
if (a[0] == '-') {
neg ^= 1;
a = a.substr(1);
}
if (b[0] == '-') {
neg ^= 1;
b = b.substr(1);
}
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
vector<int> ans(1, 0);
for (int i = 0; i < B.size(); i++) {
vector<int> C(i, 0);
for (int j : A) C.push_back(j);
ans = add(ans, mul(C, B[i]));
}
reverse(ans.begin(), ans.end());
return ans;
}
void DIV(string a, string b, bool& neg, vector<int>& ans1, vector<int>& ans2) {
if (a[0] == '-') {
neg ^= 1;
a = a.substr(1);
}
if (b[0] == '-') {
neg ^= 1;
b = b.substr(1);
}
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
vector<int> ans(1, 0);
while (true) {
int d = max(0, (int)(A.size() - B.size() - 1));
vector<int> C(d, 0);
for (int i : B) C.push_back(i);
int cnt = 0;
while (cmp(A, C)) {
cnt++;
A = sub(A, C);
}
vector<int> D(d, 0);
D.push_back(cnt);
ans = add(ans, D);
if (d == 0) break;
}
reverse(ans.begin(), ans.end());
reverse(A.begin(), A.end());
ans1 = ans;
ans2 = A;
}
int main() {
string a, b;
cin >> a >> b;
bool neg = false;
vector<int> ans1, ans2;
DIV(a, b, neg, ans1, ans2);
if (neg) cout << "-";
for (int i : ans1) cout << i;
cout << " ";
if (neg && ans2[0]) cout << "-";
for (int i : ans2) cout << i;
return 0;
}
a ^ b % p
#include <bits/stdc++.h>
using namespace std;
bool cmp(vector<int> A, vector<int> B)
{
if (A.size() != B.size()) {
return A.size() > B.size();
}
for (int i = A.size() - 1; i >= 0; i--) {
if (A[i] != B[i]) {
return A[i] > B[i];
}
}
return true;
}
vector<int> sub(vector<int> A, vector<int> B) {
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i++) {
t += A[i];
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t >= 0) t = 0;
else t = -1;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
vector<int> add(vector<int> A, vector<int> B) {
int t = 0;
vector<int> C;
for (int i = 0; i < A.size() || i < B.size(); i++) {
if (i < A.size()) t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(1);
return C;
}
vector<int> mul(vector<int> A, int b) {
int t = 0;
vector<int> C;
for (int i = 0; i < A.size() || t; i++) {
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
vector<int> div(vector<int> A, int b)
{
vector<int> C;
int r = 0;
for (int i = A.size() - 1; i >= 0; i--)
{
r *= 10;
r += A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
vector<int> mul(vector<int> A, vector<int> B) {
vector<int> ans(1, 0);
for (int i = 0; i < B.size(); i++) {
vector<int> C(i, 0);
for (int j : A) C.push_back(j);
ans = add(ans, mul(C, B[i]));
}
return ans;
}
vector<int> mod(vector<int> A, vector<int> B) {
while (true) {
int d = max(0, (int)(A.size() - B.size() - 1));
vector<int> C(d, 0);
for (int i : B) C.push_back(i);
while (cmp(A, C)) A = sub(A, C);
if (d == 0) break;
}
return A;
}
int main() {
string a, b, p;
cin >> a >> b >> p;
vector<int> A, B, P;
for (int i = a.size() - 1; i >= 0; i--) {
A.push_back(a[i] - '0');
}
for (int i = b.size() - 1; i >= 0; i--) {
B.push_back(b[i] - '0');
}
for (int i = p.size() - 1; i >= 0; i--) {
P.push_back(p[i] - '0');
}
// res = a ^ (p - 2) % p
vector<int> res(1, 1);
while (!(B.size() == 1 && B[0] == 0)) {
if (B[0] & 1) {
res = mul(res, A);
res = mod(res, P);
}
B = div(B, 2);
A = mul(A, A);
A = mod(A, P);
}
reverse(res.begin(), res.end());
for (int i : res) cout << i;
return 0;
}
大数的取模运算
#include <bits/stdc++.h>
using namespace std;
vector<int> A, B;
bool cmp(vector<int> A, vector<int> B) {
if (A.size() != B.size()) return A.size() > B.size();
for (int i = A.size() - 1; i >= 0; i--)
if (A[i] != B[i])
return A[i] > B[i];
return true;
}
vector<int> sub(vector<int> A, vector<int> B) {
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i++) {
t += A[i];
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t >= 0) t = 0;
else t = -1;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main() {
string a, b;
cin >> a >> b;
bool neg = false;
if (a[0] == '-') {
neg = true;
a = a.substr(1);
}
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
while (true) {
int d = max(0, (int)(A.size() - B.size() - 1));
vector<int> C(d, 0);
for (int i : B) C.push_back(i);
while (cmp(A, C)) A = sub(A, C);
if (d == 0) {
if (neg) A = sub(C, A);
break;
}
}
for (int i = A.size() - 1; i >= 0; i--) cout << A[i];
return 0;
}
大数的乘法运算
#include <bits/stdc++.h>
using namespace std;
vector<int> add(vector<int> A, vector<int> B) {
int t = 0;
vector<int> C;
for (int i = 0; i < A.size() || i < B.size(); i++) {
if (i < A.size()) t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t != 0) C.push_back(1);
return C;
}
vector<int> mul(vector<int> A, int b) {
int t = 0;
vector<int> C;
for (int i = 0; i < A.size() || t; i++) {
if (i < A.size()) t += A[i] * b;
C.push_back(t % 10);
t /= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main() {
string a, b;
cin >> a >> b;
bool neg = false;
if (a[0] == '-') {
neg ^= 1;
a = a.substr(1);
}
if (b[0] == '-') {
neg ^= 1;
b = b.substr(1);
}
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
vector<int> ans(1, 0);
for (int i = 0; i < B.size(); i++) {
vector<int> C(i, 0);
for (int j : A) C.push_back(j);
ans = add(ans, mul(C, B[i]));
}
if (neg) cout << "-";
reverse(ans.begin(), ans.end());
for (int i : ans) cout << i;
return 0;
}
大数的除法运算
#include <bits/stdc++.h>
using namespace std;
bool cmp(vector<int> A, vector<int> B) {
if (A.size() != B.size()) return A.size() > B.size();
for (int i = A.size() - 1; i >= 0; i--)
if (A[i] != B[i])
return A[i] > B[i];
return true;
}
vector<int> sub(vector<int> A, vector<int> B) {
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i++) {
t += A[i];
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t >= 0) t = 0;
else t = -1;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
vector<int> add(vector<int> A, vector<int> B) {
int t = 0;
vector<int> C;
for (int i = 0; i < A.size() || i < B.size(); i++) {
if (i < A.size()) t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t != 0) C.push_back(1);
return C;
}
int main() {
string a, b;
cin >> a >> b;
bool neg = false;
if (a[0] == '-') {
neg ^= 1;
a = a.substr(1);
}
if (b[0] == '-') {
neg ^= 1;
b = b.substr(1);
}
vector<int> A, B;
for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');
for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');
vector<int> ans(1, 0);
while (true) {
int d = max(0, (int)(A.size() - B.size() - 1));
vector<int> C(d, 0);
for (int i : B) C.push_back(i);
int cnt = 0;
while (cmp(A, C)) {
cnt++;
A = sub(A, C);
}
vector<int> D(d, 0);
D.push_back(cnt);
ans = add(ans, D);
if (d == 0) break;
}
if (neg) cout << "-";
reverse(ans.begin(), ans.end());
for (int i : ans) cout << i;
cout << endl;
reverse(A.begin(), A.end());
if (neg && A[0]) cout << "-";
for (int i : A) cout << i;
cout << endl;
return 0;
}