我的面向对象多项式全家桶

· · 个人记录

/* Header {{{ */
#include <bits/stdc++.h>
#include <ctime>
#include <utility>
using namespace std;

typedef long long readtype;
typedef long long var;
typedef long double let;

readtype read() {
  readtype a = 0, c = getchar(), s = 0;
  while (!isdigit(c)) s |= c == '-', c = getchar();
  while (isdigit(c)) a = a * 10 + c - 48, c = getchar();
  return s ? -a : a;
}

#ifdef LOCAL_LOGGER
#define logger(...) fprintf(stderr, __VA_ARGS__)
#define abortif(v, ...) if (v) {logger("Error in Line %d, Function '%s()'.\nInfo: ", __LINE__, __FUNCTION__); logger(__VA_ARGS__); exit(0);}
#else
#define logger(...);
#define abortif(v, ...);
#endif
/* }}} */

const int N = 6 * 1e5 + 1;
const int MOD = 998244353;

struct Node {
  // {{{
  typedef double nodetype;
  nodetype x, y;
  Node operator + (Node t) { return (Node) {x + t.x, y + t.y}; }
  Node operator - (Node t) { return (Node) {x - t.x, y - t.y}; }
  Node operator * (Node t) {
    return (Node) {x * t.x - y * t.y, x * t.y + y * t.x}; 
  }
  Node operator * (nodetype t) { return (Node) {x * t, y * t}; }
  Node operator ~ () { return (Node) {x, -y}; }
  void MTTinit(int t) { x = t >> 15, y = t & 32767; }
  // }}}
};

class MathMod {
  // {{{
  public:
    int add(int a, int b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); }
    int mul(var a, int b) { return (a * b) % MOD; }
    int qpow(int x, var y) {
      int res = 1;
      while (y) {
        if (y & 1) res = mul(res, x);
        x = mul(x, x);
        y >>= 1;
      }
      return res;
    }
    int inv(int t) { 
      abortif(!MOD, "MOD = 0");
      return qpow(t, MOD - 2); 
    }
    // }}}
};

class Poly : public MathMod {
  // {{{
  private:
    int tmp_a[N], tmp_b[N], tmp_c[N], tmp_d[N], tmp_e[N], tmp_f[N];

  protected:
    int limit, rev[N];

    void InitLimit(int need_len) {
      if ((need_len << 1) == limit) return ;
      for (limit = 1; limit <= need_len; limit <<= 1);
      for (int i = 0; i < limit; ++i)
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) ? (limit >> 1) : 0);
      InitLimitExtra();
    }
    virtual void InitLimitExtra() = 0;
    virtual void Transform() = 0;

    void Deriv(const int len, const int *a, int *b) {
      for (int i = 0; i < len; ++i) b[i] = mul(a[i + 1], i + 1);
      b[len] = 0;
    }

    void Integral(const int len, const int *a, int *b) {
      for (int i = len; i > 0; --i) b[i] = mul(a[i - 1], inv(i));
      b[0] = 0;
    }

  public:
    virtual void Mul(const int len_a, const int *a, const int len_b, const int *b, int &len_c, int *c) = 0;
    virtual void Inv(const int len, const int *a, int *b) {
      if (!len) {
        b[0] = inv(a[0]);
        return ;
      }
      Inv(len >> 1, a, b);
      int len_a = 0, len_b = 0;
      Mul(len, b, len, b, len_a, tmp_a);
      Mul(len_a, tmp_a, len, a, len_b, tmp_b);
      for (int i = 0; i <= len; ++i) 
        b[i] = add(add(b[i], b[i]), MOD - tmp_b[i]);
      for (int i = 0; i < limit; ++i) 
        tmp_a[i] = 0, tmp_b[i] = 0;
    }
    void Ln(const int len, const int *a, int *b) {
      // tmp_a, tmp_b is used in Inv();
      Inv(len, a, tmp_c);
      Deriv(len, a, b);
      int len_d = 0;
      Mul(len, tmp_c, len, b, len_d, tmp_d);
      Integral(len, tmp_d, b);
      for (int i = 0; i < limit; ++i) 
        tmp_c[i] = tmp_d[i] = 0;
    }
    void Exp(const int len, const int *a, int *b) {
      // tmp_a, tmp_b is used in Inv();
      // tmp_c, tmp_d is used in Ln();
      if (!len) {
        b[0] = 1;
        return ;
      }
      Exp(len >> 1, a, b);
      Ln(len, b, tmp_e);

      for (int i = 1; i <= len; ++i) 
        tmp_f[i] = add(MOD - tmp_e[i], a[i]);
      ++tmp_f[0];

      int len_e = 0;
      Mul(len, b, len, tmp_f, len_e, tmp_e);
      for (int i = 0; i <= len; ++i) b[i] = tmp_e[i];
      for (int i = 0; i < limit; ++i) 
        tmp_e[i] = tmp_f[i] = 0;
    }
    // }}}
};

