压位高精度模板
By ZYStream
C++#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
static const int N=1e4+5;
struct BN{
static const int BIT=9;
static const int P=1e9;
ll a[N],sgn; // a[0] is the length of a, sgn is the sign of the number
//init
BN(){
memset(a,0,sizeof(a));
a[0]=sgn=1;
}
void init(){
memset(a,0,sizeof(a));
a[0]=sgn=1;
}
//IO
void read(){
char s[N];
scanf("%s",s);
if (s[0]=='-'){
sgn=-1;
for (int i=0;s[i];i++) s[i]=s[i+1];
}
*this=s;
}
void write(){
if(sgn==-1) putchar('-');
printf("%lld",a[a[0]]);
for(int i=a[0]-1;i>=1;i--) printf("%09lld",a[i]);
}
//assign
BN operator=(const char *num){
ll l=strlen(num);
a[0]=0;
for (int i=l-1;i>=0;i-=BIT){
a[0]++;
ll w=1;
for (int j=i;j>=i-BIT+1&&j>=0;j--){
a[a[0]]+=w*(num[j]-'0');
w=(w<<3)+(w<<1);
}
}
return *this;
}
BN operator=(const ll num){
char a[N];
sprintf(a,"%lld",num);
*this=a;
return *this;
}
BN (ll num) {*this=num;}
BN (const char *num) {*this=num;}
//abs
BN abs() const{
BN c=*this;
c.sgn=1;
return c;
}
//compare
bool operator==(const BN &b) {
if (sgn!=b.sgn) return false;
if (a[0]!=b.a[0]) return false;
for(int i=1;i<=a[0];i++) if(a[i]!=b.a[i]) return false;
return true;
}
bool operator!=(const BN &b) {return !(*this==b);}
bool operator<(const BN &b) {
if (sgn!=b.sgn) return sgn<b.sgn;
if (a[0]!=b.a[0]) return a[0]<b.a[0];
for(int i=a[0];i>=1;i--){
if(a[i]!=b.a[i]) return a[i]<b.a[i];
}
return false;
}
bool operator>(const BN &b) {
if (sgn!=b.sgn) return sgn>b.sgn;
if (a[0]!=b.a[0]) return a[0]>b.a[0];
for(int i=a[0];i>=1;i--){
if(a[i]!=b.a[i]) return a[i]>b.a[i];
}
return false;
}
bool operator<=(const BN &b) {return *this<b||*this==b;}
bool operator>=(const BN &b) {return *this>b||*this==b;}
//clac
BN operator+(const BN &b) const{
if (sgn!=b.sgn) return *this-(-b);
BN c;
c.a[0]=max(a[0],b.a[0])+1;
c.sgn=sgn;
for (int i=1;i<=c.a[0];i++){
c.a[i]+=a[i]+b.a[i];
if (c.a[i]>=P){
c.a[i+1]+=c.a[i]/P;
c.a[i]%=P;
}
}
while (c.a[c.a[0]]==0&&c.a[0]>1) c.a[0]--;
if (c.a[0]==1&&c.a[1]==0) c.sgn=1;
return c;
}
BN operator+=(const BN &b) {
*this=*this + b;
return *this;
}
BN operator-() const{
BN c=*this;
c.sgn*=-1;
return c;
}
BN operator-(const BN &b) const{
if (sgn!=b.sgn) return *this+(-b);
if (abs()<b.abs()) return -(b-(*this));
BN c;
c.a[0]=max(a[0],b.a[0]);
c.sgn=sgn;
for (int i=1;i<=c.a[0];i++){
c.a[i]+=a[i]-b.a[i];
if (c.a[i]<0){
c.a[i]+=P;
c.a[i+1]--;
}
}
while (c.a[c.a[0]]==0&&c.a[0]>1) c.a[0]--;
if (c.a[0]==1&&c.a[1]==0) c.sgn=1;
return c;
}
BN operator-=(const BN &b) {
*this=*this - b;
return *this;
}
BN operator*(const BN &b){
BN c;
c.a[0]=a[0]+b.a[0];
c.sgn=sgn*b.sgn;
for (int i=1;i<=a[0];i++){
for (int j=1;j<=b.a[0];j++){
c.a[i+j-1]+=a[i]*b.a[j];
if (c.a[i+j-1]>=P){
c.a[i+j]+=c.a[i+j-1]/P;
c.a[i+j-1]%=P;
}
}
}
while (c.a[c.a[0]]==0&&c.a[0]>1) c.a[0]--;
if (c.a[0]==1&&c.a[1]==0) c.sgn=1;
return c;
}
BN operator*=(const BN &b) {
*this=*this * b;
return *this;
}
BN operator<<(const int &b) {
a[0]++;
for (int i=1;i<=a[0];i++){
a[i]<<=b;
if (a[i-1]>=P) {
a[i-1]-=P;
a[i]++;
}
}
while (a[a[0]]==0&&a[0]>1) a[0]--;
return *this;
}
BN operator<<=(const int &b) {
*this=*this<<b;
return *this;
}
BN operator>>(const int &b){
for (int i=a[0];i>=1;i--){
if ((a[i]&1)&&(i>1)) a[i-1]+=P;
a[i]>>=b;
}
while (a[a[0]]==0&&a[0]>1) a[0]--;
if (a[0]==1&&a[1]==0) sgn=1;
return *this;
}
BN operator>>=(const int &b) {
*this=*this>>b;
return *this;
}
BN operator/(const BN &b) const{
BN c=abs(),k=b.abs();
BN t,lt;
t.a[1]=1;
while (c>=k) k<<=1,t<<=1;
while (t.a[0]>1||t.a[1]){
if (c>=k) c-=k,lt+=t;
k>>=1,t>>=1;
}
c=lt;
c.sgn=sgn*b.sgn;
while (c.a[c.a[0]]==0&&c.a[0]>1) c.a[0]--;
if (c.a[0]==1&&c.a[1]==0) c.sgn=1;
return c;
}
BN operator/=(const BN &b) {
*this=*this/b;
return *this;
}
BN operator%(const BN &b) const{
BN c=*this/b;
return *this-c*b;
}
BN operator%=(const BN &b) {
*this=*this%b;
return *this;
}
};
int main(){
BN a,b,c;
a.read();
b.read();
//add test
c=a+b;
printf("a+b=");
c.write();
puts("");
// sub test
c=a-b;
printf("a-b=");
c.write();
puts("");
// mul test
c=a*b;
printf("a*b=");
c.write();
puts("");
// div test
c=a/b;
printf("a/b=");
c.write();
puts("");
// mod test
c=a%b;
printf("a%%b=");
c.write();
puts("");
return 0;
}