题解
2024-01-27 13:37:15
发布于:四川
64阅读
0回复
0点赞
此处可用模板
#include<bits/stdc++.h>
using namespace std;
const int N=10087;
struct bign {
int len;
int x[N];
bign() {
len=1;
memset(x,0,sizeof(x));
}
};
int B;
void print(bign a) {
for(int i=a.len; i>=1; i--) {
if(a.x[i]<=9){
cout<<a.x[i];
}else{
char ggh;
ggh=a.x[i]-10+'A';
cout<<ggh;
}
}
}
bign reed(string s) {
bign a;
a.len=s.size();
for(int i=1; i<=a.len; i++) {
if(s[s.size()-i]>='A')a.x[i]=s[s.size()-i]-'A'+10;
else{
a.x[i]=s[s.size()-i]-'0';
}
}
return a;
}
bign fix(bign a) {
for(int i=1; i<=a.len-1; i++) {
while(a.x[i]<0) {
a.x[i+1]--;
a.x[i]+=B;
}
a.x[i+1]+=a.x[i]/B;
a.x[i]%=B;
}
while(a.x[a.len]>=B) {
a.x[a.len+1]+=a.x[a.len]/B;
a.x[a.len]%=B;
a.len++;
}
while(a.len>1&&a.x[a.len]==0) {
a.len--;
}
return a;
}
bign operator +(bign a,int b) {
a.x[1]+=b;
return fix(a);
}
bign operator +(bign a,bign b) {
bign c;
c.len=max(a.len,b.len)+1;
for(int i=1; i<=c.len; i++) {
c.x[i]=a.x[i]+b.x[i];
}
return fix(c);
}
bign operator -(bign a,int b) {
a.x[1]-=b;
return fix(a);
}
bign operator -(bign a,bign b) {
bign c;
c.len=a.len;
for(int i=1; i<=c.len; i++) {
c.x[i]=a.x[i]-b.x[i];
}
return fix(c);
}
bool operator <= (bign a,bign b) {
if(a.len>b.len) {
return false;
}
if(a.len<b.len) {
return true;
}
for(int i=a.len; i>=1; i--) {
if(a.x[i]>b.x[i]) {
return false;
}
if(a.x[i]<b.x[i]) {
return true;
}
}
return true;
}
bool operator >(bign a,bign b) {
if(a<=b) {
return false;
} else {
return true;
}
}
bign operator *(bign a,int b) {
for(int i=1; i<=a.len; i++) {
a.x[i]*=b;
}
return fix(a);
}
bign operator *(bign a,bign b){
bign c;
c.len=a.len+b.len;
for(int i=1;i<=a.len;i++){
for(int j=1;j<=b.len;j++){
c.x[i+j-1]+=a.x[i]*b.x[j];
c.x[i+j]+=c.x[i+j-1]/B;
c.x[i+j-1]%=B;
}
}
return fix(c);
}
bign operator /(bign a,int b){
int v=0;
bign c;
c.len=a.len;
for(int i=a.len;i>=1;i--){
v*=B;
v+=a.x[i];
c.x[i]=v/b;
v%=b;
}
return fix(c);
}
bign operator /(bign a,bign b){
bign c;
bign v;
c.len=a.len;
for(int i=a.len;i>=1;i--){
v=v*B+a.x[i];
while(!(b>v)){
c.x[i]++;
v=v-b;
}
}
return fix(c);
}
int main() {
cin>>B;
string sa,**;
cin>>sa>>**;
bign a,b;
a=reed(sa);
b=reed(**);
print(a+b);
return 0;
}
全部评论 1
笑点解析:**
2024-10-02 来自 广东
0
有帮助,赞一个