【多项式模版类】

【多项式模版类】

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define whiteink signed main
#define fi first
#define sc second

#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl

#define pb push_back
#define pq priority_queue
#define all(x) x.begin(),x.end()

using i64=long long;
using i128=__int128;
using u64=unsigned long long;
using u128 = unsigned __int128;
typedef pair<int,int> PII;
typedef pair<i64,i64> P64;

mt19937_64 rng;
//std::random_device rd;
//unsigned int seed = rd();
//rng.seed(seed);
//rng();

template<typename T>
T whink_max(T a,T b){return a>b?a:b;}
template<typename T>
T whink_min(T a,T b){return a<b?a:b;}
template<typename T>
bool cmp(T a,T b){return a>b;}

const int inf_int=0x3f3f3f3f;
const i64 inf_i64=0x3f3f3f3f3f3f3f3f;

//快读快写
inline char nc(){ 
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2 && (p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}

template <typename T> void read(T &x){
    x=0;
    T f=1;
    char ch=nc();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=nc();
    }
    while(ch>='0' && ch<='9'){
        x=x*10+ch-'0';
        ch=nc();
    }
    x*=f;
}

template <typename T> void write(T x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

//快速幂
template<class T>
constexpr T power(T a, unsigned long long b, T res = 1) {
    for (; b != 0; b /= 2, a *= a) {
        if (b & 1) res *= a;
    }
    return res;
}

template<unsigned int P>
constexpr unsigned int mulMod(unsigned int a, unsigned int b) {
    return (unsigned long long)a * b % P;
}

constexpr long long safeMod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

//扩展欧几里得
constexpr std::pair<long long, long long> invGcd(long long a, long long b) {
    a = safeMod(a, b);
    if (a == 0) return {b, 0};
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;
    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;
        std::swap(s, t);
        std::swap(m0, m1);
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

//模数类
template<unsigned int P>
struct ModIntBase {
public:
    unsigned int x;
    constexpr ModIntBase() : x(0) {}
    template<std::unsigned_integral T>
    constexpr ModIntBase(T x_) : x(x_ % P) {}
    template<std::signed_integral T>
    constexpr ModIntBase(T x_) {
        long long v = x_ % (long long)P;
        if (v < 0) v += P;
        x = (unsigned int)v;
    }
    
    constexpr static unsigned int mod() { return P; }
    constexpr unsigned int val() const { return x; }
    constexpr ModIntBase operator-() const {
        ModIntBase res;
        res.x = (x == 0 ? 0 : P - x);
        return res;
    }
    constexpr ModIntBase inv() const {
        auto v = invGcd(x, P);
        return (int)v.second;
    }

    constexpr ModIntBase &operator*=(const ModIntBase &rhs) & {
        x = mulMod<P>(x, rhs.val());
        return *this;
    }
    constexpr ModIntBase &operator+=(const ModIntBase &rhs) & {
        x += rhs.val();
        if (x >= P) x -= P;
        return *this;
    }
    constexpr ModIntBase &operator-=(const ModIntBase &rhs) & {
        if (x < rhs.val()) x += P;
        x -= rhs.val();
        return *this;
    }
    constexpr ModIntBase &operator/=(const ModIntBase &rhs) & {
        return *this *= rhs.inv();
    }

    friend constexpr ModIntBase operator*(ModIntBase lhs, const ModIntBase &rhs) { lhs *= rhs; return lhs; }
    friend constexpr ModIntBase operator+(ModIntBase lhs, const ModIntBase &rhs) { lhs += rhs; return lhs; }
    friend constexpr ModIntBase operator-(ModIntBase lhs, const ModIntBase &rhs) { lhs -= rhs; return lhs; }
    friend constexpr ModIntBase operator/(ModIntBase lhs, const ModIntBase &rhs) { lhs /= rhs; return lhs; }

    friend constexpr std::istream &operator>>(std::istream &is, ModIntBase &a) {
        long long i; is >> i; a = i; return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const ModIntBase &a) {
        return os << a.val();
    }
    friend constexpr bool operator==(const ModIntBase &lhs, const ModIntBase &rhs) {
        return lhs.val() == rhs.val();
    }
    friend constexpr std::strong_ordering operator<=>(const ModIntBase &lhs, const ModIntBase &rhs) {
        return lhs.val() <=> rhs.val();
    }
};

template<int P>
using MInt = ModIntBase<(unsigned int)P>;

using Z = MInt<998244353>;

vector<int> rev;
template<int P> vector<MInt<P>> roots{0, 1};

template<int P>
constexpr MInt<P> findPrimitiveRoot() {
    MInt<P> i = 2;
    int k = __builtin_ctz(P - 1);
    while (true) {
        if (power(i, (P - 1) / 2) != 1) break;
        i += 1;
    }
    return power(i, (P - 1) >> k);
}
template<int P> constexpr MInt<P> primitiveRoot = findPrimitiveRoot<P>();
template<> constexpr MInt<998244353> primitiveRoot<998244353>{31};

//dft
template<int P>
constexpr void dft(vector<MInt<P>> &a) {
    int n = a.size();
    if ((int)rev.size() != n) {
        int k = __builtin_ctz(n) - 1;
        rev.resize(n);
        for (int i = 0; i < n; i++) {
            rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
        }
    }
    for (int i = 0; i < n; i++) {
        if (rev[i] < i) swap(a[i], a[rev[i]]);
    }
    if ((int)roots<P>.size() < n) {
        int k = __builtin_ctz(roots<P>.size());
        roots<P>.resize(n);
        while ((1 << k) < n) {
            auto e = power(primitiveRoot<P>, 1 << (__builtin_ctz(P - 1) - k - 1));
            for (int i = 1 << (k - 1); i < (1 << k); i++) {
                roots<P>[2 * i] = roots<P>[i];
                roots<P>[2 * i + 1] = roots<P>[i] * e;
            }
            k++;
        }
    }
    for (int k = 1; k < n; k *= 2) {
        for (int i = 0; i < n; i += 2 * k) {
            for (int j = 0; j < k; j++) {
                MInt<P> u = a[i + j];
                MInt<P> v = a[i + j + k] * roots<P>[k + j];
                a[i + j] = u + v;
                a[i + j + k] = u - v;
            }
        }
    }
}

//idft
template<int P>
constexpr void idft(vector<MInt<P>> &a) {
    int n = a.size();
    reverse(a.begin() + 1, a.end());
    dft<P>(a);
    MInt<P> inv = (1LL - P) / n;
    for (int i = 0; i < n; i++) a[i] *= inv;
}

//多项式封装
template<int P = 998244353>
struct Poly : public vector<MInt<P>> {
    using Value = MInt<P>;
    using vector<Value>::vector;

    Poly() : vector<Value>() {}
    explicit constexpr Poly(int n) : vector<Value>(n) {}
    explicit constexpr Poly(const vector<Value> &a) : vector<Value>(a) {}
    constexpr Poly(const initializer_list<Value> &a) : vector<Value>(a) {}

    template<class InputIt>
    explicit constexpr Poly(InputIt first, InputIt last) : vector<Value>(first, last) {}

    template<class F>
    explicit constexpr Poly(int n, F f) : vector<Value>(n) {
        for (int i = 0; i < n; i++) (*this)[i] = f(i);
    }

    // ---------- 基础操作 ----------
    //将多项式系数整体平移:
    // k>0左移,高次补0 
    // k<0右移,次数降低 
    constexpr Poly shift(int k) const {
        if (k >= 0) {
            auto b = *this;
            b.insert(b.begin(), k, 0);
            return b;
        } else if (this->size() <= -k) {
            return Poly();
        } else {
            return Poly(this->begin() + (-k), this->end());
        }
    }

    //截断到(k-1)次
    constexpr Poly trunc(int k) const {
        Poly f = *this;
        f.resize(k);
        return f;
    }

    friend constexpr Poly operator+(const Poly &a, const Poly &b) {
        Poly res(max(a.size(), b.size()));
        for (int i = 0; i < (int)a.size(); i++) res[i] += a[i];
        for (int i = 0; i < (int)b.size(); i++) res[i] += b[i];
        return res;
    }
    friend constexpr Poly operator-(const Poly &a, const Poly &b) {
        Poly res(max(a.size(), b.size()));
        for (int i = 0; i < (int)a.size(); i++) res[i] += a[i];
        for (int i = 0; i < (int)b.size(); i++) res[i] -= b[i];
        return res;
    }
    friend constexpr Poly operator-(const Poly &a) {
        vector<Value> res(a.size());
        for (int i = 0; i < (int)res.size(); i++) res[i] = -a[i];
        return Poly(res);
    }

    friend constexpr Poly operator*(Poly a, Poly b) {
        if (a.size() == 0 || b.size() == 0) return Poly();
        if (a.size() < b.size()) swap(a, b);
        int n = 1, tot = a.size() + b.size() - 1;
        while (n < tot) n *= 2;
        if (((P - 1) & (n - 1)) != 0 || b.size() < 128) {
            Poly c(a.size() + b.size() - 1);
            for (int i = 0; i < (int)a.size(); i++)
                for (int j = 0; j < (int)b.size(); j++)
                    c[i + j] += a[i] * b[j];
            return c;
        }
        a.resize(n); b.resize(n);
        dft<P>(a); dft<P>(b);
        for (int i = 0; i < n; ++i) a[i] *= b[i];
        idft<P>(a);
        a.resize(tot);
        return a;
    }

    friend constexpr Poly operator*(Value a, Poly b) { for (auto &v : b) v *= a; return b; }
    friend constexpr Poly operator*(Poly a, Value b) { for (auto &v : a) v *= b; return a; }
    friend constexpr Poly operator/(Poly a, Value b) { for (auto &v : a) v /= b; return a; }

    constexpr Poly &operator+=(Poly b) { return (*this) = (*this) + b; }
    constexpr Poly &operator-=(Poly b) { return (*this) = (*this) - b; }
    constexpr Poly &operator*=(Poly b) { return (*this) = (*this) * b; }
    constexpr Poly &operator*=(Value b) { return (*this) = (*this) * b; }
    constexpr Poly &operator/=(Value b) { return (*this) = (*this) / b; }

    // ---------- 微积分 ----------
    constexpr Poly deriv() const {
        if (this->empty()) return Poly();
        Poly res(this->size() - 1);
        for (int i = 0; i < (int)this->size() - 1; ++i)
            res[i] = (i + 1) * (*this)[i + 1];
        return res;
    }
    constexpr Poly integr() const {
        Poly res(this->size() + 1);
        for (int i = 0; i < (int)this->size(); ++i)
            res[i + 1] = (*this)[i] / (i + 1);
        return res;
    }

    // ---------- 多项式高级运算 ----------
    constexpr Poly inv(int m) const {
        Poly x{(*this)[0].inv()};
        int k = 1;
        while (k < m) {
            k *= 2;
            x = (x * (Poly{2} - trunc(k) * x)).trunc(k);
        }
        return x.trunc(m);
    }
    constexpr Poly log(int m) const {
        return (deriv() * inv(m)).integr().trunc(m);
    }
    constexpr Poly exp(int m) const {
        Poly x{1};
        int k = 1;
        while (k < m) {
            k *= 2;
            x = (x * (Poly{1} - x.log(k) + trunc(k))).trunc(k);
        }
        return x.trunc(m);
    }
    constexpr Poly pow(int k, int m) const {
        int i = 0;
        while (i < (int)this->size() && (*this)[i] == 0) i++;
        if (i == (int)this->size() || 1LL * i * k >= m) return Poly(m);
        Value v = (*this)[i];
        auto f = shift(-i) * v.inv();
        return (f.log(m - i * k) * k).exp(m - i * k).shift(i * k) * power(v, k);
    }
    constexpr Poly sqrt(int m) const {
        Poly x{1};
        int k = 1;
        while (k < m) {
            k *= 2;
            x = (x + (trunc(k) * x.inv(k)).trunc(k)) * MInt<P>(2).inv(); // 乘以 1/2
        }
        return x.trunc(m);
    }

    // 中项卷积,用于多项式求值中的多项式取模
    constexpr Poly mulT(Poly b) const {
        if (b.size() == 0) return Poly();
        int n = b.size();
        reverse(b.begin(), b.end());
        return ((*this) * b).shift(-(n - 1));
    }
    // 多点求值
    constexpr vector<Value> eval(vector<Value> x) const {
        if (this->size() == 0) return vector<Value>(x.size(), 0);
        int n = max(x.size(), this->size());
        vector<Poly> q(4 * n);
        vector<Value> ans(x.size());
        x.resize(n);
        function<void(int, int, int)> build = [&](int p, int l, int r) {
            if (r - l == 1) q[p] = Poly{1, -x[l]};
            else {
                int m = (l + r) / 2;
                build(2 * p, l, m);
                build(2 * p + 1, m, r);
                q[p] = q[2 * p] * q[2 * p + 1];
            }
        };
        build(1, 0, n);
        function<void(int, int, int, const Poly &)> work = [&](int p, int l, int r, const Poly &num) {
            if (r - l == 1) {
                if (l < (int)ans.size()) ans[l] = num[0];
            } else {
                int m = (l + r) / 2;
                work(2 * p, l, m, num.mulT(q[2 * p + 1]).resize(m - l));
                work(2 * p + 1, m, r, num.mulT(q[2 * p]).resize(r - m));
            }
        };
        work(1, 0, n, mulT(q[1].inv(n)));
        return ans;
    }
};

const int N=3e5+10;
int n;

void solve(){
    
    
}

whiteink(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--) solve();
    return 0;
}
posted @ 2026-07-15 01:01  White_ink  阅读(6)  评论(0)    收藏  举报