分治,看着貌似没错

P1303 A*B Problem

秋雨 @ 2020-02-09 13:33:04

#include<bits/stdc++.h>
using namespace std;
const int maxN=10010;
int a[maxN],b[maxN],c[2*maxN];
string a1,b1;
void dfs(int l1,int r1,int l2,int r2){
    if(l1==r1&&l2==r2){
        c[l1+l2]+=a[l1]*b[l2];
        return;
    }
    if(l1==r1){
        int mid=(l2+r2)>>1;
        dfs(l1,r1,l2,mid);
        dfs(l1,r1,mid+1,r2);
    }
    else if(l2==r2){
        int mid=(l1+r1)>>1;
        dfs(l1,mid,l2,r2);
        dfs(mid+1,r1,l2,r2);
    }
    else{
        int mid1=(l1+r1)>>1;
        int mid2=(l2+r2)>>1;
        dfs(l1,mid1,l2,mid2);
        dfs(l1,mid1,mid2+1,r2);
        dfs(mid1+1,r1,l2,mid2);
        dfs(mid1+1,r1,mid2+1,r2);
    }
}
void write(){
    int k=0;
    for(int i=0;i<maxN-1;i++){
        c[i]+=k;
        k=c[i]/10;
        c[i]%=10;
    }
    int i=maxN-1;
    while(i&&!c[i]) i--;
    while(i>=0) cout<<c[i--];
}
int main(){
    cin>>a1>>b1;
    for(int i=0;i<a1.size();i++) a[i]=a1[i]-'0';
    for(int i=0;i<b1.size();i++) b[i]=b1[i]-'0';
    dfs(0,a1.size()-1,0,b1.size()-1);
    write();
}

怎么改都不对,求大佬帮忙检查


by Monkey_Hunter @ 2020-02-09 13:38:15

py


by UltiMadow @ 2020-02-09 14:01:04

这题不是FFT裸题吗qwq


|