forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmul.cpp
99 lines (89 loc) · 2.59 KB
/
mul.cpp
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
// 2014-10-01
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
struct bignum {
typedef unsigned int uint;
vector<uint> digits;
static const uint RADIX = 1000000000;
bignum(): digits(1, 0) {}
bignum(const bignum& x): digits(x.digits) {}
bignum(const char* x) {
*this = x;
}
bignum& operator=(const char* s) {
int slen=strlen(s),i,l;
digits.resize((slen+8)/9);
for (l=0; slen>0; l++,slen-=9) {
digits[l]=0;
for (i=slen>9?slen-9:0; i<slen; i++) {
digits[l]=10*digits[l]+s[i]-'0';
}
}
while (digits.size() > 1 && !digits.back()) digits.pop_back();
return *this;
}
void mult(const bignum& x) {
vector<unsigned long long> res(digits.size() + x.digits.size());
for (int i=0; i<digits.size(); i++) {
for (int j=0; j<x.digits.size(); j++) {
unsigned long long p = digits[i]; p *= x.digits[j];
res[i+j] += p;
int k = i+j;
while (res[k] > 17e18) {
unsigned long long quot = res[k]/RADIX;
res[k] -= RADIX*quot;
res[++k] += quot;
}
}
}
digits.clear(); digits.resize(res.size());
unsigned long long carry = 0;
for (int i = 0; i < digits.size(); i++) {
unsigned long long new_carry = carry/RADIX;
digits[i] += carry - RADIX*new_carry;
unsigned long long quot = res[i]/RADIX;
digits[i] += res[i] - RADIX*quot;
while (digits[i] >= RADIX) {
digits[i] -= RADIX;
new_carry++;
}
carry = new_carry + quot;
}
while (digits.back() == 0 && digits.size() > 1) digits.pop_back();
}
string to_string() const {
ostringstream oss;
oss << digits.back();
for (int i = digits.size() - 2; i >= 0; i--) {
oss << setfill('0') << setw(9) << digits[i];
}
return oss.str();
}
};
char num1[11111], num2[11111];
void get_num(char* s) {
char c;
do {
c = getchar();
} while (c <= 32);
do {
*s++ = c;
c = getchar();
} while (c > 32);
*s = '\0';
}
int main() {
int n; scanf("%d", &n);
while (n--) {
get_num(num1); get_num(num2);
bignum l1(num1), l2(num2);
l1.mult(l2);
// string res = l1.to_string();
// puts(res.c_str());
}
}