using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace nrmari
{
class numarmare
{
private int n;
private int[] a;
private int semn;
public int N
{
get
{
return n;
}
}
public numarmare()
{
n = 1;
a = new int[10000];
a[0] = 0;
semn = 1;
}
public numarmare(string s)
{
a = new int[10000];
if (s[0] == '-')
{
semn = -1;
n = s.Length - 1;
for (int i = 1; i < s.Length; i++)
a[n - i] = s[i]-48;
}
else
{
semn = 1;
n = s.Length;
for (int i = 0; i < n; i++)
a[n - 1 - i] = s[i]-48;
}
}
public numarmare(numarmare x)
{
n = x.n;
semn = x.semn;
a = new int[10000];
for (int i = 0; i < n; i++)
a[i] = x.a[i];
}
public override string ToString()
{
string s;
if (semn == -1) s = "-";
else s = "";
for (int i = n - 1; i >= 0; i--)
s = s + a[i];
return s;
}
public static numarmare modul(numarmare x)
{
numarmare s = new numarmare(x);
s.semn = 1;
return s;
}
public static bool operator ==(numarmare x, numarmare y)
{
if (x.semn != y.semn) return false;
if (x.n != y.n) return false;
for (int i = 0; i < x.n; i++)
if (x.a[i] != y.a[i]) return false;
return true;
}
public static bool operator !=(numarmare x, numarmare y)
{
return !(x == y);
}
public static bool operator <(numarmare x, numarmare y)
{
if (x.semn > y.semn) return false;
else if (x.semn<y.semn) return true;
if (x.semn > 0)
{
if (x.n > y.n) return false;
else if (x.n < y.n) return true;
for (int i = x.n - 1; i >= 0; i--)
if (x.a[i] > y.a[i]) return false;
else if (x.a[i] < y.a[i]) return true;
return false;
}
else
{
if (x.n < y.n) return false;
else if (x.n > y.n) return true;
for (int i = x.n - 1; i >= 0; i--)
if (x.a[i] < y.a[i]) return false;
else if (x.a[i] > y.a[i]) return true;
return false;
}
}
public static bool operator >(numarmare x, numarmare y)
{
return !(x < y || x == y);
}
public static bool operator >=(numarmare x, numarmare y)
{
return !(x < y);
}
public static bool operator <=(numarmare x, numarmare y)
{
return !(x > y);
}
public static numarmare operator +(numarmare x, numarmare y)
{
numarmare s=new numarmare();
if (x.semn * y.semn > 0)
{
if (x.n >= y.n)
{
s = new numarmare(x);
int t = 0, i;
for (i = 0; i < y.n; i++)
{
int c = s.a[i] + y.a[i] + t;
s.a[i] = c % 10; t = c / 10;
}
while (t > 0 && i < s.n)
{
int c = s.a[i] + t;
s.a[i] = c % 10; t = c / 10;
i++;
}
if (t > 0)
{
s.n++; s.a[s.n - 1] = 1;
}
}
else
{
s = new numarmare(y);
int t = 0, i;
for (i = 0; i < x.n; i++)
{
int c = s.a[i] + x.a[i] + t;
s.a[i] = c % 10; t = c / 10;
}
while (t > 0 && i < s.n)
{
int c = s.a[i] + t;
s.a[i] = c % 10; t = c / 10;
i++;
}
if (t > 0)
{
s.n++; s.a[s.n - 1] = 1;
}
}
}
else
{
if (x.semn>0) s = x - modul(y);
else s = y - modul(x);
while (s.a[s.n - 1] == 0 && s.n>1) s.n--;
}
return s;
}
public static numarmare operator -(numarmare x, numarmare y)
{
numarmare zero = new numarmare();
if (x == zero)
{
numarmare r = new numarmare(y);
if (r.semn == 1) r.semn = -1;
else r.semn = 1;
return r;
}
numarmare d = new numarmare();
if (x.semn * y.semn > 0)
{
if (x.semn > 0)
if (x > y)
{
d = new numarmare(x);
int i;
for (i = 0; i < y.n; i++)
if (d.a[i] >= y.a[i]) d.a[i] = d.a[i] - y.a[i];
else
{
d.a[i] = 10 + d.a[i] - y.a[i];
int j = i + 1;
while (d.a[j] == 0)
{
d.a[j] = 9;
j++;
}
d.a[j]--;
}
}
else
{
d = new numarmare(y);
int i;
for (i = 0; i < x.n; i++)
if (d.a[i] >= x.a[i]) d.a[i] = d.a[i] - x.a[i];
else
{
d.a[i] = 10 + d.a[i] - x.a[i];
int j = i + 1;
while (d.a[j] == 0)
{
d.a[j] = 9;
j++;
}
d.a[j]--;
}
d.semn = -1;
}
else
if (x < y)
{
d = new numarmare(x);
int i;
for (i = 0; i < y.n; i++)
if (d.a[i] >= y.a[i]) d.a[i] = d.a[i] - y.a[i];
else
{
d.a[i] = 10 + d.a[i] - y.a[i];
int j = i + 1;
while (d.a[j] == 0)
{
d.a[j] = 9;
j++;
}
d.a[j]--;
}
}
else
{
d = new numarmare(y);
int i;
for (i = 0; i < x.n; i++)
if (d.a[i] >= x.a[i]) d.a[i] = d.a[i] - x.a[i];
else
{
d.a[i] = 10 + d.a[i] - x.a[i];
int j = i + 1;
while (d.a[j] == 0)
{
d.a[j] = 9;
j++;
}
d.a[j]--;
}
d.semn = 1;
}
}
else
{
if (x.semn > 0) d = x + modul(y);
else
{
d = modul(x) + y;
d.semn = -1;
}
}
while (d.a[d.n - 1] == 0 && d.n>1) d.n--;
return d;
}
private static numarmare inmultirec(numarmare x, int c)
{
numarmare p = new numarmare(x);
int t = 0, y,i;
for (i = 0; i < p.n; i++)
{
y = c * p.a[i] + t;
p.a[i] = y % 10;
t = y / 10;
}
if (t > 0)
{
p.n++;
p.a[p.n - 1] = t;
}
return p;
}
private static numarmare inmultire10(numarmare x)
{
numarmare p = new numarmare(x);
p.n++;
for (int i = p.n - 1; i >= 1; i--)
p.a[i] = p.a[i - 1];
p.a[0] = 0;
return p;
}
public static numarmare operator *(numarmare x, numarmare y)
{
numarmare a = new numarmare();
numarmare b = new numarmare();
a = modul(x);
b = modul(y);
numarmare p = new numarmare();
numarmare s = new numarmare();
for (int i = 0; i < b.n; i++)
{
p = inmultirec(a, b.a[i]);
for (int j = 1; j <= i; j++)
p = inmultire10(p);
s = s + p;
}
if (x.semn * y.semn < 0)
s.semn = -1;
return s;
}
public static numarmare operator /(numarmare a, numarmare b)
{
numarmare c = new numarmare();
numarmare unu=new numarmare("1");
numarmare x = new numarmare();
numarmare y = new numarmare(a);
x = modul(a);
y = modul(b);
while (x >= y)
{
c = c + unu;
x = x - y;
}
if (a.semn * b.semn < 0)
c.semn = -1;
return c;
}
public static numarmare operator %(numarmare a, numarmare b)
{
numarmare x = new numarmare();
numarmare y = new numarmare(a);
x = modul(a);
y = modul(b);
while (x >= y)
{
x = x - y;
}
numarmare r = new numarmare(x);
if (a.semn * b.semn < 0)
r.semn = -1;
return r;
}
public static numarmare operator ++(numarmare x)
{
numarmare a = new numarmare(x);
numarmare unu = new numarmare("1");
numarmare zero = new numarmare();
zero.semn = -1;
a = a + unu;
if (a == zero) a.semn = 1;
return a;
}
public static numarmare operator --(numarmare x)
{
numarmare a = new numarmare(x);
numarmare unu = new numarmare("1");
numarmare zero = new numarmare();
a = a - unu;
if (a == zero) a.semn = -1;
return a;
}
}
}
|