Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- #include<bitset>
- #include<cmath>
- #include<deque>
- #include<iomanip>
- #include<list>
- #include<vector>
- #include<queue>
- #include<string>
- using namespace std;
- long long good_pow(long long n, long long step, long long raz) {
- if (step == 0) {
- return 1;
- }
- else if (step % 2 == 0) {
- return (good_pow(n, step / 2, raz) * good_pow(n, step / 2, raz)) % raz;
- }
- else {
- return (good_pow(n, step - 1, raz) * n) % raz;
- }
- }
- class BigInt {
- public:
- deque <long long> ch;
- bool znak = 1;
- long long len = 0;
- long long raz = long long(1e9);
- BigInt(string a) {
- long long pl = 0;
- for (long long i = 0; i < a.size(); ++i) { ///12345, pl = 123;
- pl *= 10;
- pl += (a[i] - 48);
- if (pl > raz) {
- int plp = pl / raz;
- ch.push_front(pl % raz);
- pl = plp;
- }
- }
- ch.push_front(pl);
- reverse(ch.begin(), ch.end());
- len = ch.size();
- }
- BigInt operator + (BigInt b) {
- BigInt a("");
- long long mlen = min(len, b.len);
- a.ch.assign(max(len, b.len) + 1, 0);
- for (long long i = 0; i < mlen; ++i) {
- if (ch[i] + b.ch[i] < raz) {
- a.ch[i] = ch[i] + b.ch[i];
- }
- else {
- a.ch[i] += (ch[i] + b.ch[i]) % raz;
- a.ch[i + 1] += 1;
- }
- }
- for (int i = mlen; i < len; ++i) {
- a.ch[i] = ch[i];
- }
- a.len = a.ch.size();
- return a;
- }
- BigInt operator - (BigInt b) {
- BigInt a("");
- a.ch.assign(max(len, b.len) + 1, 0);
- for (long long i = 0; i < b.len; ++i) {
- if (ch[i] - b.ch[i] < 0) {
- a.ch[i + 1] -= 1;
- a.ch[i] = ch[i] - b.ch[i] + raz;
- }
- else {
- a.ch[i] = ch[i] - b.ch[i];
- }
- }
- for (int i = b.len; i < len; ++i) {
- a.ch[i] = ch[i];
- }
- a.len = a.ch.size();
- return a;
- }
- BigInt operator * (BigInt b) {
- BigInt a("0");
- for (long long i = 0; i < b.len; ++i) { /// 375 * 24 = (4 * 5) (7 * 2)
- BigInt prom("");
- prom.ch.assign((len + 1), 0);
- for (long long j = 0; j < len; ++j) {
- if (b.ch[i] * ch[j] < raz) {
- prom.ch[j] += ch[j] * b.ch[i];
- }
- else {
- prom.ch[j] += (ch[j] * b.ch[i]) % raz;
- prom.ch[j + 1] += (ch[j] * b.ch[i]) / raz;
- }
- }
- string h = "";
- for (int j = 0; j < i; ++j) {
- h += "0";
- }
- prom.len = prom.ch.size();
- BigInt prom1(prom.returner() + h);
- a = prom1 + a;
- }
- a.len = a.ch.size();
- return a;
- }
- void print() {
- string vyvod = "";
- long long starter = 0;
- for (long long i = len - 1; i >= 0; --i) {
- if (ch[i] != 0) {
- break;
- }
- starter += 1;
- }
- for (long long i = 0; i < len - starter; i++) {
- string prom = to_string(ch[i]);
- if (prom.size() < (int)log10(raz)) {
- long long x = (int)prom.size();
- for (int i = 0; i < (int)log10(raz) - x; ++i) {
- prom = "0" + prom;
- }
- }
- vyvod = prom + vyvod;
- }
- // cout << vyvod << endl;
- if (!znak) {
- vyvod = "-" + vyvod;
- }
- if (vyvod[0] == 48) {
- string x = "";
- bool flag = 0;
- for (long long i = 0; i < vyvod.size(); ++i) {
- if (vyvod[i] != 48) {
- flag = 1;
- }
- if (flag) {
- x += vyvod[i];
- }
- }
- vyvod = x;
- }
- cout << vyvod << endl;
- }
- string returner() {
- string vyvod = "";
- long long starter = 0;
- for (long long i = len - 1; i >= 0; --i) {
- if (ch[i] != 0) {
- break;
- }
- starter += 1;
- }
- for (long long i = 0; i < len - starter; i++) {
- string prom = to_string(ch[i]);
- if (prom.size() < (int)log10(raz)) {
- long long x = (int)prom.size();
- for (int i = 0; i < (int)log10(raz) - x; ++i) {
- prom = "0" + prom;
- }
- }
- vyvod = prom + vyvod;
- }
- // cout << vyvod << endl;
- if (!znak) {
- vyvod = "-" + vyvod;
- }
- if (vyvod[0] == 48) {
- string x = "";
- bool flag = 0;
- for (long long i = 0; i < vyvod.size(); ++i) {
- if (vyvod[i] != 48) {
- flag = 1;
- }
- if (flag) {
- x += vyvod[i];
- }
- }
- vyvod = x;
- }
- return vyvod;
- }
- void look() {
- for (auto e : ch) cout << e << ' ';
- }
- };
- int main() {
- BigInt a("1234098673896"), b("10"); //10000000010 10 10 0 0
- a.print();
- b.print();
- a.look();
- cout << endl;
- b.look();
- cout << endl;
- (a + b).print();
- (a * b).print();
- //(a * b).look();
- //(a + b).look();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement