近不知道哪根筋抽风居然想起写高精度,确实很恶心。。。
不过有一点好处是以后碰到高精度的恶心题就不用在写了。在网上找到的高精度模板基本只有 + - * / 4种运算,我的加上了个 % 。。。
Code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int LEN = 10001;
struct BigInt
{
static const int BIT = 9;
static const int MOD = 1000000000;//1eBIT
long long s[LEN];
bool flag;
BigInt()
{
memset(s, 0, sizeof(s));
flag = s[0] = 1;
}
void init()
{
memset(s, 0, sizeof(s));
s[0] = 1;
}
BigInt operator = (const char *num)
{
int l = strlen(num);
s[0] = 0;
for(int i = l - 1; i >= 0; i -= BIT)
{
++s[0];
long long w = 1;
for(int j = i; j > i - BIT && j >= 0; j--)
{
s[s[0]] += (num[j] ^ 48) * w;
w = (w << 1) + (w << 3);
}
}
return *this;
}
BigInt operator = (const int num)
{
char a[LEN];
sprintf(a, "%d", num);
*this = a;
return *this;
}
BigInt(int num)
{
*this = num;
}
BigInt(const char *num)
{
*this = num;
}
BigInt operator + (const BigInt &a)
{
BigInt c;
int x = 0;
c.s[0] = max(a.s[0], s[0]) + 1;
for(int i = 1; i <= c.s[0]; i++)
{
c.s[i] = a.s[i] + s[i] + x;
x = c.s[i] / MOD;
c.s[i] %= MOD;
}
while(c.s[c.s[0]] == 0 && c.s[0] > 1) c.s[0]--;
return c;
}
BigInt operator += (const BigInt &a)
{
*this = *this + a;
return *this;
}
bool operator == (const BigInt &a)
{
int up = max(s[0], a.s[0]);
for(int i = 0; i < up; i++)
if(s[up - i] != a.s[up - i]) return false;
return true;
}
bool operator > (const BigInt &a)
{
if(s[0] != a.s[0]) return s[0] > a.s[0];
int up = max(s[0], a.s[0]);
for(int i = 0; i < up; i++)
if(s[up - i] != a.s[up - i]) return s[up - i] > a.s[up - i];
return false;
}
bool operator < (const BigInt &a)
{
if(s[0] != a.s[0]) return s[0] < a.s[0];
int up = max(s[0], a.s[0]);
for(int i = 0; i < up; i++)
if(s[up - i] != a.s[up - i]) return s[up - i] < a.s[up - i];
return false;
}
bool operator >= (const BigInt &a)
{
if(*this > a || *this == a) return true;
return false;
}
bool operator <= (const BigInt &a)
{
if(*this < a || *this == a) return true;
return false;
}
BigInt operator - (const BigInt &a)
{
BigInt c;
c.s[0] = max(a.s[0], s[0]) + 1;
if(*this < a) c.flag = false;
for(int i = 1; i <= c.s[0]; i++)
{
if(c.flag) c.s[i] += s[i] - a.s[i];
else c.s[i] += a.s[i] - s[i];
if(c.s[i] < 0)
{
c.s[i] += MOD;
c.s[i + 1]--;
}
}
while(c.s[c.s[0]] == 0 && c.s[0] > 1) c.s[0]--;
return c;
}
BigInt operator -= (const BigInt &a)
{
*this = *this - a;
return *this;
}
BigInt operator * (const BigInt &a)
{
BigInt c;
c.s[0] = s[0] + a.s[0];
for(int i = 1; i <= s[0]; i++)
{
int x = 0;
for(int j = 1; j <= a.s[0]; j++)
{
c.s[i + j - 1] += s[i] * a.s[j] + x;
x = c.s[i + j - 1] / MOD;
c.s[i + j - 1] %= MOD;
}
c.s[i + a.s[0]] = x;
}
while(c.s[c.s[0]] > 0) c.s[0]++;
while(c.s[c.s[0]] == 0 && c.s[0] > 1) c.s[0]--;
return c;
}
BigInt operator *= (const BigInt &a)
{
*this = *this * a;
return *this;
}
BigInt operator << (const int &num)
{
s[0]++;
for(int i = 1; i <= s[0]; i++)
{
s[i] <<= num;
if(s[i - 1] >= MOD)
s[i - 1] -= MOD, ++s[i];
}
while(s[s[0]] == 0 && s[0] > 1) s[0]--;
return *this;
}
BigInt operator >> (const int &num)
{
for(int i = s[0]; i >= 1; i--)
{
if((s[i] & 1) && i > 1) s[i - 1] += MOD;
s[i] >>= num;
}
while(s[s[0]] == 0 && s[0] > 1) s[0]--;
return *this;
}
BigInt operator / (const BigInt &k)
{
BigInt c = *this, tmp, lt, a;
a = k;
tmp.s[1] = 1;
while(c >= a)
{
a = a << 1;
tmp = tmp << 1;
}
while(tmp.s[0] > 1 || tmp.s[1])
{
if(c >= a)
{
c -= a;
lt += tmp;
}
a = a >> 1;
tmp = tmp >> 1;
}
c = lt;
while(c.s[c.s[0]] == 0 && c.s[0] > 1) c.s[0]--;
if(c.s[0] < 1) c.s[c.s[0] = 1] = 0;
return c;
}
BigInt operator /= (const BigInt &a)
{
*this = *this / a;
return *this;
}
BigInt operator % (const BigInt &a)
{
BigInt zero=0,tmp=a;
if(tmp==zero)
return zero;
BigInt d = *this,
c = *this / a;
c *= a;
return d - c;
}
BigInt operator %= (const BigInt &a)
{
*this = *this % a;
return *this;
}
}a,b;
ostream& operator << (ostream &out, const BigInt &a)
{
if(!a.flag) putchar('-');
printf("%d", a.s[a.s[0]]);
for(int i = a.s[0] - 1; i >= 1; i--)
printf("%09d", a.s[i]);
return out;
}
istream& operator >> (istream &in, BigInt &a)
{
char str[LEN];
in >> str;
a = str;
return in;
}
int main()
{
cin >> a >> b;
cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl;
cout << a % b << endl;
return 0;
}
放在这里,主要是备忘以及需要的时候拿出来用。