class FFT : public Poly {
  // {{{
  private:
    Node w[N];

  protected:
    void InitLimitExtra() {
      const Node::nodetype Pi = acos(-1.0);
      for (int mid = 1; mid < limit; mid <<= 1) {
        w[mid] = (Node) {1, 0};
        for (int i = 1; i < mid; ++i) {
          if ((i & 31) == 1) 
            w[mid + i] = (Node) {cos(Pi * i / mid), sin(Pi * i / mid)};
          else 
            w[mid + i] = w[mid + i - 1] * w[mid + 1];
        }
      }
    }

    void Transform() {} 
    void Transform(Node *val, int type) {
      if (type == -1) reverse(val + 1, val + limit);
      for (int i = 0; i < limit; ++i)
        if (i < rev[i]) swap(val[i], val[rev[i]]);
      for (int mid = 1; mid < limit; mid <<= 1) {
        int R = mid << 1;
        for (int i = 0; i < limit; i += R) {
          for (int j = 0; j < mid; ++j) {
            Node v = w[mid + j] * val[i + mid + j];
            val[i + mid + j] = val[i + j] - v;
            val[i + j] = val[i + j] + v;
          }
        }
      }
    }

  public:
    virtual void Mul(const int len_a, const int *a, const int len_b, const int *b, int &len_c, int *c) = 0;
  // }}}
};

class NTT : public Poly {
  // {{{
  private:
    int tmp_a[N], tmp_b[N];
    int inv_limit, G, w[N];

  protected:
    void InitLimitExtra() {
      inv_limit = inv(limit);
      w[0] = 1, w[1] = qpow(G, (MOD - 1) / limit);
      for (int i = 2; i < limit; ++i) 
        w[i] = mul(w[i - 1], w[1]);
    }

    void Transform() {}
    void Transform(int *val, int type) {
      for (int i = 0; i < limit; ++i) 
        if (i < rev[i]) swap(val[i], val[rev[i]]);
      for (int mid = 1; mid < limit; mid <<= 1) {
        int R = mid << 1, delta = limit / R;
        for (int i = 0; i < limit; i += R) {
          for (int j = 0; j < mid; ++j) {
            int v = mul(w[delta * j], val[i + mid + j]);
            val[i + mid + j] = add(val[i + j], MOD - v);
            val[i + j] = add(val[i + j], v);
          }
        }
      }
      if (type == 1) return ;
      reverse(val + 1, val + limit);
      for (int i = 0; i < limit; ++i) 
        val[i] = mul(val[i], inv_limit);
    }

  public:
    void SetG(const int pg) { G = pg; }

    void Mul(const int len_a, const int *a, const int len_b, const int *b, int &len_c, int *c) {
      len_c = len_a + len_b;
      InitLimit(len_c);
      for (int i = 0; i <= len_a; ++i) tmp_a[i] = a[i];
      for (int i = 0; i <= len_b; ++i) tmp_b[i] = b[i];
      Transform(tmp_a, 1), Transform(tmp_b, 1);
      for (int i = 0; i < limit; ++i) {
        c[i] = mul(tmp_a[i], tmp_b[i]);
        tmp_a[i] = tmp_b[i] = 0;
      }
      Transform(c, -1);
    }

    void Inv(const int len, const int *a, int *b) {
      if (!len) {
        b[0] = inv(a[0]);
        return ;
      }
      Inv(len >> 1, a, b);
      InitLimit(len << 1);
      for (int i = 0; i <= len; ++i) tmp_a[i] = a[i];
      Transform(tmp_a, 1), Transform(b, 1);
      for (int i = 0; i < limit; ++i) {
        b[i] = add(add(b[i], b[i]), MOD - mul(mul(b[i], b[i]), tmp_a[i]));
        tmp_a[i] = 0;
      }
      Transform(b, -1);
      for (int i = len + 1; i < limit; ++i) b[i] = 0;
    }
    // }}}
};

class DirtyNTT : public NTT {
  // {{{
  private:
    int tmp_a[N], tmp_b[N], tmp_c[N];

