結果

問題 No.1618 Convolution?
ユーザー mugen_1337mugen_1337
提出日時 2021-07-22 21:36:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 446 ms / 2,000 ms
コード長 3,720 bytes
コンパイル時間 3,967 ms
コンパイル使用メモリ 211,124 KB
実行使用メモリ 46,808 KB
最終ジャッジ日時 2023-09-24 16:01:43
合計ジャッジ時間 12,483 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 405 ms
44,896 KB
testcase_03 AC 423 ms
46,176 KB
testcase_04 AC 214 ms
26,232 KB
testcase_05 AC 24 ms
6,028 KB
testcase_06 AC 423 ms
45,272 KB
testcase_07 AC 412 ms
45,268 KB
testcase_08 AC 217 ms
26,364 KB
testcase_09 AC 431 ms
46,664 KB
testcase_10 AC 422 ms
44,144 KB
testcase_11 AC 437 ms
46,092 KB
testcase_12 AC 432 ms
46,808 KB
testcase_13 AC 445 ms
46,704 KB
testcase_14 AC 431 ms
46,796 KB
testcase_15 AC 434 ms
46,776 KB
testcase_16 AC 446 ms
46,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

struct FastFourierTransform{
    using Real = long double;
    using C=complex<Real>;
    const Real PI=acosl(-1);
    int base;
    vector<C> rts;
    vector<int> rev;
 
    FastFourierTransform():base(1){
        rts.emplace_back(0,0);
        rts.emplace_back(1,0);
        rev.push_back(0);
        rev.push_back(1);
    }
 
    void ensure_base(int nbase){
        if(nbase<=base) return;
        rev.resize(1<<nbase);
        rts.resize(1<<nbase);
        for(int i=0;i<(1<<nbase);i++) {
            rev[i]=(rev[i>>1]>>1)+((i&1)<<(nbase-1));
        }
        while(base<nbase) {
            Real angle=PI*2.0/(1<<(base+1));
            for(int i=1<<(base-1);i<(1<<base);i++) {
                rts[i<<1]=rts[i];
                Real angle_i=angle*(2*i+1-(1<<base));
                rts[(i<<1)+1]=C(cos(angle_i), sin(angle_i));
            }
            ++base;
        }
    }
 
    void fft(vector<C> &a,int n){
        assert((n&(n-1))==0);
        int zeros=__builtin_ctz(n);
        ensure_base(zeros);
        int shift=base-zeros;
        for(int i=0;i<n;i++) if(i<(rev[i]>>shift)) swap(a[i],a[rev[i]>>shift]);
        for(int k=1;k<n;k<<=1)for(int i=0;i<n;i+=2*k)for(int j=0;j<k;j++){
            //バタフライ演算
            C z=a[i+j+k]*rts[j+k];
            a[i+j+k]=a[i+j]-z;
            a[i+j]=a[i+j]+z;
        }
    }
    
    // C[k] = sum_i A[i] * B[k-i]
    vector<long long> multiply(const vector<int> &a,const vector<int> &b) {
        int need=(int)a.size()+(int)b.size()-1;
        int nbase=1;
        while((1<<nbase)<need) nbase++;
        ensure_base(nbase);
        int sz=(1<<nbase);
        vector<C> fa(sz);
        for(int i=0;i<sz;i++){
            int x=(i<(int)a.size()?a[i]:0);
            int y=(i<(int)b.size()?b[i]:0);
            fa[i]=C(x,y);
        }
        fft(fa,sz);
        C r(0,-0.25/(sz>>1)),s(0,1),t(0.5,0);
        for(int i=0;i<=(sz>>1);i++){
            int j=(sz-i)&(sz-1);
            C z=(fa[j]*fa[j]-conj(fa[i]*fa[i]))*r;
            fa[j]=(fa[i]*fa[i]-conj(fa[j]*fa[j]))*r;
            fa[i]=z;
        }
        for(int i=0;i<(sz>>1);i++){
            C A0=(fa[i]+fa[i+(sz>>1)])*t;
            C A1=(fa[i]-fa[i+(sz>>1)])*t*rts[(sz>>1)+i];
            fa[i]=A0+A1*s;
        }
        fft(fa,sz>>1);
        vector<long long> ret(need);
        for(int i=0;i<need;i++) ret[i]=llround(i&1?fa[i>>1].imag():fa[i>>1].real());
        return ret;
    }
};

FastFourierTransform FFT;

signed main(){

    int N;cin>>N;
    vector<int> A(N+1),B(N+1);
    rep(i,N) cin>>A[i+1];
    rep(i,N) cin>>B[i+1];
    vector<int> C(N+1,0);
    rep(i,N) C[i+1]=i+1;

    auto D=FFT.multiply(A,C);
    auto E=FFT.multiply(B,C);

    for(int i=1;i<=N*2;i++) cout<<D[i]+E[i]<<(i==2*N?"\n":" ");

    return 0;
}
0