Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cctype>
- int first[10010], second[10010], last[20010];
- inline void read()
- {
- first[0] = second[0] = last[0] = 0;
- int min, i, temp;
- char c;
- while (!isspace(c = getchar())) {
- first[++first[0]] = c - '0';
- }
- while (!(isspace(c = getchar()) || c == EOF)) {
- second[++second[0]] = c - '0';
- }
- min = (first[0] < second[0] ? first[0] : second[0]) / 2;
- for (i = 1; i <= min; ++i) {
- temp = first[i];
- first[i] = first[first[0] - i + 1];
- first[first[0] - i + 1] = temp;
- temp = second[i];
- second[i] = second[second[0] - i + 1];
- second[second[0] - i + 1] = temp;
- }
- while (i <= first[0] / 2) {
- temp = first[i];
- first[i] = first[first[0] - i + 1];
- first[first[0] - i + 1] = temp;
- ++i;
- }
- while (i <= second[0] / 2) {
- temp = second[i];
- second[i] = second[second[0] - i + 1];
- second[second[0] - i + 1] = temp;
- ++i;
- }
- }
- inline void write()
- {
- for (int i = last[0]; i > 0; --i) {
- putchar(last[i] + '0');
- }
- putchar('\n');
- }
- void MultHuge(int* A, int* B, int* C)
- /* C <- A x B */
- { int i,j,T=0;
- if ((A[0] == 1 && A[1] == 0) || (B[0] == 1 && B[1] == 0)) {
- C[0] = 1;
- C[1] = 0;
- return;
- }
- C[0]=A[0]+B[0]-1;
- for (i=1;i<=A[0]+B[0];) C[i++]=0;
- for (i=1;i<=A[0];i++)
- for (j=1;j<=B[0];j++)
- C[i+j-1]+=A[i]*B[j];
- for (i=1;i<=C[0];i++)
- { T=(C[i]+=T)/10;
- C[i]%=10;
- }
- if (T) C[++C[0]]=T;
- }
- int main()
- {
- //freopen("mul.in", "rt", stdin);
- int nr;
- scanf("%d\n", &nr);
- while (nr-- > 0) {
- read();
- MultHuge(first, second, last);
- write();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement