Micro_Seven @ 2020-01-02 12:28:54
#include<bits/stdc++.h>
using namespace std;
string itos(long long n)
{
stringstream newstr;
newstr<<n;
return newstr.str();
}
string llutos(unsigned long long x)
{
string str="";
if(x<0)
{
str="-";
x=-x;
}
while(x>0)
{
str.insert(0,"0");
str.at(0)=x%10+'0';
x/=10;
}
return str;
}
char compare(string a,string b)
{
if(a.length()>b.length())return '>';
else if(a.length()<b.length())return '<';
else
{
for(__int128 i=0;i<a.length();i++)
{
if(a.at(i)>b.at(i))return '>';
else if(a.at(i)<b.at(i))return '<';
}
return '=';
}
}
string addition(string a,string b)
{
__int128 i;
int count=0;
string ans;
if(a.length()<b.length())
{
for(i=0;i<a.length()-b.length();i++)a.insert(0,"0");
}
else
{
for(i=0;i<b.length()-a.length();i++)b.insert(0,"0");
}
for(i=0;i<a.length();i++)ans.insert(0,"0");
for(i=a.length()-1;i>=0;i--)
{
if((a.at(i)-'0')+(b.at(i)-'0')+count<=9)
{
ans.at(i)+=(a.at(i)-'0')+(b.at(i)-'0')+count;
count=0;
}
else
{
ans.at(i)+=(a.at(i)-'0')+(b.at(i)-'0')+count-10;
count=1;
}
}
if(count==1)ans.insert(0,"1");
while(ans.at(0)=='0'&&ans.length()>1)ans.replace(0,1,"");
return ans;
}
string mulplication(string a,string b)
{
unsigned long long itrs;
long long i,j,k;
string ans="0",strs;
if(compare(a,b)=='>')swap(a,b);
long long a_len=a.length(),b_len=b.length();
for(i=a_len-1;i>=0;i--)
{
for(j=b_len-1;(j+1)/16>0;j-=16)
{
itrs=0;
for(k=j;k>=j-15;k--)
{
itrs+=(a.at(i)-'0')*(b.at(k)-'0')*(long long)pow(10,j-k);
}
strs=llutos(itrs);
for(k=0;k<(a_len-i-1)+(b_len-j-1);k++)
strs.append("0");
ans=addition(ans,strs);
}
itrs=0;
for(k=j;k>=0;k--)
{
itrs+=(a.at(i)-'0')*(b.at(k)-'0')*(long long)pow(10,j-k);
}
strs=llutos(itrs);
for(k=0;k<(a_len-i-1)+(b_len-j-1);k++)
strs.append("0");
ans=addition(ans,strs);
}
return ans;
}
int main()
{
string a,b;
cin>>a>>b;
cout<<mulplication(a,b);
return 0;
}
by zr太弱了 @ 2020-01-02 12:29:20
%%%
by 百因必有AC @ 2020-01-02 12:48:42
为什么你们写的高精都那么复杂
by JonuarlReden @ 2020-01-02 12:49:52
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
by zr太弱了 @ 2020-01-02 12:53:16
python
by 樱初音斗橡皮 @ 2020-01-02 17:26:02
@zr太弱了 宁是py小鬼
by 樱初音斗橡皮 @ 2020-01-02 17:26:16
@百因必有AC 压位
by panyf @ 2020-01-02 17:50:02
@Micro_Seven
我以前交的代码,没压位,28msAC
#include<bits/stdc++.h>
using namespace std;
string a,b;
int x[9999],y[9999],z[9999],A,B,i,m,j;
main(){
cin>>a>>b,A=a.size(),B=b.size();
for(i=0;i<A;i++)x[A-i]=a[i]-48;
for(i=0;i<B;i++)y[B-i]=b[i]-48;
for(i=1;m=0,i<=A;z[i+B]=m,i++)for(j=1;j<=B;j++)z[i+j-1]+=x[i]*y[j]+m,m=z[i+j-1]/10,z[i+j-1]%=10;
B+=A;
while(z[B]==0&&B>1)B--;
for(i=B;i>0;i--)cout<<z[i];
}
by Micro_Seven @ 2020-01-03 17:51:42
@AK新手村 数组有长度限制。。。我喜欢无限的string
by panyf @ 2020-01-03 17:54:53
@Micro_Seven 你可以考虑用vector,也是无限长度的
by Micro_Seven @ 2020-01-03 17:56:24
@AK新手村 好主意