Advertisement
welleyth

G?

Feb 16th, 2023
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6. #define pb push_back
  7. #define mp make_pair
  8.  
  9. constexpr int N = (int)3000 + 111;
  10. constexpr int INF = (int)1e9 + 11;
  11. constexpr int md = (int)1e9+7;
  12.  
  13. #pragma GCC optimize("O3")
  14. #pragma GCC optimize("unroll-loops")
  15. #pragma GCC target("avx2,popcnt")
  16.  
  17. int total = 0;
  18.  
  19. void normalize(string& s){
  20.     if(s.find('.') == string::npos)
  21.         return;
  22.     int pos = s.find('.');
  23.     while(s.back() == '0')
  24.         s.pop_back();
  25.     while(total >= 0 && s.size() - pos - 1 < total)
  26.         s.push_back('0');
  27.     if(s.back() == '.')
  28.         s.pop_back();
  29.     return;
  30. }
  31.  
  32. void solve(){
  33.     string A,B;
  34.     cin >> A >> B;
  35.  
  36.     bool allIntegers = (A.find('.') == string::npos && B.find('.') == string::npos);
  37.  
  38.     double a = atof(A.c_str()), b = atof(B.c_str());
  39.  
  40.     for(int i = 0; i < A.size(); i++)
  41.         if(A[i] == '.')
  42.             total += (A.size() - i - 1);
  43.  
  44.     int cnt = 0;
  45.     for(int i = 0; i < B.size(); i++)
  46.         if(B[i] == '.')
  47.             cnt = (B.size() - i - 1);
  48.  
  49.     total += cnt;
  50.  
  51.     vector<string> v;
  52.     int mx = 0;
  53.     mx = max(mx,(int)A.size());
  54.     mx = max(mx,(int)B.size());
  55.     int p = 0;
  56.     vector<int> T;
  57.     int S = total;
  58.  
  59.     for(int i = B.size()-1; i >= 0; i--){
  60.         if(B[i] == '.'){
  61.             continue;
  62.         }
  63.         double x = B[i] - '0';
  64.         if(!allIntegers){
  65.             if(cnt > 0)
  66.                 for(int j = 0; j < cnt; j++)
  67.                     x /= 10;
  68.             else
  69.                 for(int j = 0; j < -cnt; j++)
  70.                     x *= 10;
  71.         }
  72.         cnt--;
  73. //        cerr << "x*a: " << x*a << "\n";
  74.         string C = to_string(x*a);
  75.         normalize(C);
  76.         if(C.find('.') == string::npos && (A.find('.') != string::npos || B.find('.') != string::npos))
  77.             C.pb('.');
  78.         if(x > 0){
  79.             v.pb(C);
  80.             T.pb(max(0ll,S-total));
  81.             mx = max(mx,(int)C.size()+max(0ll,S-total));
  82.         }
  83.         if(!allIntegers)
  84.             total = max(0ll,total-1);
  85.         else
  86.             total--;
  87.     }
  88.     string t = to_string(a*b);
  89.     total = S;
  90.     normalize(t);
  91.     if(t.find('.') == string::npos && (A.find('.') != string::npos || B.find('.') != string::npos))
  92.         t.pb('.');
  93.     mx = max(mx,(int)t.size());
  94.  
  95.     for(int i = 0; i < mx - A.size(); i++)
  96.         cout << " ";
  97.     cout << A << "\n";
  98.     for(int i = 0; i < mx - B.size(); i++)
  99.         cout << " ";
  100.     cout << B << "\n";
  101.     for(int i = 0; i < mx; i++)
  102.         cout << "-";
  103.     cout << "\n";
  104.  
  105.     if(v.size() == 1){
  106.         for(int j = 0; j < mx-t.size(); j++)
  107.             cout << " ";
  108.         cout << t << "\n";
  109.     } else {
  110.         for(int i = 0; i < v.size(); i++){
  111.             for(int j = 0; j < mx-(int)v[i].size()-T[i]; j++)
  112.                 cout << " ";
  113.             cout << v[i];
  114.             for(int j = 0; j < T[i]; j++)
  115.                 cout << " ";
  116.             cout << "\n";
  117.         }
  118.         for(int i = 0; i < mx; i++)
  119.             cout << "-";
  120.         cout << "\n";
  121.         for(int j = 0; j < mx-t.size(); j++)
  122.             cout << " ";
  123.         cout << t;
  124.     }
  125.  
  126.     return;
  127. }
  128.  
  129. signed main(){
  130.     ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  131.     int tests = 1;
  132. //  cin >> tests;
  133.     for(int test = 1; test <= tests; test++){
  134.         solve();
  135.     }
  136.     return 0;
  137. }
  138. /**
  139.  
  140. a[0] a[1] a[2] a[3] a[4] a[5] a[6]
  141.  
  142. **/
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement