__int1024
手搓结构体
整合高精
(其实还可以更大 改下数组就行)
Elaina's code
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<cstdlib>
#include<algorithm>
#define base 100000
#define cut 5
#define L 1024
using namespace std;
struct __int1024{
int len;
long long num[L];
__int1024(){
len=1;
memset(num,0,sizeof(num));
}
__int1024 operator=(const __int1024 &a){
len=a.len;
for(int i=0;i<len;i++)
num[i]=a.num[i];
return *this;
}
long long &operator[](int a){
return num[a];
}
long long operator[](int a)const{
return num[a];
}
friend istream&operator>>(istream&,__int1024&);
friend ostream&operator<<(ostream&,__int1024&);
};
int operator>(const __int1024 a,const __int1024 b){
if(a.len!=b.len){
if(a.len>b.len){
return 1;
}
return 0;
}
for(int i=a.len-1;i>=0;i--)
if(a[i]!=b[i]){
if(a[i]>b[i]){
return 1;
}
return 0;
}
return 0;
}
int operator==(const __int1024 a,const __int1024 b){
if(a.len!=b.len)
return 0;
for(int i=0;i<a.len;i++)
if(a[i]!=b[i])
return 0;
return 1;
}
int operator<(const __int1024 a,const __int1024 b){
if(a>b||a==b)return 0;
return 1;
}
__int1024 operator+(__int1024 a,__int1024 b){
__int1024 ret;
long long carry=0;
for(int i=0;;i++){
ret[i]=a[i]+b[i]+carry;
carry=ret[i]/base;
ret[i]%=base;
if(i>=a.len&&i>=b.len&&carry==0)
break;
}
ret.len=min(L,max(a.len,b.len)+10);
while(ret.len>0&&ret[ret.len-1]==0)
ret.len--;
if(ret.len==0)
ret.len=1;
return ret;
}
__int1024 operator+(__int1024 a,int b){
long long carry=b;
for(int i=0;;i++){
a[i]+=carry;
carry=a[i]/base;
a[i]%=base;
if(a[i]==0&&carry==0&&i>=a.len)
break;
}
a.len=min(L,a.len+10);
while(a.len>0&&a[a.len-1]==0)
a.len--;
return a;
}
__int1024 operator*(__int1024 a,__int1024 b){
__int1024 ret;
for(int i=0;i<a.len;i++)
for(int j=0;j<b.len;j++)
ret[i+j]+=a[i]*b[j];
long long carry=0;
for(int i=0;;i++){
ret[i]+=carry;
carry=ret[i]/base;
ret[i]%=base;
if(ret[i]==0&&carry==0&&i>=a.len+b.len-1)
break;
}
a.len=min(L,a.len+b.len+10);
while(a.len>0&&a[a.len-1]==0)
a.len--;
return a;
}
__int1024 operator*(__int1024 a,int b){
long long carry=0;
for(int i=0;;i++){
carry+=a[i]*b;
a[i]=carry%base;
carry/=base;
if(carry==0&&a[i]==0&&i>=a.len)
break;
}
a.len=min(L,a.len+10);
while(a.len>0&&a[a.len-1]==0)
a.len--;
return a;
}
__int1024 operator-(__int1024 a,__int1024 b){
long long carry=0;
for(int i=0;;i++){
a[i]-=b[i]+carry;
if(a[i]<0)
carry=(-a[i]/base+1);
else carry=0;
a[i]+=carry*base;
if(carry==0&&i>=b.len)
break;
}
while(a.len>0&&a[a.len-1]==0)
a.len--;
return a;
}
__int1024 operator-(__int1024 a,int b){
long long carry=b;
for(int i=0;;i++){
a[i]-=carry;
if(a[i]<0)
carry=(-a[i]/base+1);
else carry=0;
a[i]+=carry*base;
if(carry==0)
break;
}
while(a.len>0&&a[a.len-1]==0)
a.len--;
return a;
}
__int1024 operator/(__int1024 a,int b){
long long carry=0;
for(int i=a.len-1;i>=0;i--){
a[i]+=carry*base;
carry=a[i]%b;
a[i]/=b;
}
while(a.len>0&&a[a.len-1]==0)
a.len--;
return a;
}
__int1024 operator%(const __int1024 a,int b){
return a-(a/b)*b;
}
istream& operator>>(istream & in,__int1024 &b){
char ch[L*cut+5];
in>>ch;
int l=strlen(ch);
int count=0,sum=0,r=0;
for(int i=0;i<l;i++)
if(ch[i]!='0'){
r=i;
break;
}
if(r==0&&ch[r]=='0')
r=l-1;
for(int i=l-1;i>=r;){
sum=0;
int t=1;
for(int j=0;j<cut&&i>=0;j++,i--,t*=10)
sum+=(ch[i]-'0')*t;
b[count]=sum;
count++;
}
b.len=count;
return in;
}
ostream& operator<<(ostream& out,__int1024& b){
printf("%lld",b[b.len-1]);
for(int i=b.len-2;i>=0;i--){
cout.width(cut);
cout.fill('0');
printf("%lld",b[i]);
}
return out;
}
int main(){
__int1024 a,b;
cin>>a>>b;
a=a+b;
cout<<a;
}