提交记录 515


用户 题目 状态 得分 用时 内存 语言 代码长度
orbitingfIea 1002. 测测你的多项式乘法 Accepted 100 381.81 ms 32412 KB C 1.51 KB
提交时间 评测时间
2018-06-20 14:23:12 2020-07-31 20:39:44

#define ll long long
#define rint register int
#define swap(x,y) x^=y^=x^=y

  const int mod=998244353, gen=3;
  
  ll K(ll x,int y){
    ll t=1;
    for (;y;y>>=1,x=x*x%mod) if (y&1) t=t*x%mod;
    return t;
  }
  int up(int x){
    return x>=mod? x-mod: x;
  }
  int dn(int x){
    return x<0? x+mod: x;
  }
 
  int dp[2101000];
  void fft(int *a,int n){
    dp[0]=0;
    int i, j, b;
    for (i=1;i<(1<<n);++i){
      dp[i]= j= i&1? dp[i-1]+(1<<n>>1): dp[i>>1]>>1;
      if (i<j) swap(a[i],a[j]);
    }
    for (b=0;b<n;++b){
      int len1=1<<b, len2=1<<b+1, w0=K(gen,(mod-1)/len2);
      for (i=0;i<(1<<n);i+=len2){
        int *l=a+i, *r=a+i+len1; int w=1, tmp;
        for (j=0;j<len1;++j){
          tmp=(ll)w*r[j]%mod;
          r[j]=dn(l[j]-tmp);
          l[j]=up(l[j]+tmp);
          w=(ll)w*w0%mod;
        }
      }
    }
  }
 
  void realmain(int *a,int *b,int la,int lb){
    ++la, ++lb; int len=la+lb;
    int n=0, i, j; for (;len>(1<<n);) ++n;
    for (i=la;i<(1<<n);++i) a[i]=0;
    for (i=lb;i<(1<<n);++i) b[i]=0;
    fft(a,n); fft(b,n);
    for (i=0;i<(1<<n);++i) a[i]=(ll)a[i]*b[i]%mod;
    //reverse(a+1,a+(1<<n));
    for (i=1;;++i){
    	j=(1<<n)-i;
		if (i<j) swap(a[i],a[j]);
		else break;	
    }
    
    fft(a,n); ll inv=K(1<<n,mod-2);
    for (i=0;i<(1<<n);++i) a[i]=(ll)a[i]*inv%mod;
  }

int A[2101000], B[2101000];

void poly_multiply(unsigned *a, int n, unsigned *b, int m, unsigned *c)
{
	int i;
	for (i=0;i<=n;++i) A[i]=a[i];
	for (i=0;i<=m;++i) B[i]=b[i];
	realmain(A,B,n,m);
	for (i=0;i<=n+m;++i) c[i]=A[i];
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1381.81 ms31 MB + 668 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-23 05:34:43 | Loaded in 0 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