Sunday, August 28, 2016

UVa - 10106 - Product

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. const int S = 333;
  5. const int MOD = 1e9+7;
  6. const double pi = 2 * acos (0.0);
  7. int _I(){int x; scanf("%d"&x); return x;}
  8. LL _LL(){LL x; scanf("%lld"&x); return x;}
  9. int dirX[]={10-101-11-1};
  10. int dirY[]={010-11-1-11};
  11. int rX[] = {1122-1-1-2-2};
  12. int rY[] = {2-21-12-21-1};
  13.  
  14. template <class T> T Distance(T x1, T y1, T x2, T y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));};
  15.  
  16. int sset(int N, int pos){return N=N|(1<<pos);}
  17. bool check(int N, int pos){return (bool)(N&(1<<pos));}
  18. int reset(int N, int pos){return N=N&~(1<<pos);}
  19. ///...............Code Starts From Here...............///
  20.  
  21. vector <int> B[S], Ans;
  22.  
  23. int main(){
  24.     string a, b;
  25.     while(cin >> a >> b){
  26.         int bs = b.size();
  27.         int as = a.size();
  28.         int carry = 0, k = 0;
  29.         int mx = -1;
  30.         for(int i = as-1; i >= 0; i--){
  31.             int x = a[i]-'0';
  32.             for(int j = bs-1; j >= 0; j--){
  33.                 int y = b[j]-'0';
  34.                 int z = (x*y)+carry;
  35.                 if(> 9){
  36.                     B[k].push_back(z%10);
  37.                     carry = z/10;
  38.                 }
  39.                 else{
  40.                     B[k].push_back(z);
  41.                     carry = 0;
  42.                 }
  43.             }
  44.             while(carry){
  45.                 B[k].push_back(carry%10);
  46.                 carry /= 10;
  47.             }
  48.             int sz = B[k].size();
  49.             mx = max(mx, sz);
  50.             k++;
  51.             int kk = 0;
  52.             while(kk < k){
  53.                 kk++;
  54.                 B[k].push_back(0);
  55.             }
  56.         }
  57.         for(int i = 0; i < k; i++){
  58.             int sz = B[i].size();
  59.             while(sz < mx){
  60.                 B[i].push_back(0);
  61.                 sz++;
  62.             }
  63.         }
  64.         carry = 0;
  65.         for(int i = 0; i < mx; i++){
  66.             int z = 0;
  67.             for(int j = 0; j < k; j++){
  68.                 z += B[j][i];
  69.             }
  70.             z += carry;
  71.             if(> 9){
  72.                 Ans.push_back(z%10);
  73.                 carry = z/10;
  74.             }
  75.             else{
  76.                 Ans.push_back(z);
  77.                 carry = 0;
  78.             }
  79.         }
  80.         while(carry){
  81.             Ans.push_back(carry%10);
  82.             carry /= 10;
  83.         }
  84.         int zz = Ans.size();
  85.         int zero = 0;
  86.         for(int i = zz-1; i >= 0; i--)if(Ans[i] == 0)zero++;
  87.         if(zero == zz)printf("0");
  88.         else for(int i = zz-1; i >= 0; i--)printf("%d", Ans[i]);
  89.         puts("");
  90.         Ans.clear();
  91.         for(int i = 0; i <= k; i++)B[i].clear();
  92.     }
  93.     return 0;
  94. }

No comments:

Post a Comment