提交记录 17199


用户 题目 状态 得分 用时 内存 语言 代码长度
whatever 1002i. 【模板题】多项式乘法 Accepted 100 51.723 ms 19064 KB C++ 2.78 KB
提交时间 评测时间
2021-12-08 12:49:43 2021-12-08 12:49:47
#include<iostream>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long
const int mod=998244353;
inline int qpow(int a,int b){
    int ret=1;
    while(b){
        if(b&1) ret=ret*a%mod;
        a=a*a%mod;b>>=1;
    }return ret;
}
inline void up(int &x){
    if(x>=mod) x-=mod;
}
int *rev[23],*wn[23];
inline void init(){
    for(int mid=1,lg=0;mid<(1<<20);mid<<=1,lg++){
        wn[lg]=new int[mid];
        wn[lg][0]=1;
        int w=qpow(3,(mod-1)/(mid<<1));
        for(int k=1;k<mid;k++){
            wn[lg][k]=wn[lg][k-1]*w%mod;
        }
    }
}
struct poly{
    #define rep(i,l,r) for(int i=l;i<=r;i++)
    vector<int> a;
    inline poly(){}
    inline poly(int n){a.resize(n+1);}
    
    inline void ntt(int lim){
        a.resize(lim);
        int len=__lg(lim);
        if(rev[len]==nullptr){
            rev[len]=new int [lim];
            for(int i=1;i<lim;i++) rev[len][i]=rev[len][i>>1]>>1|((i&1)?(lim>>1):0);
        }
        //cerr<<"! lim : "<<lim<<endl;
        for(int i=0;i<lim;i++) if(rev[len][i]<i) swap(a[rev[len][i]],a[i]);
        for(int mid=1,lg=0;mid<lim;mid<<=1,lg++){
            for(int j=0;j<lim;j+=(mid<<1)){
                for(int k=0;k<mid;k++){
                    int x=a[j+k],y=a[j+k+mid]*wn[lg][k]%mod;
                    a[j+k]=(x+y);if(a[j+k]>=mod) a[j+k]-=mod;
                    a[j+k+mid]=(x-y);if(a[j+k+mid]<0) a[j+k+mid]+=mod;
                }
            }
        }
    }
    inline void intt(int lim){
        a.resize(lim);
        reverse(a.begin()+1,a.end());
        ntt(lim);
        int inv=qpow(lim,mod-2);
        for(int i=0;i<lim;i++) a[i]=a[i]*inv%mod;
    }
    inline int& operator [](int x){
        return a[x];
    }
    inline poly operator +(const poly&b){
        int lim=max(a.size(),b.a.size());
        poly c(lim-1);
        rep(i,0,lim-1) c[i]=(a[i]+b.a[i]),up(c[i]);
        return c;
    }
    inline poly operator -(const poly&b){
        int lim=max(a.size(),b.a.size());
        poly c(lim-1);
        rep(i,0,lim-1) c[i]=(a[i]-b.a[i]+mod),up(c[i]);
        return c;
    }
    inline poly operator *(const poly&b){
        poly c=b,d=(*this);
        int len=(int)(c.a.size())-1+(int)(d.a.size())-1;
        int lim=1;
        while(lim<=len) lim<<=1;//lim -> x^{lim-1}
        d.ntt(lim),c.ntt(lim);
        rep(i,0,lim-1) c[i]=c[i]*d[i]%mod;//,cerr<<c[i]<<" ";cerr<<endl;
        //cerr<<"OPK"<<endl;
        c.intt(lim);
        c.a.resize(len+1);
        return c;
    }
};
int n,m;
int32_t main(){
    ios::sync_with_stdio(false);
    //cerr<<n<<" "<<m<<" "<<(rev[0]==nullptr)<<endl;
    cin>>n>>m;
    //cerr<<n<<" "<<m<<" "<<(rev[0]==nullptr)<<endl;
    init();
    poly a(n),b(m);
    for(int i=0;i<=n;i++) cin>>a[i];
    for(int i=0;i<=m;i++) cin>>b[i];
    poly c=a*b;
    for(auto x:c.a) cout<<x<<" ";
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #14.164 ms8 MB + 60 KBAcceptedScore: 0

Subtask #1 Testcase #251.676 ms18 MB + 552 KBAcceptedScore: 0

Subtask #1 Testcase #326.376 ms12 MB + 892 KBAcceptedScore: 100

Subtask #1 Testcase #426.233 ms12 MB + 880 KBAcceptedScore: 0

Subtask #1 Testcase #54.166 ms8 MB + 60 KBAcceptedScore: 0

Subtask #1 Testcase #64.164 ms8 MB + 60 KBAcceptedScore: 0

Subtask #1 Testcase #74.164 ms8 MB + 60 KBAcceptedScore: 0

Subtask #1 Testcase #847.246 ms17 MB + 768 KBAcceptedScore: 0

Subtask #1 Testcase #947.2 ms17 MB + 768 KBAcceptedScore: 0

Subtask #1 Testcase #1042.948 ms16 MB + 984 KBAcceptedScore: 0

Subtask #1 Testcase #1151.723 ms18 MB + 632 KBAcceptedScore: 0

Subtask #1 Testcase #1248.737 ms17 MB + 512 KBAcceptedScore: 0

Subtask #1 Testcase #134.163 ms8 MB + 60 KBAcceptedScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:22:15 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