Advertisement
cmiN

tmul

May 20th, 2011
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define base 1000000000
  4. #define len 9
  5. #define N 1113
  6.  
  7. unsigned long first[N], second[N], last[2 * N];
  8. unsigned short todel;
  9.  
  10. inline void init()
  11. {
  12.     char chr;
  13.     unsigned short cnt;
  14.     unsigned long total;
  15.     todel = first[0] = second[0] = 0;
  16.     cnt = 0;
  17.     total = getchar() - '0';
  18.     while (!isspace(chr = getchar())) {
  19.         if (++cnt != len) {
  20.             total = total * 10 + chr - '0';
  21.         } else {
  22.             first[++first[0]] = total;
  23.             total = chr - '0';
  24.             cnt = 0;
  25.         }
  26.     }
  27.     todel += len - ++cnt;
  28.     if (total) {
  29.         while (cnt < len) {
  30.             total *= 10;
  31.             ++cnt;
  32.         }
  33.     }
  34.     first[++first[0]] = total;
  35.     cnt = 0;
  36.     total = getchar() - '0';
  37.     while (!(isspace(chr = getchar()) || chr == EOF)) {
  38.         if (++cnt != len) {
  39.             total = total * 10 + chr - '0';
  40.         } else {
  41.             second[++second[0]] = total;
  42.             total = chr - '0';
  43.             cnt = 0;
  44.         }
  45.     }
  46.     todel += len - ++cnt;
  47.     if (total) {
  48.         while (cnt < len) {
  49.             total *= 10;
  50.             ++cnt;
  51.         }
  52.     }
  53.     second[++second[0]] = total;
  54.     //printf("%hu\n", todel);
  55. }
  56.  
  57. inline void zfill(unsigned long nr)
  58. {
  59.     short i;
  60.     if (nr) {
  61.         while (nr < base / 10) {
  62.             putchar('0');
  63.             nr *= 10;
  64.         }
  65.     } else {
  66.         for (i = 1; i < len; ++i) {
  67.             putchar('0');
  68.         }
  69.     }
  70. }
  71.  
  72. inline void process()
  73. {
  74.     unsigned long long temp;
  75.     unsigned short i, j;
  76.     last[0] = first[0] + second[0];
  77.     if (!first[1] || !second[1]) {
  78.         putchar('0');
  79.     } else {
  80.         //printf("%lu\n", last[0]);
  81.         for (i = first[0]; i > 0; --i) {
  82.             temp = 0;
  83.             for (j = second[0]; j > 0; --j) {
  84.                 temp = (unsigned long long) first[i] * second[j] + temp + last[i + j];
  85.                 last[i + j] = temp % base;
  86.                 temp /= base;
  87.                 //printf("%lu %lu\n", (unsigned long) temp, last[i + j]);
  88.             }
  89.             last[i + j] = temp;
  90.         }
  91.         if (last[0] > 2) {
  92.             printf("%lu", last[1]);
  93.             last[1] = 0;
  94.             for (i = 2; i < last[0] - 1; ++i) {
  95.                 zfill(last[i]);
  96.                 printf("%lu", last[i]);
  97.                 last[i] = 0;
  98.             }
  99.         }
  100.         if (todel < 9) {
  101.             if (last[0] > 2) {
  102.                 zfill(last[last[0] - 1]);
  103.             }
  104.             printf("%lu", last[last[0] - 1]);
  105.             temp = last[last[0]];
  106.             if (temp) {
  107.                 zfill(temp);
  108.             }
  109.         } else {
  110.             todel -= 9;
  111.             temp = last[last[0] - 1];
  112.             if (temp && last[0] > 2) {
  113.                 zfill(temp);
  114.             }
  115.         }
  116.         if (temp) {
  117.             while (todel) {
  118.                 temp /= 10;
  119.                 --todel;
  120.             }
  121.             printf("%lu", (unsigned long) temp);
  122.         } else {
  123.             i = len - todel;
  124.             while (i) {
  125.                 putchar('0');
  126.                 --i;
  127.             }
  128.         }
  129.     }
  130.     putchar('\n');
  131.     last[last[0]] = last[last[0] - 1] = 0;
  132. }
  133.  
  134. int main()
  135. {
  136.     short nr;
  137.     freopen("mul.in", "rt", stdin);
  138.     freopen("cmul.out", "wt", stdout);
  139.     scanf("%hd\n", &nr);
  140.     while (nr-- > 0) {
  141.         init();
  142.         process();
  143.     }
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement