Hướng dẫn giải của Xử lý số nguyên lớn


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

const base=100000000;
      digit=8;
      maxk=130000;
type bignum=array[0..maxk] of int64;
var a,b,c,d,re:bignum;
    e:array[1..1000] of char;
    bg:boolean;


procedure rf(var a:bignum);
var s:string; i,m,j:longint; c:char; code:integer; t:int64;
begin
     fillchar(a,sizeof(a),0);
     while not eoln do
     begin
          inc(a[0]);
          read(e[a[0]]);
     end;
     m:=a[0] mod digit;
     if m=0 then m:=digit;
     a[0]:=(a[0]+digit-1) div digit;
     s:='';
     for i:=1 to m do s:=s+e[i];
     val(s,t,code);
     a[a[0]]:=t;
     for i:=0 to a[0]-2 do
     begin
          s:='';
          for j:=1 to digit do s:=s+e[m+j+i*digit];
          val(s,t,code);
          a[a[0]-i-1]:=t;
     end;
end;

function big:boolean;
var i:longint;
begin
     big:=a[0]>b[0];
     if a[0]=b[0] then
     begin
          big:=true;
          for i:=a[0] downto 1 do
              if a[i]>b[i] then break
              else
              begin
                   if a[i]<b[i] then
                   begin
                        big:=false;
                        break;
                   end;
              end;
     end;
end;

procedure wr(var k:bignum);
var s:string; t,j,i:longint;
begin
     for i:=k[0] downto 1 do
     begin
          if i<k[0] then
          begin
               str(k[i],s);
               t:=length(s);
               for j:=t+1 to digit do write(0);
          end;
          write(k[i]);
     end;
     writeln;
end;

procedure plus;
var i,max:longint; mem:int64;
begin
     fillchar(c,sizeof(c),0);
     mem:=0;
     if a[0]>b[0] then max:=a[0] else max:=b[0];
     for i:=1 to max do
     begin
          c[i]:=(a[i]+b[i]+mem) mod base;
          mem:=(a[i]+b[i]+mem) div base;
     end;
     if mem>0 then
     begin
          inc(max);
          c[max]:=mem;
     end;
     c[0]:=max;
     wr(c);
end;

procedure minus;
var i,max:longint; mem:int64;
begin
     fillchar(c,sizeof(c),0);
     mem:=0;
     if not bg then
     begin
          write('-');
          d:=a; a:=b; b:=d;
     end;
     max:=a[0];
     for i:=1 to a[0] do
     begin
          if a[i]<b[i]+mem then
          begin
               c[i]:=a[i]+base-b[i]-mem;
               mem:=1;
          end
          else
          begin
               c[i]:=a[i]-b[i]-mem;
               mem:=0;
          end;
     end;
     while (c[max]=0) and (max>1) do dec(max);
     c[0]:=max;
     wr(c);
end;

procedure multi;
var i,j,max,maxr:longint; mem,t:int64;
begin
     fillchar(re,sizeof(re),0);
     maxr:=a[0];
     for i:=1 to b[0] do
     begin
          mem:=0;
          fillchar(c,sizeof(c),0);
          c[0]:=a[0];
          for j:=1 to a[0] do
          begin
               c[j]:=(a[j]*b[i]+mem) mod base;
               mem:=(a[j]*b[i]+mem) div base;
          end;
          if mem>0 then
          begin
               inc(c[0]);
               c[c[0]]:=mem;
          end;
          mem:=0;
          for j:=1 to c[0] do
          begin
               t:=c[j]+re[j+i-1]+mem;
               re[j+i-1]:=t mod base;
               mem:=t div base;
          end;
          j:=c[0]+i-1;
          if j>maxr then maxr:=j;
          while mem>0 do
          begin
               inc(j);
               if j>maxr then maxr:=j;
               t:=mem+re[j];
               re[j]:=t mod base;
               mem:=t div base;
          end;
     end;
     re[0]:=maxr;
     wr(re);
end;

begin
     rf(a);
     readln;
     rf(b);
     bg:=big;
     plus;
     minus;
     multi;
end.

Code mẫu của happyboy99x

a=int(input())
b=int(input())
print(a+b)
print(a-b)
print(a*b)

Code mẫu của ladpro98

#include <bits/stdc++.h>

using namespace std;

// * BIG INTEGER

typedef vector<int> bigInt;
const int BASE = 1000;
const int LENGTH = 3;

// * Refine function
bigInt& fix(bigInt &a) {
  a.push_back(0);
  for (int i = 0; i + 1 < a.size(); ++i) {
    a[i + 1] += a[i] / BASE; a[i] %= BASE;
    if (a[i] < 0) a[i] += BASE, --a[i + 1];
  }
  while (a.size() > 1 && a.back() == 0) a.pop_back();
  return a;
}

// * Constructors
bigInt big(int x) {
  bigInt result;
  while (x > 0) {
    result.push_back(x % BASE);
    x /= BASE;
  }
  return result;
}

bigInt big(string s) {
  bigInt result(s.size() / LENGTH + 1);
  for (int i = 0; i < s.size(); ++i) {
    int pos = (s.size() - i - 1) / LENGTH;
    result[pos] = result[pos] * 10 + s[i] - '0';
  }
  return fix(result), result;
}

// * Compare operators

int compare(bigInt &a, bigInt &b) {
  if (a.size() != b.size()) return (int)a.size() - (int)b.size();
  for (int i = 0; i < a.size(); ++i)
    if (a[i] != b[i]) return a[i] - b[i];
  return 0;
}

#define DEFINE_OPERATOR(x) bool operator x (bigInt &a, bigInt &b) { return compare(a, b) x 0; }
DEFINE_OPERATOR(==)
DEFINE_OPERATOR(!=)
DEFINE_OPERATOR(>)
DEFINE_OPERATOR(<)
DEFINE_OPERATOR(>=)
DEFINE_OPERATOR(<=)
#undef DEFINE_OPERATOR

// * Arithmetic operators

void operator += (bigInt &a, bigInt b) {
  a.resize(max(a.size(), b.size()));
  for (int i = 0; i < b.size(); ++i)
    a[i] += b[i];
  fix(a);
}

void operator -= (bigInt &a, bigInt b) {
  for (int i = 0; i < b.size(); ++i)
    a[i] -= b[i];
  fix(a);
}

void operator *= (bigInt &a, int b) {
  for (int i = 0; i < a.size(); ++i)
    a[i] *= b;
  fix(a);
}

void divide(bigInt a, int b, bigInt &q, int &r) {
  for (int i = int(a.size()) - 1; i >= 0; --i) {
    r = r * BASE + a[i];
    q.push_back(r / b); r %= b;
  }
  reverse(q.begin(), q.end());
  fix(q);
}

bigInt operator + (bigInt a, bigInt b) { a += b; return a; }
bigInt operator - (bigInt a, bigInt b) { a -= b; return a; }
bigInt operator * (bigInt a, int b) { a *= b; return a; }

bigInt operator / (bigInt a, int b) {
  bigInt q; int r = 0;
  divide(a, b, q, r);
  return q;
}
int operator % (bigInt a, int b) {
  bigInt q; int r = 0;
  divide(a, b, q, r);
  return r;
}

bigInt operator * (bigInt a, bigInt b) {
  bigInt result (a.size() + b.size());
  for (int i = 0; i < a.size(); ++i)
    for (int j = 0; j < b.size(); ++j)
      result[i + j] += a[i] * b[j];
  return fix(result);
}

// * I/O routines

istream& operator >> (istream& cin, bigInt &a) {
  string s; cin >> s;
  a = big(s);
  return cin;
}

ostream& operator << (ostream& cout, const bigInt &a) {
  cout << a.back();
  for (int i = (int)a.size() - 2; i >= 0; --i)
    cout << setw(LENGTH) << setfill('0') << a[i];
  return cout;
}