  public:
    void DirtyMul(int &len_a, int *a, int len_b, int *b) {
      InitLimit(len_a += len_b);
      Transform(a, 1), Transform(b, 1);
      for (int i = 0; i < limit; ++i) a[i] = mul(a[i], b[i]);
      Transform(a, -1);
    }

    void DirtyLn(const int len, const int *a, int *b) {
      Inv(len, a, tmp_a);
      Deriv(len, a, b);
      Transform(tmp_a, 1), Transform(b, 1);
      for (int i = 0; i < limit; ++i)
        tmp_a[i] = mul(tmp_a[i], b[i]);
      Transform(tmp_a, -1);
      Integral(len, tmp_a, b);
      for (int i = 0; i < limit; ++i) tmp_a[i] = 0;
    }

    void DirtyExp(const int len, const int *a, int *b) {
      // tmp_a is used in DirtyLn()
      if (!len) {
        b[0] = 1;
        return ;
      }
      DirtyExp(len >> 1, a, b);
      DirtyLn(len, b, tmp_b);

      for (int i = 0; i <= len; ++i) 
        tmp_c[i] = add(MOD - tmp_b[i], a[i]);
      ++tmp_c[0];

      Transform(tmp_c, 1), Transform(b, 1);
      for (int i = 0; i < limit; ++i) b[i] = mul(b[i], tmp_c[i]);
      Transform(b, -1);
      for (int i = len + 1; i < limit; ++i) b[i] = 0;
    }
    // }}}
};

class ExFFT : public FFT {
  // {{{
  private:
    Node tmp_a[N], tmp_b[N], tmp_c[N], tmp_d[N];

  public:
    void Mul(const int len_a, const int *a, const int len_b, const int *b, int &len_c, int *c) {
      len_c = len_a + len_b;
      InitLimit(len_c);
      for (int i = 0; i <= len_a; ++i) tmp_a[i].MTTinit(a[i]);
      for (int i = 0; i <= len_b; ++i) tmp_b[i].MTTinit(b[i]);

      Transform(tmp_a, 1), Transform(tmp_b, 1);
      for (int i = 0; i < limit; ++i) {
        Node ft = ~tmp_a[i ? (limit - i) : 0], 
             f0 = (tmp_a[i] - ft) * (Node) {0, -0.5},
             f1 = (tmp_a[i] + ft) * 0.5;
        Node gt = ~tmp_b[i ? (limit - i) : 0], 
             g0 = (tmp_b[i] - gt) * (Node) {0, -0.5},
             g1 = (tmp_b[i] + gt) * 0.5;
        tmp_c[i] = f1 * g1, tmp_d[i] = f0 * g1 + f1 * g0 + f0 * g0 * (Node) {0, 1};
      }
      Transform(tmp_c, -1), Transform(tmp_d, -1);
      for (int i = 0; i <= len_c; ++i) {
        var v1 = (var) (tmp_c[i].x / limit + 0.5) % MOD,
            v2 = (var) (tmp_d[i].x / limit + 0.5) % MOD,
            v3 = (var) (tmp_d[i].y / limit + 0.5) % MOD;
        c[i] = ((v1 << 30) + (v2 << 15) + v3) % MOD;
      }
      for (int i = 0; i < limit; ++i)
        tmp_a[i] = tmp_b[i] = tmp_c[i] = tmp_d[i] = (Node) {0, 0};
    }
    // }}}
};

class ExNTT : public NTT {
};

typedef ExFFT MTT;

int n;
int f[N], g[N];
MTT poly;

int main() {
#ifndef ONLINE_JUDGE
  freopen("NeoPoly.in", "r", stdin);
  freopen("NeoPoly.out", "w", stdout);
#endif
#ifdef LOCAL_LOGGER
  freopen("NeoPoly.log", "w", stderr);
#endif
  n = read() - 1;
  for (int i = 0; i <= n; ++i) f[i] = read();
  poly.Exp(n, f, g);

  for (int i = 0; i <= n; ++i) printf("%d ", g[i]);
  puts("");
  return 0;
}

/* ==== Makefile ==== {{{
CompileAndRun:
    make Compile
    make Run

Compile:
    g++ -o NeoPoly NeoPoly.cpp -g -Wall -DLOCAL_LOGGER

CompileUF:
    g++ -o NeoPoly NeoPoly.cpp -g -Wall -DLOCAL_LOGGER -fsanitize=undefined

Run:
    ./NeoPoly < NeoPoly.in > NeoPoly.out
==================
}}} */