20分!

P1303 A*B Problem

tianyuanye @ 2024-07-25 21:15:48

**每个非负整数不超过10的2000次方 得加多少个long?


by lcfollower @ 2024-07-25 21:17:47

那要高精度,long long 存不下。


by zhouzihang1 @ 2024-07-25 21:18:09

@tianyuanye 高精度


by _xm_ @ 2024-07-25 21:29:14

如果压位到 2^{64} 进制只需要62位。


by _xm_ @ 2024-07-25 21:33:48

哦,没看题目,乘法需要保证 base^2 \le V


by Wang_Xu @ 2024-07-25 21:38:51

#include<iostream>
#include<vector>
#include<string> 
#include<algorithm>
using namespace std;
string mul(string s1,string s2)
{
    reverse(s1.begin(),s1.end());
    reverse(s2.begin(),s2.end());
    vector<int> s3(s1.size()+s2.size(),0);
    for(int i=0;i<s1.size();i++)
    {   
        for(int j=0;j<s2.size();j++)
        {
            int num=(s1[i]-'0')*(s2[j]-'0');
            s3[i+j]+=num;
        }   
    } 
    int x=0;
    string ans;
    for(int i=0;i<s3.size();i++)
    {
        s3[i]+=x;
        x=s3[i]/10;
        s3[i]%=10;
        ans.push_back(s3[i]+'0');
    }
    while(ans.size()>1&&ans.back()=='0') 
    {
        ans.pop_back();
    }
    reverse(ans.begin(),ans.end());
    return ans;
}
int main() 
{
    string s1,s2;
    cin>>s1>>s2;
    cout<<mul(s1,s2)<<endl;
    return 0;
}

@tianyuanye


by zhukaiting @ 2024-08-07 17:39:36

@tianyuanye 高精度了解一下?


|