int main() {
  bigInt a, b;
  cin >> a >> b;
  cout << a + b << endl;
  if (a < b)
    cout << '-' << b - a << endl;
  else
    cout << a - b << endl;
  cout << a * b << endl;
  return 0;
}

Code mẫu của RR

#include <vector>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;

// base and base_digits must be consistent
const int base = 1000000000;
const int base_digits = 9;

struct bigint {
    vector<int> a;
    int sign;

    bigint() :
        sign(1) {
    }

    bigint(long long v) {
        *this = v;
    }

    bigint(const string &s) {
        read(s);
    }

    void operator=(const bigint &v) {
        sign = v.sign;
        a = v.a;
    }

    void operator=(long long v) {
        sign = 1;
        if (v < 0)
            sign = -1, v = -v;
        for (; v > 0; v = v / base)
            a.push_back(v % base);
    }

    bigint operator+(const bigint &v) const {
        if (sign == v.sign) {
            bigint res = v;

            for (int i = 0, carry = 0; i < (int) max(a.size(), v.a.size()) || carry; ++i) {
                if (i == (int) res.a.size())
                    res.a.push_back(0);
                res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
                carry = res.a[i] >= base;
                if (carry)
                    res.a[i] -= base;
            }
            return res;
        }
        return *this - (-v);
    }

    bigint operator-(const bigint &v) const {
        if (sign == v.sign) {
            if (abs() >= v.abs()) {
                bigint res = *this;
                for (int i = 0, carry = 0; i < (int) v.a.size() || carry; ++i) {
                    res.a[i] -= carry + (i < (int) v.a.size() ? v.a[i] : 0);
                    carry = res.a[i] < 0;
                    if (carry)
                        res.a[i] += base;
                }
                res.trim();
                return res;
            }
            return -(v - *this);
        }
        return *this + (-v);
    }

    void operator*=(int v) {
        if (v < 0)
            sign = -sign, v = -v;
        for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {
            if (i == (int) a.size())
                a.push_back(0);
            long long cur = a[i] * (long long) v + carry;
            carry = (int) (cur / base);
            a[i] = (int) (cur % base);
            //asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base));
        }
        trim();
    }

    bigint operator*(int v) const {
        bigint res = *this;
        res *= v;
        return res;
    }

    friend pair<bigint, bigint> divmod(const bigint &a1, const bigint &b1) {
        int norm = base / (b1.a.back() + 1);
        bigint a = a1.abs() * norm;
        bigint b = b1.abs() * norm;
        bigint q, r;
        q.a.resize(a.a.size());

        for (int i = a.a.size() - 1; i >= 0; i--) {
            r *= base;
            r += a.a[i];
            int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
            int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1];
            int d = ((long long) base * s1 + s2) / b.a.back();
            r -= b * d;
            while (r < 0)
                r += b, --d;
            q.a[i] = d;
        }

