多项式合集

FFT

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ls (x<<1)
#define rs (x<<1|1)
#define db double
#define all(x)  x.begin(),x.end()
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define eps 1e-9
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=4e5+7;
const int mod=1e9+7;
const double pi=acos(-1.0);
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct Complex
{
    double x,y;
    Complex(double xx=0,double yy=0)
    {
        x=xx;y=yy;
    }
}A[maxn],B[maxn];
Complex operator+(Complex p,Complex q)
{
    return Complex(p.x+q.x,p.y+q.y);
}
Complex operator*(Complex p,Complex q)
{
    return Complex(p.x*q.x-p.y*q.y,p.x*q.y+p.y*q.x);
}
Complex operator-(Complex p,Complex q)
{
    return Complex(p.x-q.x,p.y-q.y);
}
int lim=1,len=0;
int r[maxn];
int n;
char s1[maxn],s2[maxn];
int num[maxn];
void FFT(Complex* A,int op)
{
    for(int i=0;i<lim;i++)
    {
        if(i<r[i])swap(A[i],A[r[i]]);
    }
    for(int j=1;j<lim;j<<=1)
    {
        Complex tmp(cos(pi/j),op*sin(pi/j));
        for(int i=0;i<lim;i+=(j<<1))
        {
            Complex tt(1,0);
            for(int l=0;l<j;l++,tt=tt*tmp)
            {
                Complex t1=A[i+l],t2=A[i+j+l]*tt;
                A[i+l]=t1+t2;
                A[i+j+l]=t1-t2;
            }
        }
    }
}

int main()
{
    cin>>n;
    scanf("%s%s",s1,s2);
    for(int i=0;i<n;i++)A[i]=s1[n-i-1]-'0';
    for(int i=0;i<n;i++)B[i]=s2[n-i-1]-'0';
    while(lim<n*2)lim<<=1,len++;
    for(int i=0;i<=lim;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(len-1));
    FFT(A,1);FFT(B,1);
    for(int i=0;i<=lim;i++)A[i]=A[i]*B[i];
    FFT(A,-1);
    for(int i=0;i<=lim;i++)num[i]=(int)(A[i].x/lim+0.5);
    //cout<<lim<<"\n";
    //for(int i=0;i<=lim;i++)cout<<A[i].x<<" ";cout<<"\n";
    for(int i=0;i<lim;i++){
        num[i+1]+=num[i]/10,num[i]%=10;
    }
    //cout<<num[lim]<<"\n";
    lim++;
    while(!num[lim]&&lim>0)lim--;
    for(int i=lim;i>=0;i--){
        cout<<num[i];
    }
}

  

NTT

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ls (x<<1)
#define rs (x<<1|1)
#define db double
#define all(x)  x.begin(),x.end()
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define eps 1e-9
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=4e5+7;
const int mod=998244353;
const double pi=acos(-1.0);
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int pr=3;
const int phi=mod-1;
int n,m;
int a[maxn],b[maxn],r[maxn];
int len=0;
int qpow(int a,int b)
{
    int res=1;
    while(b){
        if(b&1)res=1ll*res*a%mod;
        a=1ll*a*a%mod;
        b>>=1;
    }return res;
}
void NTT(int *A,int op)
{
    for(int i=0;i<n;i++)if(i<r[i])swap(A[i],A[r[i]]);
    for(int i=1;i<n;i<<=1)
    {
        int W=qpow(pr,phi/(i<<1));
        for(int p=(i<<1),j=0;j<n;j+=p)
        {
            int w=1;
            for(int k=0;k<i;k++,w=1ll*w*W%mod)
            {
                int x=A[j+k],y=1ll*w*A[j+i+k]%mod;
                A[j+k]=(x+y)%mod;
                A[j+i+k]=(x-y+mod)%mod;
            }
        }
    }
    if(op==-1)reverse(A+1,A+n);
}
int main()
{
    cin>>n>>m;
    n++;m++;
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    for(int i=0;i<m;i++)scanf("%d",&b[i]);
    m+=n;
    n=1;
    while(n<=m)n<<=1,len++;
    for(int i=0;i<n;i++)r[i]=(r[i>>1]>>1)|((i&1)<<(len-1));
    //cout<<n<<"\n";
    NTT(a,1);NTT(b,1);
    for(int i=0;i<n;i++)a[i]=1ll*a[i]*b[i]%mod;
    //
    NTT(a,-1);//for(int i=0;i<n;i++)cout<<a[i]<<" ";cout<<"\n";
    int inv=qpow(n,mod-2);
    for(int i=0;i<n;i++)a[i]=1ll*a[i]*inv%mod;
    for(int i=0;i<m-1;i++)printf("%d ",a[i]);printf("\n");
}

  

 

posted @ 2019-05-08 15:08  Twilight7  阅读(200)  评论(0编辑  收藏  举报