提交记录 9299


用户 题目 状态 得分 用时 内存 语言 代码长度
Jazengm 1002. 测测你的多项式乘法 Accepted 100 291.556 ms 73376 KB C++11 1.75 KB
提交时间 评测时间
2019-04-22 20:14:14 2020-08-01 01:36:14
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>

using std::swap;
using std::reverse;

const double eps(0.4), pi(acos(-1.0));

inline int toInt(double x) {return int(x+(x>0? eps: -eps));}

struct C
{
    double r, i;
    C() {} C(double r): r(r), i(0) {}
    C(double r, double i): r(r), i(i) {}

    inline C operator +(const C &c) const {return C(r+c.r, i+c.i);}
    inline C operator -(const C &c) const {return C(r-c.r, i-c.i);}
    inline C operator *(const C &c) const {return C(r*c.r-i*c.i, r*c.i+i*c.r);}
    inline C conj() {return C(r, -i);}
};

inline void dFT(C* a, int s)
{
    static C b[1<<21|233];
    C *f(a), *g(b);
    for(int k(s>>1); k; k>>=1, swap(f, g))
        for(int i(0); i<s>>1; i+=k)
        {
            C *g0(g+i), *g1(g+(s>>1|i)), *f0(f+(i<<1)), *f1(f+(i<<1|k)), ome(cos(2*pi*i/s), sin(2*pi*i/s));
            for(int j(0); j<k; ++j)
            {
                C h(f1[j]*ome);
                g0[j] = f0[j] + h;
                g1[j] = f0[j] - h;
            }
        }
    for(int i(0); i<s; ++i)
        a[i] = f[i];
}

inline void iDFT(C* a, int s)
{
    dFT(a, s);
    reverse(a+1, a+s);
    double k(1.0/s);
    for(int i(0); i<s; ++i)
        a[i].r*=k, a[i].i*=k;
}

void poly_multiply(unsigned *a, int n, unsigned *b, int m, unsigned *r)
{
    int s(1);
    for(; s<n+m+1; s<<=1);

    static C c[1<<21|233];
    for(int i(0); i<s; ++i)
        c[i] = C(i<=n? a[i]: 0, i<=m? b[i]: 0);
    dFT(c, s);
    c[0] = c[0].r * c[0].i;
    c[s>>1] = c[s>>1].r * c[s>>1].i;
    for(int i(1); i<s>>1; ++i)
    {
        C x(c[i]), y(c[s-i]);
        c[i] = (x+y.conj())*(x-y.conj())*C(0, -0.25);
        c[s-i] = (y+x.conj())*(y-x.conj())*C(0, -0.25);
    }
    iDFT(c, s);
    for(int i(0); i<=n+m; ++i)
        r[i] = toInt(c[i].r);
}



CompilationN/AN/ACompile OKScore: N/A

Testcase #1291.556 ms71 MB + 672 KBAcceptedScore: 100


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