        q.sign = a1.sign * b1.sign;
        r.sign = a1.sign;
        q.trim();
        r.trim();
        return make_pair(q, r / norm);
    }

    bigint operator/(const bigint &v) const {
        return divmod(*this, v).first;
    }

    bigint operator%(const bigint &v) const {
        return divmod(*this, v).second;
    }

    void operator/=(int v) {
        if (v < 0)
            sign = -sign, v = -v;
        for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
            long long cur = a[i] + rem * (long long) base;
            a[i] = (int) (cur / v);
            rem = (int) (cur % v);
        }
        trim();
    }

    bigint operator/(int v) const {
        bigint res = *this;
        res /= v;
        return res;
    }

    int operator%(int v) const {
        if (v < 0)
            v = -v;
        int m = 0;
        for (int i = a.size() - 1; i >= 0; --i)
            m = (a[i] + m * (long long) base) % v;
        return m * sign;
    }

    void operator+=(const bigint &v) {
        *this = *this + v;
    }
    void operator-=(const bigint &v) {
        *this = *this - v;
    }
    void operator*=(const bigint &v) {
        *this = *this * v;
    }
    void operator/=(const bigint &v) {
        *this = *this / v;
    }

    bool operator<(const bigint &v) const {
        if (sign != v.sign)
            return sign < v.sign;
        if (a.size() != v.a.size())
            return a.size() * sign < v.a.size() * v.sign;
        for (int i = a.size() - 1; i >= 0; i--)
            if (a[i] != v.a[i])
                return a[i] * sign < v.a[i] * sign;
        return false;
    }

    bool operator>(const bigint &v) const {
        return v < *this;
    }
    bool operator<=(const bigint &v) const {
        return !(v < *this);
    }
    bool operator>=(const bigint &v) const {
        return !(*this < v);
    }
    bool operator==(const bigint &v) const {
        return !(*this < v) && !(v < *this);
    }
    bool operator!=(const bigint &v) const {
        return *this < v || v < *this;
    }

    void trim() {
        while (!a.empty() && !a.back())
            a.pop_back();
        if (a.empty())
            sign = 1;
    }

    bool isZero() const {
        return a.empty() || (a.size() == 1 && !a[0]);
    }

    bigint operator-() const {
        bigint res = *this;
        res.sign = -sign;
        return res;
    }

    bigint abs() const {
        bigint res = *this;
        res.sign *= res.sign;
        return res;
    }

    long long longValue() const {
        long long res = 0;
        for (int i = a.size() - 1; i >= 0; i--)
            res = res * base + a[i];
        return res * sign;
    }

    friend bigint gcd(const bigint &a, const bigint &b) {
        return b.isZero() ? a : gcd(b, a % b);
    }
    friend bigint lcm(const bigint &a, const bigint &b) {
        return a / gcd(a, b) * b;
    }

    void read(const string &s) {
        sign = 1;
        a.clear();
        int pos = 0;
        while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) {
            if (s[pos] == '-')
                sign = -sign;
            ++pos;
        }
        for (int i = s.size() - 1; i >= pos; i -= base_digits) {
            int x = 0;
            for (int j = max(pos, i - base_digits + 1); j <= i; j++)
                x = x * 10 + s[j] - '0';
            a.push_back(x);
        }
        trim();
    }

    friend istream& operator>>(istream &stream, bigint &v) {
        string s;
        stream >> s;
        v.read(s);
        return stream;
    }

    friend ostream& operator<<(ostream &stream, const bigint &v) {
        if (v.sign == -1)
            stream << '-';
        stream << (v.a.empty() ? 0 : v.a.back());
        for (int i = (int) v.a.size() - 2; i >= 0; --i)
            stream << setw(base_digits) << setfill('0') << v.a[i];
        return stream;
    }

    static vector<int> convert_base(const vector<int> &a, int old_digits, int new_digits) {
        vector<long long> p(max(old_digits, new_digits) + 1);
        p[0] = 1;
        for (int i = 1; i < (int) p.size(); i++)
            p[i] = p[i - 1] * 10;
        vector<int> res;
        long long cur = 0;
        int cur_digits = 0;
        for (int i = 0; i < (int) a.size(); i++) {
            cur += a[i] * p[cur_digits];
            cur_digits += old_digits;
            while (cur_digits >= new_digits) {
                res.push_back(int(cur % p[new_digits]));
                cur /= p[new_digits];
                cur_digits -= new_digits;
            }
        }
        res.push_back((int) cur);
        while (!res.empty() && !res.back())
            res.pop_back();
        return res;
    }

    typedef vector<long long> vll;

    static vll karatsubaMultiply(const vll &a, const vll &b) {
        int n = a.size();
        vll res(n + n);
        if (n <= 32) {
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    res[i + j] += a[i] * b[j];
            return res;
        }

        int k = n >> 1;
        vll a1(a.begin(), a.begin() + k);
        vll a2(a.begin() + k, a.end());
        vll b1(b.begin(), b.begin() + k);
        vll b2(b.begin() + k, b.end());

        vll a1b1 = karatsubaMultiply(a1, b1);
        vll a2b2 = karatsubaMultiply(a2, b2);

        for (int i = 0; i < k; i++)
            a2[i] += a1[i];
        for (int i = 0; i < k; i++)
            b2[i] += b1[i];

        vll r = karatsubaMultiply(a2, b2);
        for (int i = 0; i < (int) a1b1.size(); i++)
            r[i] -= a1b1[i];
        for (int i = 0; i < (int) a2b2.size(); i++)
            r[i] -= a2b2[i];

        for (int i = 0; i < (int) r.size(); i++)
            res[i + k] += r[i];
        for (int i = 0; i < (int) a1b1.size(); i++)
            res[i] += a1b1[i];
        for (int i = 0; i < (int) a2b2.size(); i++)
            res[i + n] += a2b2[i];
        return res;
    }

    bigint operator*(const bigint &v) const {
        vector<int> a6 = convert_base(this->a, base_digits, 6);
        vector<int> b6 = convert_base(v.a, base_digits, 6);
        vll a(a6.begin(), a6.end());
        vll b(b6.begin(), b6.end());
        while (a.size() < b.size())
            a.push_back(0);
        while (b.size() < a.size())
            b.push_back(0);
        while (a.size() & (a.size() - 1))
            a.push_back(0), b.push_back(0);
        vll c = karatsubaMultiply(a, b);
        bigint res;
        res.sign = sign * v.sign;
        for (int i = 0, carry = 0; i < (int) c.size(); i++) {
            long long cur = c[i] + carry;
            res.a.push_back((int) (cur % 1000000));
            carry = (int) (cur / 1000000);
        }
        res.a = convert_base(res.a, 6, base_digits);
        res.trim();
        return res;
    }
};

int main() {
    ios :: sync_with_stdio(false);
    string A, B; cin >> A >> B;
    bigint a(A);
    bigint b(B);

    cout << a + b << endl;
    cout << a - b << endl;
    cout << a * b << endl;
}

Code mẫu của hieult

#include <cstdlib>
#include <iostream>
using namespace std;
int a[3000],b[3000],c[3000],_a[3000],_b[3000],_c[3000],ti[3000],hi[3000],to[3000];
int a1,b1,c1,i,_a1,_b1,_c1;
int tong()
{
long tong,nho=0;
int i,j;
//int a[1000], b[1000], c[1000],a1, b1, c1;
a1=_a1;
b1=_b1;
for (i=1; i<=a1; i++) a[i]=_a[i];
for (i=1; i<=b1; i++) b[i]=_b[i];
for (i=1; i<=a1; i++) c[i]=a[i];
for (i=1; i<=a1; i++) a[i]=c[a1-i+1];
for (i=1; i<=b1; i++) c[i]=b[i];
for (i=1; i<=b1; i++) b[i]=c[b1-i+1];
if (a1>b1) c1=a1; else c1=b1;
for (i=a1+1; i<=c1; i++)a[i]=0;
for (i=b1+1; i<=c1; i++)b[i]=0;
for (i=1; i<=c1; i++)
  {
  tong=nho+a[i]+b[i];
  c[i]=tong%10;
  nho=tong/10;
  }
if (nho>0) c1++, c[c1]=nho;
for (i=1; i<=c1; i++) a[i]=c[i];
for (i=1; i<=c1; i++) c[i]=a[c1-i+1];
for (i=1; i<=c1; i++) to[i]=c[i];
to[0]=c1;
}
int _hieu()
{
//int a[1000], b[1000], c[1000], a1, b1, c1;
long tong,nho=0,dau=1;
int i,j,tg;
a1=_a1;
b1=_b1;
for (i=1; i<=a1; i++) a[i]=_a[i];
for (i=1; i<=b1; i++) b[i]=_b[i];
for (i=1; i<=a1; i++) c[i]=a[i];
for (i=1; i<=a1; i++) a[i]=c[a1-i+1];
for (i=1; i<=b1; i++) c[i]=b[i];
for (i=1; i<=b1; i++) b[i]=c[b1-i+1];
if (a1>b1) c1=a1; else c1=b1;
for (i=a1+1; i<=c1; i++)a[i]=0;
for (i=b1+1; i<=c1; i++)b[i]=0;
for (i=c1; i>=1; i--)
if (a[i]!=b[i]){
if (a[i]<b[i])dau=-1; else dau=1;
break;
}
if (dau==-1){
for (i=1; i<=c1; i++){
tg=a[i];
a[i]=b[i];
b[i]=tg;
}
}
for (i=1; i<=c1; i++){
tong=10+nho+a[i]-b[i];
c[i]=tong%10;
if (tong/10>0) nho=0; else nho=-1;
}
while (c[c1]==0& c1>1)c1--;
for (i=1; i<=c1; i++) a[i]=c[i];
for (i=1; i<=c1; i++) c[i]=a[c1-i+1];
hi[c1+1]=dau;
hi[0]=c1;
for (i=1; i<=c1; i++) hi[i]=c[i];
}
int tich(){
//int a[1000], b[1000], c[1000], a1, b1, c1;
long tong=0,nho=0;
int i,j;
a1=0;b1=0;c1=0;
a1=_a1;
b1=_b1;
tong=0; nho=0;
for (i=1; i<=a1; i++) a[i]=_a[i];
for (i=1; i<=b1; i++) b[i]=_b[i];
for (i=1; i<=a1; i++) c[i]=a[i];
for (i=1; i<=a1; i++) a[i]=c[a1-i+1];
for (i=1; i<=b1; i++) c[i]=b[i];
for (i=1; i<=b1; i++) b[i]=c[b1-i+1];
c1=a1+b1;
for (i=1; i<=c1; i++) c[i]=0;
for (i=1; i<=c1; i++){
tong=nho;
for (j=1; j<=i; j++)
tong = tong + a[j]*b[i+1-j];
c[i]=tong%10;
nho=tong/10; 
}
if (nho>0) c[c1]=nho;
if (c[c1]==0) c1--;
for (i=1; i<=c1; i++) a[i]=c[i];
for (i=1; i<=c1; i++) c[i]=a[c1-i+1];
ti[0]=c1;
for (i=1; i<=c1; i++) ti[i]=c[i];
}
int nhap(){
string st1,st2;
char ch='a';
cin>>st1;
cin>>st2;
a1=st1.length();
b1=st2.length();
for (i=0; i<a1; i++) a[i+1]=short(st1[i])-48;
for (i=0; i<b1; i++) b[i+1]=short(st2[i])-48;
for (i=1; i<=a1; i++) _a[i]=a[i];
for (i=1; i<=b1; i++) _b[i]=b[i];
_a1=a1;
_b1=b1;
}
int thu(){
tich();
tong(); 
_hieu();
for(i=1; i<=to[0]; i++) cout<<to[i]; cout<<endl;
if (hi[hi[0]+1]==-1) cout<<"-";
for(i=1; i<=hi[0]; i++) cout<<hi[i]; cout<<endl;
for(i=1; i<=ti[0]; i++) cout<<ti[i]; 
}
int main(int argc, char *argv[])
{
nhap();
thu();
//system("PAUSE");
return(0);
}

