整理高精模板
首先推销一下zd大佬的高精板子,比我的要全,可惜没压位。
下面是我的板子,压了8位:
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int width = 8;
const long long _mod = 1e8;
const int wei = 2000;
struct HugeInt
{
long long a[wei];
int len;
inline void clear()
{
memset(a, 0, sizeof(a));
len = 0;
}
HugeInt operator = (const string& str)
{
clear();
int x;
int llen = (str.length()-1)/width + 1;
for(int i = 0; i < llen; ++i)
{
int end = str.length() - i*width;
int start = max(0, end-width);
sscanf(str.substr(start, end-start).c_str(), "%d", &x);
a[len++] = x;
}
return* this;
}
inline bool is_odd()
{
return a[0] & 1;
}
inline bool is_even()
{
return !is_odd();
}
};
HugeInt operator + (HugeInt a, HugeInt b)
{
HugeInt c;
c.clear();
int maxlen = max(a.len, b.len);
for(int i = 0; i < maxlen; ++i)
{
c.a[i] += a.a[i]+b.a[i];
c.a[i+1] += c.a[i]/_mod;
c.a[i] %= _mod;
}
c.len = maxlen;
while(c.a[c.len])
c.len++;
return c;
}
HugeInt operator - (HugeInt a, HugeInt b)
{
HugeInt c;
c.clear();
c.len = a.len;
for(int i = 0; i < c.len; ++i)
{
c.a[i] += a.a[i]-b.a[i];
if(c.a[i]<0)
{
a.a[i+1]--;
c.a[i] += _mod;
}
}
while(!c.a[c.len-1] && c.len>=1)
c.len--;
return c;
}
HugeInt operator * (HugeInt a, HugeInt b)
{
HugeInt c;
c.clear();
for(int i = 0; i < a.len; ++i)
for(int j = 0; j < b.len; ++j)
c.a[i+j] += a.a[i] * b.a[j];
c.len = a.len + b.len - 1;
for(int i = 0; i < c.len; ++i)
{
c.a[i+1] += c.a[i]/_mod;
c.a[i] %= _mod;
}
while(c.a[c.len])
c.len++;
return c;
}
bool operator < (HugeInt a, HugeInt b)
{
if(a.len != b.len)
return a.len < b.len;
else
{
for(int i = a.len-1; i >= 0; --i)
{
if(a.a[i] != b.a[i])
return a.a[i] < b.a[i];
}
}
return false;
}
bool operator > (HugeInt a, HugeInt b)
{
return b < a;
}
bool operator <= (HugeInt a, HugeInt b)
{
return !(a>b);
}
bool operator >= (HugeInt a, HugeInt b)
{
return !(a<b);
}
bool operator != (HugeInt a, HugeInt b)
{
return a<b || b<a;
}
bool operator == (HugeInt a, HugeInt b)
{
return !(a!=b);
}
HugeInt give(long long a)
{
HugeInt re;
re.clear();
while(a)
{
re.a[re.len++] = a%_mod;
a /= _mod;
}
return re;
}
HugeInt operator + (HugeInt a, long long b)
{
return a + give(b);
}
HugeInt operator + (HugeInt a, int b)
{
return a + (long long)b;
}
HugeInt operator + (int a, HugeInt b)
{
return b + a;
}
HugeInt operator + (long long a, HugeInt b)
{
return b + a;
}
HugeInt operator * (HugeInt a, long long b)
{
return a * give(b);
}
HugeInt operator * (HugeInt a, int b)
{
return a * (long long)b;
}
HugeInt operator * (int a, HugeInt b)
{
return b * a;
}
HugeInt operator * (long long a, HugeInt b)
{
return b * a;
}
HugeInt operator - (HugeInt a, long long b)
{
return a - give(b);
}
HugeInt operator - (HugeInt a, int b)
{
return a - (long long)b;
}
HugeInt operator / (HugeInt a,long long b)
{
HugeInt ans;
ans.clear();
ans=a;
long long my=0;
for(int i=ans.len-1; i>=0; i--)
{
ans.a[i]+=my;
my=ans.a[i]%b*_mod;
ans.a[i]/=b;
}
while(!ans.a[ans.len-1])
ans.len--;
return ans;
}
HugeInt operator / (HugeInt a, int b)
{
return a / (long long)b;
}
void print(HugeInt a)
{
printf("%lld", a.a[a.len-1]);
for(int i = a.len-2; i>=0; --i)
printf("%08lld", a.a[i]);
}
istream& operator >> (istream& in, HugeInt& x)
{
string s;
if(!(in >> s)) return in;
x = s;
return in;
}
ostream& operator << (ostream& out, const HugeInt& x)
{
print(x);
return out;
}
inline HugeInt pow(HugeInt a, long long b)
{
HugeInt ans = give(1);
for(; b; b >>= 1, a = a*a)
if(b & 1)
ans = ans*a;
return ans;
}
inline HugeInt pow(HugeInt a, int b)
{
return pow(a, (long long)b);
}
inline HugeInt gcd(HugeInt x, HugeInt y)
{
int cnt = 0;
for(; x != y; )
{
if(x<y) swap(x, y);
if(x.is_even())
{
if(y.is_even())
{
x = x/2;
y = y/2;
cnt++;
}
else
x = x/2;
}
else
{
if(y.is_even())
y = y/2;
else
x = x-y;
}
}
return x * pow(give(2), cnt);
}
这个板子是准备更新的,还在整理……
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
const int maxn = 2000;
struct HugeInt
{
static const int WIDTH = 8;
static const long long WEI = 1e8;
long long a[maxn];
int length;
bool fu;
inline void init()
{
memset(a, 0, sizeof(a));
length = 0;
fu = false;
}
HugeInt operator -()
{
HugeInt tmp = *this;
tmp.fu = !tmp.fu;
return tmp;
}
friend HugeInt operator / (HugeInt a, long long b)
{
HugeInt ans;
ans.init();
if(b < 0)
{
ans.fu = true;
b = -b;
}
ans.fu ^= a.fu;
long long tmp = 0;
ans.length = a.length;
for(int i = a.length; i; --i)
{
tmp *= WEI;
tmp += a.a[i];
ans.a[i] = tmp / b;
tmp = tmp % b;
}
while(!ans.a[ans.length])
ans.length--;
return ans;
}
friend long long operator % (HugeInt a, long long b)
{
bool f = false;
if(a.fu)
f = true;
long long tmp = 0;
for(int i = a.length; i; --i)
{
tmp *= WEI;
tmp += a.a[i];
tmp = tmp % b;
}
return f ? -tmp : tmp;
}
HugeInt operator = (string s)
{
this->init();
int len = s.length();
if(s[0] == '-')
{
this->fu = true;
string tmp = s.substr(1, s.length()-1);
s = tmp;
len--;
}
this->length = len / WIDTH + (bool)(len % WIDTH);
int tt = length;
for(int i = 1; i <= len; ++i)
{
if(!((len - i + 1) % WIDTH) && i != 1)
tt--;
a[tt] = (a[tt] << 1) + (a[tt] << 3) + (s[i-1] ^ 48);
}
return *this;
}
};
inline void print(HugeInt a)
{
if(a.fu && (a.a[1] || a.length > 1))
putchar('-');
printf("%lld", a.a[a.length]);
for(int i = a.length-1; i; --i)
printf("%08lld", a.a[i]);
}
istream& operator >> (istream& in, HugeInt& a)
{
string s;
if(!(in >> s))
return in;
a = s;
return in;
}
ostream& operator << (ostream& out, const HugeInt& a)
{
print(a);
return out;
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
HugeInt a;
cin >> a;
cout << -a << endl;
return 0;
}