-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBigInt.cpp
More file actions
39 lines (32 loc) · 943 Bytes
/
BigInt.cpp
File metadata and controls
39 lines (32 loc) · 943 Bytes
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
// Big int
//
// https://vjudge.net/problem/UVA-10106 - 10^250
int main() {
string x, y;
cin >> x >> y;
int n = x.size(), m = y.size();
vector<int> a(n), b(m), p(n + m, 0);
// invertendo para facilitar a lógica
for (int i = 0; i < n; i++) a[i] = x[n - 1 - i] - '0';
for (int j = 0; j < m; j++) b[j] = y[m - 1 - j] - '0';
// Multiplicação O(n·m)
for (int i = 0; i < n; i++) {
int carry = 0;
for (int j = 0; j < m; j++) {
int idx = i + j;
int prod = a[i] * b[j] + p[idx] + carry;
p[idx] = prod % 10;
carry = prod / 10;
}
// adiciona o carry
p[i + m] += carry;
}
// Remove zeros à esquerda (do fim do vetor invertido)
int k = n + m - 1;
while (k > 0 && p[k] == 0) --k;
// Imprime resultado em ordem correta
for (int i = k; i >= 0; i--) {
cout << p[i];
}
cout << "\n";
}