Code mẫu của ll931110

Program BIGNUM;
        Const
                input  = '';
                output = '';
        Type
                arr = array[1..2000] of longint;
        Var
                a,b,c: arr;
                k,m,n: integer;
                   fa: text;
                   ch: char;
                    i: integer;

Procedure init;
          Var
                f: text;
                i: integer;
          Begin
                m:= 0;
                n:= 0;

                Assign(f, input);
                       Reset(f);
                       While not eoln(f) do
                                Begin
                                        inc(m);
                                        Read(f, ch);
                                        a[m]:= ord(ch) - 48;
                                End;
                       For i:= 1 to m do a[2000 - m + i]:= a[i];
                       m:= 2001 - m;

                       Readln(f);
                       While not eoln(f) do
                                Begin
                                        inc(n);
                                        Read(f, ch);
                                        b[n]:= ord(ch) - 48;
                                End;
                       For i:= 1 to n do b[2000 - n + i]:= b[i];
                       n:= 2001 - n;
                Close(f);

                If m > n then k:= n else k:= m;
          End;

Procedure printresult(p: integer);
          Var
                i: integer;
          Begin
                For i:= p to 2000 do write(fa, c[i]);
                Writeln(fa);
          End;

Procedure add;
          Var
                i: integer;
          Begin
                Fillchar(c, sizeof(c), 0);

                For i:= 2000 downto k do
                        Begin
                                c[i]:= c[i] + (a[i] + b[i]);
                                If c[i] > 9 then
                                        Begin
                                                inc(c[i - 1]);
                                                c[i]:= c[i] - 10;
                                        End;
                        End;

                If c[k - 1] <> 0 then printresult(k - 1)
                                 else printresult(k);
          End;

Procedure subtract(x,y: arr);
          Var
                s,i: integer;
          Begin
                Fillchar(c, sizeof(c), 0);

                For i:= 2000 downto k do
                        Begin
                                c[i]:= c[i] + (x[i] - y[i]);
                                If c[i] < 0 then
                                        Begin
                                                dec(c[i - 1]);
                                                c[i]:= c[i] + 10;
                                        End;
                        End;

                 s:= k;
                 While c[s] = 0 do inc(s);
                 printresult(s);
          End;

Procedure minus;
          Var
                d: integer;
          Begin
                d:= k;
                While (d <= 2000) and (a[d] = b[d]) do inc(d);

                If d > 2000 then writeln(fa, 0) else
                        If a[d] > b[d] then subtract(a,b)
                                       else begin
                                                  write(fa, '-');
                                                  subtract(b,a);
                                            end;
          End;


Procedure multiply;
          Var
                i,j,t: integer;
          Begin
                Fillchar(c, sizeof(c), 0);

                For i:= 2000 downto m do
                        For j:= 2000 downto n do
                                Begin
                                        t:= i + j - 2000;
                                        c[t]:= c[t] + a[i] * b[j];
                                        If c[t] > 9 then
                                                Begin
                                                        c[t - 1]:= c[t - 1] + c[t] div 10;
                                                        c[t]:= c[t] mod 10;
                                                End;
                                End;

                t:= 1;
                While c[t] = 0 do inc(t);
                printresult(t);
          End;

Begin
        init;
        Assign(fa, output);
                Rewrite(fa);
                        add;
                        minus;
                        multiply;
                Close(fa);
End.

