跳到正文

个人记录

任意奇模数多项式乘法

ExplodingKonjac约 9 分钟
原文
文章信息与版本

文章信息

发布
2024年1月24日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 9 分钟

版本

  1. v1当前

任意奇模数多项式乘法

采用了 https://www.luogu.com.cn/blog/explodingkonjac/polynomial-multiplication-on-any-algebra 中介绍的技术,并且加入了一些向量化运算优化(采用 GCC 扩展 __attribute__((vector_size)))。

与云剪贴板同步。

Introduction\textbf{Introduction}

模板类 Convolution

定义

C++
template<int MOD>
class Convolution;

模板参数

  • MOD:模数。要求必须为 [1,230)[1,2^{30}) 以内的奇数。

构造函数

  • 默认构造函数。

静态成员函数

  • static std::vector<int> conv(std::vector<int> p,std::vector<int> q):将 p,qp,q 视为多项式的系数序列,返回其乘积的系数序列,系数对 MOD\mathrm{MOD} 取模。

Example\mathbf{Example}

模板题实现:#144195451。

Code\mathbf{Code}

C++
#include <bits/stdc++.h>
// using namespace std;

namespace Math::ZPoly
{

// Convolution for any odd modular
template<int MOD>
class Convolution
{
 private:
	static constexpr int NTT_LIM=64,VSIZE=4;
	using Vec=int __attribute__((vector_size(16)));
	static_assert(sizeof(Vec)==VSIZE*sizeof(int));

	static void qmo(int &x) { x+=((x>>31)&MOD); }
	static int inv(int a,int b)
	{ int x=0,y=1,t=0;for(;a;t=b/a,std::swap(a,b-=t*a),std::swap(y,x-=t*y));return x; }
	// res <- A
	static void cpyI(int p,const int *A,int *res)
	{ std::copy((const Vec*)A,(const Vec*)A+p/VSIZE,(Vec*)res); }
	// res <- A+B
	static void addI(int p,const int *_A,const int *_B,int *_res)
	{
		const Vec *A=(const Vec*)_A,*B=(const Vec*)_B;
		Vec *res=(Vec*)_res;
		for(int i=0;i<p/VSIZE;i++)
			res[i]=A[i]+B[i]-MOD,res[i]+=(res[i]>>31&MOD);
	}
	// res <- A-B
	static void subI(int p,const int *_A,const int *_B,int *_res)
	{
		const Vec *A=(const Vec*)_A,*B=(const Vec*)_B;
		Vec *res=(Vec*)_res;
		for(int i=0;i<p/VSIZE;i++)
			res[i]=A[i]-B[i],res[i]+=(res[i]>>31&MOD);
	}
	// A <- A*w^t, assuming 0<=t<2*p
	static void mulI(int p,const int *A,int t,int *res)
	{ if(t>=p) t-=p; std::rotate_copy(A,A+(p-t),A+p,res); }

	// FFT on I_p[x] with length q, p=2^u, q=2^v
	static void DIT(int *a,int p,int q)
	{
		int n=p*q,*x=new int[p]{},*y=new int[p]{};
		for(int i=n>>1;i>=p;i>>=1) for(int j=0;j<n;j+=(i<<1))
		{
			// w'=w_{2i}=w_p^{p/(2i)}
			int step=p*p/(i<<1),w=0;
			for(int k=0;k<i;k+=p,w+=step)
			{
				// (x,y) <- (x+y,w(x-y))
				// x: [j+k,j+k+p)
				// y: [i+j+k,i+j+k+p)
				int *a0=a+j+k,*a1=a+i+j+k;
				addI(p,a0,a1,x);
				subI(p,a0,a1,y);
				cpyI(p,x,a0);
				mulI(p,y,w,a1);
			}
		}
		delete[] x,delete[] y;
	}
	// IFFT on I_p[x] with length q, p=2^u, q=2^v
	static void DIF(int *a,int p,int q)
	{
		int n=p*q,*x=new int[p]{},*y=new int[p]{};
		for(int i=p;i<n;i<<=1) for(int j=0;j<n;j+=(i<<1))
		{
			// w'=w_{2i}=w_p^{p/(2i)}
			int step=p*p/(i<<1),w=0;
			for(int k=0;k<i;k+=p,w+=step)
			{
				// (x,y) <- (x+wy,x-wy)
				// x: [j+k,j+k+p)
				// y: [i+j+k,i+j+k+p)
				int *a0=a+j+k,*a1=a+i+j+k;
				cpyI(p,a0,x);
				mulI(p,a1,p-w,y);
				addI(p,x,y,a0);
				subI(p,x,y,a1);
			}
		}
		int ilen=inv(q,MOD)%MOD;
		qmo(ilen);
		for(int i=0;i<p*q;i++) a[i]=1ll*a[i]*ilen%MOD;
		delete[] x,delete[] y;
	}
	// calculates A(x)*B(x) mod (x^n-1), n=2^r
	static void __multiply(int r,const int *A,const int *B,int *res)
	{
		int n=(1<<r);
		if(n<=NTT_LIM)
		{
			for(int i=0;i<n;i++) if(A[i])
			{
				for(int j=0;j<n-i;j++)
					res[i+j]=(res[i+j]+1ll*A[i]*B[j])%MOD;
				for(int j=n-i;j<n;j++)
					res[i+j-n]=(res[i+j-n]+1ll*A[i]*B[j])%MOD;
			}
			return;
		}
		int v=r/2-!(r&1),u=r-v,p=(1<<u),q=(1<<v);
		// v+1<=u<=v+2, 2q<=p<=4q

		int *tA=new int[2*n]{},*tB=new int[2*n]{},*tmp=new int[p]{};
		for(int i=0;i<p;i++) for(int j=0;j<q;j++)
		{
			tA[j*p+i]=A[i*q+j];
			tB[j*p+i]=B[i*q+j];
		}
		DIT(tA,p,2*q);
		DIT(tB,p,2*q);
		for(int i=0;i<2*q;i++)
		{
			std::fill(tmp,tmp+p,0);
			__multiply(u,tA+i*p,tB+i*p,tmp);
			cpyI(p,tmp,tA+i*p);
		}
		DIF(tA,p,2*q);
		for(int i=0;i<2*q;i++) for(int j=0;j<p;j++)
		{
			int k=j*q+i;
			if(k>=n) k-=n;
			qmo(res[k]+=tA[i*p+j]-MOD);
		}
		delete[] tA;
		delete[] tB;
		delete[] tmp;
	}

 public:
	static std::vector<int> conv(std::vector<int> p,std::vector<int> q)
	{
		int n1=p.size(),n2=q.size(),r=1;
		while((1<<(r-1))<n1+n2) r++;
		int m=(1<<r);
		p.resize(m),q.resize(m);
		std::vector<int> res(m);
		__multiply(r,p.data(),q.data(),res.data());
		for(int i=(m>>1);i<m;i++) qmo(res[i-(m>>1)]-=res[i]);
		res.resize(n1+n2-1);
		return res;
	}
};

} // namespace Math::ZPoly
原文