Advertisement
Insyder01

Untitled

Dec 10th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 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.  
  22. const int BASE = 1e9;
  23. vi setN(string s){
  24.     vi n, rn;
  25.     int c = 0;
  26.     string t;
  27.     for(int i = (int)s.size()-1;i>=0;i--){
  28.         t+=s[i], c++;
  29.         if(c == 9){
  30.             int x;
  31.             reverse(t.begin(), t.end());
  32.             stringstream ss; ss << t; ss >> x;
  33.             n.pb(x), t.clear(), c = 0;
  34.         }
  35.     }
  36.     if(c){
  37.         int x;reverse(t.begin(), t.end());
  38.         stringstream ss; ss << t; ss >> x;
  39.         n.pb(x);
  40.     }
  41.     for(int i = n.size()-1;i >=0;i--)rn.pb(n[i]);
  42.     return rn;
  43. }
  44. string print(vi &n){
  45.     string ans;
  46.     loop(i, 0, (int)n.size())
  47.     if (i == 0 && n[i]){
  48.         stringstream ss; string x;
  49.         ss << n[i]; ss >>x; ans+=x;
  50.         //cout << setw(0) << n[i];
  51.     }
  52.     else{
  53.         stringstream ss; string x;
  54.         ss << n[i]; ss >>x;
  55.         x.insert(0, 9 - (int)x.size(), '0'); ans +=x;
  56.         //cout << setfill('0') << setw(9) << n[i];
  57.     }
  58.     return ans;
  59. }
  60. string add(vi &n1, vi &n2){
  61.     vi ans;
  62.     int i, j, sz1 = (int)n1.size(), sz2 = (int)n2.size(), carry = 0;
  63.     if(sz1 < sz2) swap(n1, n2);
  64.     for(i = (int)n1.size()-1, j = (int)n2.size()-1;i >= 0; i--, j--){
  65.         j>=0? ans.pb( (carry + n1[i] + n2[j] ) % BASE), carry = (carry + n1[i] + n2[j] ) / BASE:ans.pb( (carry + n1[i]) % BASE), carry = (carry + n1[i]) / BASE;
  66. //      if(j>=0)
  67. //          ans.pb( (carry + n1[i] + n2[j] ) % BASE), carry = (carry + n1[i] + n2[j] ) / BASE;
  68. //      else
  69. //          ans.pb( (carry + n1[i]) % BASE), carry = (carry + n1[i]) / BASE;
  70.     }
  71.     if(carry) ans.pb(carry);
  72.     vi anss; for(int i = (int)ans.size()-1;i>=0;i--) anss.pb(ans[i]);
  73.     return print(anss);
  74. }
  75. string sub(vi &n1,vi  &n2){
  76.     int sz1 = (int)n1.size(), sz2 = n2.size();
  77.     if(sz1 < sz2) swap(n1, n2);
  78.     else if(sz1 == sz2){
  79.         bool big = 0, f = 1;
  80.         for(int i = 0;i < sz1 && f;i++){
  81.             if(n2[i] > n1[i]) f = 0, big = 1;
  82.             else if(n1[i] > n2[i]) f= 0;
  83.         }
  84.         if(big) swap(n1, n2);
  85.     }
  86.     vi ans;
  87.         int i = (int) n1.size()-1,  j = n2.size()-1, carry = 0;
  88.         for(;i >= 0; i--, j--){
  89.             if(j >= 0){
  90.                 if(n1[i] - n2[j]-carry >= 0)ans.pb(n1[i] - n2[j]-carry), carry = 0;
  91.                 else{
  92.                     n1[i] += BASE;
  93.                     ans.pb(n1[i] - n2[j]-carry), carry = 1;
  94.                 }
  95.             }
  96.             else{
  97.                 ans.pb(n1[i]-carry), carry = 0;
  98.             }
  99.         }
  100.     vi anss; for(int i = (int)ans.size()-1;i>=0;i--) anss.pb(ans[i]);
  101.     return print(anss);
  102. }
  103.  
  104. int main() {
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement