Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- #define sz(a) (int)a.size()
- #define eq(a) for(int i=sz(a);i<k;i++)a.push_back(buf)
- using namespace std;
- const int nmax=4, D[5]={0,1,1,0,0}, E[5]={1,0,0,0,0}, Z[5]={0,0,0,0,0};
- struct num {
- int c[5];
- void init(const int x[5] = Z) {
- for (int i=0; i<=4; i++) this->c[i]=x[i]; }
- void inc(const int x[5] = E)
- {
- for (int i = 0; i <= 4; i++)
- {
- this->c[i] += x[i];
- if (this->c[i] > 1)
- this->c[i+1]+=this->c[i]/2, this->c[i]%=2;
- }
- }
- };
- typedef vector < num > vn;
- vn tonum(int x)
- {
- int t, c; vn ans; num p;
- while (x > 0)
- {
- p.init();
- t = x % 10, x = x / 10, c = 0;
- while (c < 5) p.c[c++] = t % 2, t = t / 2;
- ans.push_back(p);
- }
- return ans;
- }
- vn sum(vn a, vn b)
- {
- int k = max(sz(a), sz(b));
- vn ans, temp;
- num buf, over, o;
- buf.init();
- over.init(E);
- bool of = false;
- if (sz(a) > sz(b)) eq(b); else eq(a);
- for (int i = 0; i < k; i++)
- {
- buf.init();
- if (!of) ans.push_back(buf);
- of = false;
- buf.init(a[i].c);
- buf.inc(b[i].c);
- o.init(D);
- o.inc(buf.c);
- o.inc(ans[i].c);
- if (o.c[4] > 0)
- {
- of = true;
- o.c[4] = 0;
- ans.back().init(o.c);
- ans.push_back(over);
- } else ans.back().inc(buf.c);
- }
- return ans;
- }
- void printnum(vn x, string t = "")
- {
- int c;
- if (sz(x) > nmax) cout << "OVERFLOW\n", c = nmax; else c = sz(x);
- for (int j = c-1; j >=0; j--)
- {
- for (int i = 3; i >= 0; i--) cout << x[j].c[i];
- cout << " ";
- }
- if (t == "") t = "\n"; else t = "\n"+t+"\n";
- cout << t;
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- int a, b;
- vn A, B, C;
- cin >> a >> b;
- A = tonum(a);
- printnum(A, "+");
- B = tonum(b);
- printnum(B, "=");
- C = sum(A, B);
- printnum(C);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement