- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int S = 333;
- const int MOD = 1e9+7;
- const double pi = 2 * acos (0.0);
- int _I(){int x; scanf("%d", &x); return x;}
- LL _LL(){LL x; scanf("%lld", &x); return x;}
- int dirX[]={1, 0, -1, 0, 1, -1, 1, -1};
- int dirY[]={0, 1, 0, -1, 1, -1, -1, 1};
- int rX[] = {1, 1, 2, 2, -1, -1, -2, -2};
- int rY[] = {2, -2, 1, -1, 2, -2, 1, -1};
- template <class T> T Distance(T x1, T y1, T x2, T y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));};
- int sset(int N, int pos){return N=N|(1<<pos);}
- bool check(int N, int pos){return (bool)(N&(1<<pos));}
- int reset(int N, int pos){return N=N&~(1<<pos);}
- ///...............Code Starts From Here...............///
- vector <int> B[S], Ans;
- int main(){
- string a, b;
- while(cin >> a >> b){
- int bs = b.size();
- int as = a.size();
- int carry = 0, k = 0;
- int mx = -1;
- for(int i = as-1; i >= 0; i--){
- int x = a[i]-'0';
- for(int j = bs-1; j >= 0; j--){
- int y = b[j]-'0';
- int z = (x*y)+carry;
- if(z > 9){
- B[k].push_back(z%10);
- carry = z/10;
- }
- else{
- B[k].push_back(z);
- carry = 0;
- }
- }
- while(carry){
- B[k].push_back(carry%10);
- carry /= 10;
- }
- int sz = B[k].size();
- mx = max(mx, sz);
- k++;
- int kk = 0;
- while(kk < k){
- kk++;
- B[k].push_back(0);
- }
- }
- for(int i = 0; i < k; i++){
- int sz = B[i].size();
- while(sz < mx){
- B[i].push_back(0);
- sz++;
- }
- }
- carry = 0;
- for(int i = 0; i < mx; i++){
- int z = 0;
- for(int j = 0; j < k; j++){
- z += B[j][i];
- }
- z += carry;
- if(z > 9){
- Ans.push_back(z%10);
- carry = z/10;
- }
- else{
- Ans.push_back(z);
- carry = 0;
- }
- }
- while(carry){
- Ans.push_back(carry%10);
- carry /= 10;
- }
- int zz = Ans.size();
- int zero = 0;
- for(int i = zz-1; i >= 0; i--)if(Ans[i] == 0)zero++;
- if(zero == zz)printf("0");
- else for(int i = zz-1; i >= 0; i--)printf("%d", Ans[i]);
- puts("");
- Ans.clear();
- for(int i = 0; i <= k; i++)B[i].clear();
- }
- return 0;
- }
Sunday, August 28, 2016
UVa - 10106 - Product
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment