【HIHOCODER 1420】 Bigint Multiplication
描述
Given 2 nonnegative integers a and b, calculate a × b.
输入
One line with 2 integers a and b separated by a single space.
0 ≤ a, b ≤ 10100.
输出
The value of a × b.
样例输入
100000000000000000000 100000000000000000000
样例输出
10000000000000000000000000000000000000000
注意很多0时只要输出一个
#include <bits/stdc++.h>
#define ll long long
#define inf 1000000000
#define PI acos(-1)
#define bug puts("here")
#define REP(i,x,n) for(int i=x;i<=n;i++)
#define DEP(i,n,x) for(int i=n;i>=x;i--)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=205;
class BigNumber{
public:
char a[N];
int tot;
BigNumber(char t[]){
tot=0;
DEP(i,strlen(t)-1,0) a[tot++]=t[i];
}
BigNumber(){tot=0;}
};
BigNumber Add (BigNumber an,BigNumber bn){
int mn=max(bn.tot,an.tot),sum,add=0;
BigNumber tmp;
REP(i,0,mn-1){
sum=0;
if(i<bn.tot) sum+=bn.a[i]-'0';
if(i<an.tot) sum+=an.a[i]-'0';
sum+=add;
add=sum/10;sum%=10;
tmp.a[i]=sum+'0';
}
tmp.tot=mn;
if(add!=0) tmp.a[tmp.tot++]=add+'0';
return tmp;
}
BigNumber s1 (int s,BigNumber bn){
int sum,add=0;
BigNumber tmp;
REP(i,0,bn.tot-1){
sum=s*(bn.a[i]-'0')+add;
add=sum/10;sum%=10;
tmp.a[i]=sum+'0';
}
tmp.tot=bn.tot;
if(add!=0) tmp.a[tmp.tot++]=add+'0';
return tmp;
}
BigNumber multi (BigNumber an,BigNumber bn){
BigNumber c1,t;
int sum,add;
REP(i,0,an.tot-1){
t=s1(an.a[i]-'0',bn);
if(i!=an.tot-1){
DEP(j,bn.tot-1,0) bn.a[j+1]=bn.a[j];
bn.a[0]='0';
bn.tot++;
}
c1=Add(c1,t);
}
return c1;
}
char t1[N],t2[N];
int main(){
// while(1)
{
cin>>t1>>t2;
BigNumber ans=multi(BigNumber(t1),BigNumber(t2));
bool flag=false;
REP(i,0,ans.tot-1) if(ans.a[i]!='0') {flag=true;break;}
if(!flag) {puts("0");return 0;}
DEP(i,ans.tot-1,0) printf("%c",ans.a[i]);
puts("");
}
return 0;
}