Code mẫu của skyvn97

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX   2611
using namespace std;
struct bignum
{
       int nd;
       int sb;
       int d[MAX];
       bignum()
       {
        nd=0;
       }     
       bignum(int x)
       {
        if (x==0)
           {
            nd=1;
            sb=1;
            d[1]=0;
           }
        if (x>0)
           {
            nd=0;
            sb=1;
            while (x>0)
                  {
                   nd++;
                   d[nd]=x%10;
                   x=x/10;
                  }            
           }
        if (x<0)
           {
            nd=0;
            sb=-1;
            x=-x;
            while (x>0)
                  {
                   nd++;
                   d[nd]=x%10;
                   x=x/10;
                  }
           }
       }
       bignum(const bignum &x)
       {
        nd=x.nd;
        sb=x.sb;
        int i;
        for (i=1;i<=nd;i=i+1) d[i]=x.d[i];
       }
       void input(void)
       {
        char s[MAX];
        scanf("%s",s);
        sb=1;
        nd=0;
        int i;
        for (i=strlen(s)-1;i>=0;i=i-1)
            {
             nd++;
             d[nd]=s[i]-48;
            } 
       }
       void print(void)
       {
        if (sb<0) printf("-");
        int i;
        for (i=nd;i>=1;i=i-1) printf("%d",d[i]);        
       }              
       bignum abs()
       {
        bignum r=bignum(*this);
        r.sb=1;
        return (r);
       }
       int cmp(const bignum&x)
       {
        if (sb>x.sb) return (1);        
        if (sb<x.sb) return (-1);
        if (nd>x.nd) return (sb);
        if (nd<x.nd) return (-sb);
        int i;
        for (i=nd;i>=1;i=i-1)
            {
             if (d[i]>x.d[i]) return (sb);
             if (d[i]<x.d[i]) return (-sb);
            }
        return (0);
       }
       bignum operator + (const bignum &x) // only compatible for non-negative integer
       {
        int n=max(nd,x.nd);
        int i,s,c;
        bignum res (*this);
        res=bignum();
        res.sb=1;;
        s=0;c=0;
        for (i=nd+1;i<=n;i=i+1) d[i]=0;
        //for (i=x.nd+1;i<=n;i=i+1) x.d[i]=0;
        for (i=1;i<=n;i=i+1)
            {
             s=d[i]+x.d[i]+c;
             if (s>9) c=1;
             else c=0;
             res.nd++;
             res.d[res.nd]=s%10;
            }
        if (c>0)
           {
            res.nd++;
            res.d[res.nd]=1;
           }
        return (res);
       }
       bignum operator - (const bignum &x) // only compatible for non-negative integer
       {
        int tmp=cmp(x);
        if (tmp>0)
           {
            bignum res;
            res=bignum();
            res.sb=1;
            int i,s,c;
            //for (i=x.nd+1;i<=nd;i=i+1) x.d[i]=0;
            s=0;
            c=0;                
            for (i=1;i<=nd;i=i+1)
                {
                 s=d[i]-x.d[i]-c;
                 if (s<0)
                    {
                     s=s+10;
                     c=1;
                    }
                 else c=0;
                 res.nd++;
                 res.d[res.nd]=s%10;                 
                }
            while (res.d[res.nd]==0) res.nd--;            
            return (res);
           }       
        if (tmp==0) return (bignum(0));
        if (tmp<0)
           {
            bignum res;
            res=bignum();
            int i,s,c;
            res.sb=-1;
            s=0;c=0;
            for (i=nd+1;i<=x.nd;i=i+1) d[i]=0;
            for (i=1;i<=x.nd;i=i+1)
                {
                 s=x.d[i]-d[i]-c;
                 if (s<0)
                    {
                     s=s+10;
                     c=1;
                    }
                 else c=0;
                 res.nd++;
                 res.d[res.nd]=s%10;
                }                
            while (res.d[res.nd]==0) res.nd--;
            return (res);
           }     
       }
       bignum operator * (const bignum &x) //compatible for all numbers
       {
        if ((nd==1) && (d[1]==0)) return (bignum(0));
        if ((x.nd==1) && (x.d[1]==0)) return (bignum(0));
        bignum res;
        res=bignum();
        res.sb=sb*x.sb;
        int i,j,s,c;
        for (i=1;i<=x.nd;i=i+1)
            {
             bignum tmp=bignum();
             for (j=1;j<i;j=j+1)
                 {
                  tmp.nd++;
                  tmp.d[tmp.nd]=0;                  
                 }
             s=0;c=0;
             for (j=1;j<=nd;j=j+1)
                 {
                  s=d[j]*x.d[i]+c;
                  c=s/10;
                  tmp.nd++;
                  tmp.d[tmp.nd]=s%10;                  
                 }
             while (c>0)
                   {
                    tmp.nd++;
                    tmp.d[tmp.nd]=c%10;
                    c=c/10;
                   }
             res=res+tmp;
            }
        return (res);
       }
};
bignum a,b,c;
int main(void)
{
    a.input();
    b.input();
    c=a+b;
    c.print();
    printf("\n");
    c=a-b;
    c.print();
    printf("\n");
    c=a*b;
    c.print();
    printf("\n");   
}

Code mẫu của khuc_tuan

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger a = new BigInteger(sc.next());
        BigInteger b = new BigInteger(sc.next());
        System.out.println(a.add(b));
        System.out.println(a.subtract(b));
        System.out.println(a.multiply(b));
    }
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.