#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int base = 1e8;
const int N = 1001;
int aux[N];//储存原始数据
class HighPrecision{//高精度压位模板
private:
int p[N], sz;//类的私有成员
public://区域一:
void cin() {//读入函数
this -> clear();//清空对象
memset(aux, 0, sizeof(aux));
int tot = 0,x = 0, y, k = 1;
char ch = getchar();
int fu = 1; //符号判断
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') fu *= -1;
for (; ch >= '0' && ch <= '9'; ch = getchar()){
if(tot==1&&aux[tot]==0)aux[tot] = ch - '0';
else aux[++tot] = ch - '0';//处理前缀零和读入零的情况
}
sz = (tot + 7) / 8;//计算位数
y = (tot - 1) % 8 + 1;//最高位标记
for (int i = tot; i >= y; i -= 8, x=0){
for(int j=7;j>=0;--j){//压位(亿进制)
if(i <= j)continue;//最高位处理
x = x * 10 + aux[i - j];
}
p[k++] = x;//对每一位进行储存
}
p[sz] *= fu;//标记符号
}
void cout(bool u = false){//读出函数
if(sz == 0) { printf("inf"); return;}//异常状况
printf("%d", p[sz]);//先输出首位带符号
for(int i = sz - 1; i; --i)
printf("%08d", p[i]);
if(u) printf("\n");//换行
}
public://区域二:
HighPrecision operator = (const int &d){//重载= (int)
int b = d;
int fu = 1;//符号判断
if(b < 0) b *= -1, fu = -1;
this -> clear();//清空对象
do {//用do~while处理d为零的状况
p[++sz] = b % base;
b /= base;
} while(b);
p[sz] *= fu;
return *this;
}
HighPrecision operator = (const LL &d){//重载= (long long)
LL b = d;
int fu = 1;//符号判断
if (b < 0) b *= -1, fu = -1;
this -> clear();//清空对象
do {//用do~while处理d为零的状况
p[++sz] = b % base;
b /= base;
} while(b);
p[sz] *= fu;//标记符号
return *this;
}
HighPrecision operator + (const int &d){//重载+ (int)
LL x = d;
int fu = 1;//符号判断
HighPrecision c;
if (p[sz] < 0 && x < 0) p[sz] *= -1, x *= -1, fu = -1;
else if (p[sz] < 0) {p[sz] *= -1; return (c = x) - *this;}
else if (x < 0) {x *= -1; return *this - x;}
for (int i = 1; i <= sz && x; ++i) {//进位计算
x = x + p[i];
p[i] = x % base;
x = x / base;
}
if (x) p[++sz] = x;//进位 位数+1
p[sz] *= fu;
return *this;
}
HighPrecision operator + (const LL &d){//重载+ (long long)
LL x = d;
int fu = 1;//符号判断
HighPrecision c;
if (p[sz] < 0 && x < 0) p[sz] *= -1, x *= -1, fu = -1;
else if (p[sz] < 0) {p[sz] *= -1; return (c = x) - *this;}
else if (x < 0) {x *= -1; return *this - x;}
for (int i = 1; i <= sz && x; ++i) {//进位计算
x = x + p[i];
p[i] = x % base;
x = x / base;
}
if (x) p[++sz] = x;//进位 位数+1
p[sz] *= fu;
return *this;
}
HighPrecision operator + (const HighPrecision &d){//重载+ (HighPrecision)
HighPrecision c, a = *this, b = d;
int fu = 1;//符号判断
if (a.p[a.sz] < 0 && b.p[b.sz] < 0) a.p[a.sz] *= -1, b.p[b.sz] *= -1, fu = -1;
else if (a.p[a.sz] < 0) {a.p[a.sz] *= -1; return b - a;}
else if (b.p[b.sz] < 0) {b.p[b.sz] *= -1; return a - b;}
if (a < b) a.swap(b);//a为大值,方便计算
if (b.sz < 3) {c = a + b.toLL(); c.p[c.sz] *= fu; return c;}//节省时间
LL x = 0;
for(int i = 1; i <= a.sz; ++i) {
x = x + a.p[i] + b.p[i];
c.p[i] = x % base;
x = x / base;
}
c.sz = a.sz;
if (x) c.p[++c.sz] = x;//进位 位数+1
c.p[c.sz] *= fu;//标记符号
return c;
}
HighPrecision operator - (const HighPrecision &d){//重载- (HighPrecision)
HighPrecision a = *this, c, b = d;
int fu = 1;//符号判断
if(a.p[a.sz] < 0 && b.p[b.sz] < 0) a.p[a.sz] *= -1, b.p[b.sz] *= -1, fu *= -1;
else if (a.p[a.sz] < 0) {a.p[a.sz] *= -1;c = a + b; c.p[c.sz] *= -1; return c;}
else if (b.p[b.sz] < 0) {b.p[b.sz] *= -1; return a + b;}
LL x = 1, k = 0;
if(a<b) a.swap(b), fu *= -1;
if(a == b)return c = 0;//特殊情况直接输出
for(int i = 1; i <= a.sz; ++i){//借位计算
x = x + a.p[i] + base -1 - b.p[i];
c.p[i] = x % base;
x = x / base;
}
for(c.sz = a.sz; c.p[c.sz] == 0; --c.sz);//计算c.sz
c.p[c.sz] *= fu;//符号标记
return c;
}
HighPrecision operator - (const int &d){//重载- (int)
HighPrecision c;
int b = d;
int fu = 1;//符号判断
if(p[sz] < 0 && b < 0) p[sz] *= -1, b *= -1, fu *= -1;
else if (p[sz] < 0) {p[sz] *= -1;c = *this + b; c.p[c.sz] *= -1; return c;}
else if (b < 0) {b *= -1; return *this + b;}
c=*this - (c = b);
c.p[c.sz] *= fu;//符号标记
return c;
}
HighPrecision operator - (const LL &d){//重载- (long long)
HighPrecision c;
LL b = d;
int fu = 1;//符号判断
if(p[sz] < 0 && b < 0) p[sz] *= -1, b *= -1, fu *= -1;
else if (p[sz] < 0) {p[sz] *= -1;c = *this + b; c.p[c.sz] *= -1; return c;}
else if (b < 0) {b *= -1; return *this + b;}
c=*this - (c = b);
c.p[c.sz] *= fu;//符号标记
return c;
}
HighPrecision operator * (const int &d){//重载* (int)
HighPrecision a=*this,c;
int b = d;
int fu = 1;//符号判断
if(a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if(b < 0) b *= -1, fu *= -1;
LL x = 0;
if(b == 0||a.sz == 1 && a.p[a.sz] == 0) return c = 0;
for(int i = 1; i <= a.sz; ++i){
x = x + 1ll * a.p[i] * b;
c.p[i] = x % base;
x = x / base;
}
c.sz = a.sz;//计算c.sz
for(; x; x /= base){
c.p[++c.sz] = x % base;
}
c.p[c.sz] *= fu;//符号标记
return c;
}
HighPrecision operator * (const HighPrecision &d){//重载* (HighPrecision)
HighPrecision a =* this, b = d, c;
int fu = 1;//符号判断
if(a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if(b.p[b.sz] < 0) b.p[b.sz] *= -1, fu *= -1;
if(a < b) a.swap(b);
if(b.sz < 2) return a * b.toLL(fu);
if(b.sz == 1 && b.p[b.sz] == 0) return c = 0;
LL x=0;
for (int i = 1; i < a.sz + b.sz || x; ++i){//按位依次求出
int l = 1, r = b.sz, k = i;
if (i <= b.sz) r = i;
if (i > a.sz) l = i - a.sz + 1, k = a.sz;
for(int j = l; j <= r; ++j, --k){
x += 1ll * a.p[k] * b.p[j]; //注意数值范围
}
c.p[++c.sz] = x % base;
x = x / base;
}
c.p[c.sz] *= fu;//符号标记
return c;
}
HighPrecision operator * (const LL &d){//重载* (long long)
HighPrecision a=*this,c;
LL b=d;
int fu = 1;//符号判断
if(a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if(b < 0) b *= -1, fu *= -1;
if(d > 2e9) return a * (c = d);
LL x = 0;
if(b == 0||a.sz == 1 && a.p[a.sz] == 0) return c = 0;
for(int i = 1; i <= a.sz; ++i){
x = x + 1ll * a.p[i] * b;
c.p[i] = x % base;
x = x / base;
}
c.sz = a.sz;//计算c.sz
for(; x; x /= base){
c.p[++c.sz] = x%base;
}
c.p[c.sz] *= fu; //符号标记
return c;
}
HighPrecision operator / (const int &d){//重载/ (int)
HighPrecision a=*this, c;
int b=d;
int fu = 1;//判断符号
if (a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if (b < 0) b *= -1, fu *= -1;
LL x=0;
if (b == 0) return c;//除数为零 抛出异常状况
for(int i = a.sz; i; --i){
x = x * base + a.p[i];
c.p[i] = x / b;
x = x % b;
}
for(c.sz = a.sz; c.p[c.sz] == 0 && c.sz > 1; --c.sz);
c.p[c.sz] *= fu; //符号标记
return c;
}
HighPrecision operator / (const LL &d){//重载/ (long long)
HighPrecision a=*this, c;
LL b=d;
int fu = 1;//判断符号
if (a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if (b < 0) b *= -1, fu *= -1;
LL x=0;
if (b == 0) return c;//除数为零 抛出异常状况
for(int i = a.sz; i; --i){
x = x * base + a.p[i];
c.p[i] = x / b;
x = x % b;
}
for(c.sz = a.sz; c.p[c.sz] == 0 && c.sz > 1; --c.sz);
c.p[c.sz] *= fu;//符号标记
return c;
}
HighPrecision operator / (const HighPrecision &d){//重载/ (HighPrecision)
HighPrecision a=*this,b=d,c,ce;
int fu = 1;//判断符号
if (a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if (b.p[b.sz] < 0) b.p[b.sz] *= -1, fu *= -1;
if(b.sz == 1 && b.p[b.sz] == 0) return c;//除数为零 抛出异常状况
if (a < b) return c = 0;//被除数小于除数
if (a == b) return c = 1;//被除数等于除数
if (b.sz < 2) return a / b.toLL(fu);
for(int i = a.sz; i; --i){
for(int j = ++ce.sz; j > 1; --j)//模拟进位
ce.p[j] = ce.p[j-1];
ce.p[1] = a.p[i];
if (ce < b) continue;//商为零 直接跳过
int l = 0, r = base, x;
while(l <= r) {//二分查找商
int mid = l + r >> 1;
if (b * mid <= ce) {
x = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
c.p[i] = x;
ce = ce - b * x;
}
for(c.sz = a.sz; c.p[c.sz] == 0 && c.sz > 1; --c.sz);
c.p[c.sz] *= fu; //符号标记
return c;
}
HighPrecision operator % (const int &d){//重载% (int)
HighPrecision a=*this, c;
int b=d;
int fu = 1;//判断符号
if (a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if (b < 0) b *= -1, fu *= -1;
LL x=0;
if (d == 0) return c;//除数为零 抛出异常状况
for(int i = a.sz; i; --i){
x = (x * base + a.p[i]) % b;
}
return c = x * fu;
}
HighPrecision operator % (const LL &d){//重载% (long long)
HighPrecision a=*this, c;
LL b=d;
int fu = 1;//判断符号
if (a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if (b < 0) b *= -1, fu *= -1;
LL x=0;
if (d == 0) return c;//除数为零 抛出异常状况
for(int i = a.sz; i; --i){
x = (x * base + a.p[i]) % b;
}
return c = x * fu;
}
HighPrecision operator % (const HighPrecision &d){//重载% (HighPrecision)
HighPrecision a=*this,b=d,ce;
int fu = 1;//判断符号
if (a.p[a.sz] < 0) a.p[a.sz] *= -1, fu *= -1;
if (b.p[b.sz] < 0) b.p[b.sz] *= -1, fu *= -1;
if (b.sz == 1 && b.p[b.sz] == 0) return ce;//除数为零 抛出异常状况
if (a < b) return a;//被除数小于除数
if (a == b) return ce = 0;//被除数等于除数
if (b.sz < 2) return a % b.toLL(fu);
for(int i = a.sz; i; --i){
if (!(ce.sz == 1 && ce.p[ce.sz] == 0))
for(int j = ++ce.sz; j > 1; --j)//模拟进位
ce.p[j] = ce.p[j-1];
ce.p[1] = a.p[i];
if (ce < b) continue;//商为零 直接跳过
int l = 0, r = base, x;
while(l <= r) {//二分查找商
int mid = l + r >> 1;
if (ce < b * mid) {
r = mid - 1;
}
else {
x = mid;
l = mid + 1;
}
}
ce = ce - b * x;
}
ce.p[ce.sz] *= fu; //符号标记
return ce;
}
public://区域三:
HighPrecision operator += (int &b) {return *this = *this + b;}
HighPrecision operator -= (int &b) {return *this = *this - b;}
HighPrecision operator *= (int &b) {return *this = *this * b;}
HighPrecision operator /= (int &b) {return *this = *this / b;}
HighPrecision operator %= (int &b) {return *this = *this % b;}
HighPrecision operator += (LL &b) {return *this = *this + b;}
HighPrecision operator -= (LL &b) {return *this = *this - b;}
HighPrecision operator *= (LL &b) {return *this = *this * b;}
HighPrecision operator /= (LL &b) {return *this = *this / b;}
HighPrecision operator %= (LL &b) {return *this = *this % b;}
HighPrecision operator += (HighPrecision &b) {return *this = *this + b;}
HighPrecision operator -= (HighPrecision &b) {return *this = *this - b;}
HighPrecision operator *= (HighPrecision &b) {return *this = *this * b;}
HighPrecision operator /= (HighPrecision &b) {return *this = *this / b;}
HighPrecision operator %= (HighPrecision &b) {return *this = *this % b;}
bool operator < (const int &b) const {HighPrecision c;return *this < (c=b);}
bool operator <= (const int &b) const {HighPrecision c;return *this <= (c=b);}
bool operator > (const int &b) const {HighPrecision c;return *this > (c=b);}
bool operator >= (const int &b) const {HighPrecision c;return *this >= (c=b);}
bool operator == (const int &b) const {HighPrecision c;return *this == (c=b);}
bool operator != (const int &b) const {HighPrecision c;return *this != (c=b);}
bool operator < (const LL &b) const {HighPrecision c;return *this < (c=b);}
bool operator <= (const LL &b) const {HighPrecision c;return *this <= (c=b);}
bool operator > (const LL &b) const {HighPrecision c;return *this > (c=b);}
bool operator >= (const LL &b) const {HighPrecision c;return *this >= (c=b);}
bool operator == (const LL &b) const {HighPrecision c;return *this == (c=b);}
bool operator != (const LL &b) const {HighPrecision c;return *this != (c=b);}
bool operator == (const HighPrecision &b) const {if(sz^b.sz)return false;
for (int i=sz;i;i--) if(p[i] ^ b.p[i]) return false; return true ;}
bool operator != (const HighPrecision &b) const {if(sz^b.sz)return true ;
for (int i=sz;i;i--) if(p[i] ^ b.p[i]) return true ; return false;}
bool operator < (const HighPrecision &b) const {if(sz^b.sz)return sz<b.sz;
for (int i=sz;i;i--) if(p[i] ^ b.p[i]) return p[i] < b.p[i]; return false;}
bool operator <= (const HighPrecision &b) const {if(sz^b.sz)return sz<b.sz;
for (int i=sz;i;i--) if(p[i] ^ b.p[i]) return p[i] < b.p[i]; return true ;}
bool operator > (const HighPrecision &b) const {if(sz^b.sz)return sz>b.sz;
for (int i=sz;i;i--) if(p[i] ^ b.p[i]) return p[i] > b.p[i]; return false;}
bool operator >= (const HighPrecision &b) const {if(sz^b.sz)return sz>b.sz;
for (int i=sz;i;i--) if(p[i] ^ b.p[i]) return p[i] > b.p[i]; return true ;}
public://区域四:
HighPrecision() {clear();}//重载构造函数
HighPrecision(int x) {*this = x;}
HighPrecision(LL x) {*this = x;}
void clear() {sz = 0; memset(p, 0, sizeof(p));}//清空函数
void swap(HighPrecision &b) {HighPrecision c = *this; *this = b; b = c;}//交换函数
LL toLL(int fu = 1) {//化值函数
LL x = 0;
if (p[sz] < 0)
p[sz] *= -1, fu = -1;
for(int i=sz;i;--i)
x=x*base+p[i];
return x * fu;
}
//区域一:输入输出 、区域二:四则运算 、区域三:基础配置 、区域四:辅助函数
} a, b;
int main(){
a.cin(); b.cin();//输入
(a + b).cout(1); //A + B
(a - b).cout(1); //A - B
(a * b).cout(1); //A * B
(a / b).cout(1); //A / B
(a % b).cout(1); //A % B
return 0;
}
高精度压位(亿进制)模板
最新推荐文章于 2023-01-04 19:30:00 发布