Insyder01

Untitled

Dec 9th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define loop(i, m, n) for(int i(m);i < n;i++)
  3. #define pb push_back
  4. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  5. #define DD(A) loop(i, 0, A.size())cout << A[i] <<" ";
  6. #define in(x) cin >> x
  7. #define clr(A, V) memset(A, V, sizeof(A))
  8. #define ff first
  9. #define ss second
  10. #define readfile(X) freopen(X, "r", stdin)
  11. #define writefile(X) freopen(X, "w", stdout)
  12. using namespace std;
  13. typedef long long ll;
  14. typedef pair<ll,ll> pll;
  15. typedef vector<int> vi;
  16. typedef pair<int,int> pii;
  17. typedef vector<pii> vpii;
  18. typedef pair<int, string> pis;
  19. typedef pair<pair<int, int>, pair<int, int > > piiii;
  20.  
  21. const int BASE = 1e9;
  22. vi setN(string s){
  23.     vi n, rn;
  24.     int c = 0;
  25.     string t;
  26.     for(int i = (int)s.size()-1;i>=0;i--){
  27.         t+=s[i], c++;
  28.         if(c == 9){
  29.             int x;
  30.             reverse(t.begin(), t.end());
  31.             stringstream ss; ss << t; ss >> x;
  32.             n.pb(x), t.clear(), c = 0;
  33.         }
  34.     }
  35.     if(c){
  36.         int x;reverse(t.begin(), t.end());
  37.         stringstream ss; ss << t; ss >> x;
  38.         n.pb(x);
  39.     }
  40.     for(int i = n.size()-1;i >=0;i--)rn.pb(n[i]);
  41.     return rn;
  42. }
  43. string print(vi &n){
  44.     string ans;
  45.     loop(i, 0, (int)n.size())
  46.     if (i == 0 && n[i]){
  47.         stringstream ss; string x;
  48.         ss << n[i]; ss >>x; ans+=x;
  49.         //cout << setw(0) << n[i];
  50.     }
  51.     else{
  52.         stringstream ss; string x;
  53.         ss << n[i]; ss >>x;
  54.         x.insert(0, 9 - (int)x.size(), '0'); ans +=x;
  55.         //cout << setfill('0') << setw(9) << n[i];
  56.     }
  57.     return ans;
  58. }
  59. string add(vi &n1, vi &n2){
  60.     vi ans;
  61.     int i, j, sz1 = (int)n1.size(), sz2 = (int)n2.size(), carry = 0;
  62.     if(sz1 < sz2) swap(n1, n2);
  63.     i = (int)n1.size()-1, j = (int)n2.size()-1;
  64.     for(;i >= 0; i--, j--){
  65.         if(j>=0)
  66.             ans.pb( (carry + n1[i] + n2[j] ) % BASE), carry = (carry + n1[i] + n2[j] ) / BASE;
  67.         else
  68.             ans.pb( (carry + n1[i]) % BASE), carry = (carry + n1[i]) / BASE;
  69.     }
  70.     if(carry) ans.pb(carry);
  71.     vi anss; for(int i = (int)ans.size()-1;i>=0;i--) anss.pb(ans[i]);
  72.     return print(anss);
  73. }
  74. string sub(vi &n1,vi  &n2){
  75.     int sz1 = (int)n1.size(), sz2 = n2.size();
  76.     if(sz1 < sz2) swap(n1, n2);
  77.     else if(sz1 == sz2){
  78.         bool big = 0, f = 1;
  79.         for(int i = 0;i < sz1 && f;i++){
  80.             if(n2[i] > n1[i]) f = 0, big = 1;
  81.             else if(n1[i] > n2[i]) f= 0;
  82.         }
  83.         if(big) swap(n1, n2);
  84.     }
  85.     vi ans;
  86.         int i = (int) n1.size()-1,  j = n2.size()-1, carry = 0;
  87.         for(;i >= 0; i--, j--){
  88.             if(j >= 0){
  89.                 if(n1[i] - n2[j]-carry >= 0)ans.pb(n1[i] - n2[j]-carry), carry = 0;
  90.                 else{
  91.                     n1[i] += BASE;
  92.                         ans.pb(n1[i] - n2[j]-carry), carry = 1;
  93.                 }
  94.             }
  95.             else{
  96.                 ans.pb(n1[i]-carry), carry = 0;
  97.             }
  98.         }
  99.     vi anss; for(int i = (int)ans.size()-1;i>=0;i--) anss.pb(ans[i]);
  100.     return print(anss);
  101. }
  102.  
  103. int main() {
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment