80分超时求助

P1303 A*B Problem

yuyuelong @ 2023-10-22 16:52:16

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
char s1[2005], s2[2005];
int a[2005], b[2005], c[2005];
int main() {
    size_t la, lb, lc;
    scanf("%s", s1);
    scanf("%s", s2);
    la = strlen(s1);
    lb = strlen(s2);
    for (int i = 0; i < la; i++) {
        a[la-i] = s1[i]-'0';
    }
    for (int i = 0; i < lb; i++) {
        b[lb-i] = s2[i]-'0';
    }
    lc = la+lb;
    for(int i=1;i<=la;i++){
        for(int j=1;j<=lb;j++){
            c[i+j-1]+=a[i]*b[j];
            c[i+j]+=c[i+j-1]/10;
            c[i+j-1]=c[i+j-1]%10;
        }
    }
    while (c[lc] == 0 && lc != 1) {
        lc--;
    }
    for (size_t i = lc; i >0; i--) {
        cout << c[i];
    }
    return 0;
}

最后一个计分点总是超时,求大佬给个优化的办法


by AAA404 @ 2023-10-22 16:53:44

用FFT(大雾)


by yuyuelong @ 2023-10-22 17:01:15

@AAA404 ???


by Bingxiu @ 2023-10-22 17:10:59

@yuyuelong 你的 c 数组开小了,至少开 4002


by yuyuelong @ 2023-10-22 17:13:11

@Bingxiu 谢谢大佬,过了,但能给我这个小白解释一下为什么数组开小了不是错误而是超时吗


by Bingxiu @ 2023-10-22 17:18:17

@yuyuelong 因为访问越界会导致未知错误


by yuyuelong @ 2023-10-22 20:15:24

@Bingxiu 感谢


|