下载数据后本地1ms出答案,洛谷TLE?

P1303 A*B Problem

asd890123 @ 2024-06-06 18:23:01

#include <iostream>
#include <cstring>
#include <string>

#define maxn 4005
#define mod 10000

struct BigInterger{

    int a[maxn],len;

    int &operator[](int i){return a[i];}

     BigInterger(std::string s = "0"){

        int k = 1,j = 1;

        memset(a,0,sizeof(a));
        for (int i = s.length() - 1;i >= 0;i--){

            if (k == mod) j++,k = 1;
            a[j] += k * (s[i] - '0');
            k *= 10;

        }
        len = j;

    }

    BigInterger &operator=(std::string s){return *this = BigInterger(s);}

    void flatten(int l){

        len = l;
        for (int i = 1;i <= len;i++)
            a[i + 1] += a[i] / mod,a[i] %= mod;
        while (len > 1 && !a[len]) len--;

    }

    BigInterger operator*(BigInterger x){

        BigInterger ans;

        for (int i = 1;i <= len;i++)
            for (int j = 1;j <= x.len;j++)
                ans[i + j - 1] += a[i] * x[j];
        ans.flatten(len + x.len);

        return ans;
    }

};

std::istream &operator>>(std::istream &is,BigInterger &x){

    std::string s;

    is >> s;
    x = s;

    return is;
}

std::ostream &operator<<(std::ostream &os,BigInterger x){

    os << x[x.len];
    if (x.len == 1) return os;
    for (int i = x.len - 1;i;i--) os.width(4),os.fill('0'),os << x[i];

}

int main(){

    std::ios::sync_with_stdio(0);
    std::cin.tie(0),std::cout.tie(0);

    BigInterger a,b;

    std::cin >> a >> b;
    std::cout << a * b << '\n';

    return 0;
}

by Tx1234567 @ 2024-06-28 14:26:31

@asd890123 不用想那么复杂

#include <bits/stdc++.h>
using namespace std;
int a[2010],b[2010],c[4010];
int main(){
    int s,ss;
    string al, bl;
    cin>>al>>bl;
    if (al=="0"||bl=="0"){
        cout<<"0";
        return 0;
    }
    s = al.size();
    ss = bl.size();
    for (int i = 0;i<s;a[s-i] = al[i++]-48);
    for (int i = 0;i<ss;b[ss-i] = bl[i++]-48);
    for (int i = 0;i<s;i++){
        for (int j = 0;j<ss;j++){
            c[i+j+1]+=a[i]*b[j];
        }
    }
    int s3 = s+ss;
    for (int i = 1;i<=s3;i++){
        c[i+1]+=c[i]/10;
        c[i]%=10;
    }
    while (c[s3]==0&&s3) s3--;
    for (int i = s3;i;i--) cout<<c[i];
    return 0;
}

by Tx1234567 @ 2024-06-28 14:29:24

@asd890123

报名这个比赛吧!https://www.luogu.com.cn/contest/175848,9天后关闭报名通道


|