码迷,mamicode.com
首页 > 其他好文 > 详细

P1303 A*B Problem(高精度乘法)

时间:2020-02-19 19:05:51      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:com   clu   str   htm   end   复杂度   tps   ret   char   

P1303 A*B Problem

模拟就好了。\(c_ {i+j} +=a_i \times b_j\).时间复杂度 \(O(n*m)\)FFT版可以做到 \(O((n+m)\log (n+m)\)

#include<bits/stdc++.h>
using namespace std;
string times(string a,string b)
{
    int aa[15000]={0},bb[15000]={0},ans[30000]={0};
    string str="";
    for(int i=0;i<a.length();i++)
        aa[a.length()-i-1]=a[i]-'0';
    for(int i=0;i<b.length();i++)
        bb[b.length()-i-1]=b[i]-'0';
    for(int i=0;i<a.length();i++)
        for(int j=0;j<b.length();j++)
        {
            ans[i+j]+=aa[i]*bb[j];
            if(ans[i+j]>9)ans[i+j+1]+=ans[i+j]/10,ans[i+j]=ans[i+j]%10;
        }
    int len=a.length()+b.length();
    while(ans[len-1]==0&&len-1>0)len--;
    for(int i=0;i<len;i++)
        str=char(ans[i]+'0')+str;
    return str;
}
int main()
{
    string x,y;
    cin>>x>>y;
    cout<<times(x,y)<<endl;
    return 0;
}

P1303 A*B Problem(高精度乘法)

标签:com   clu   str   htm   end   复杂度   tps   ret   char   

原文地址:https://www.cnblogs.com/zzctommy/p/12332472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!