专栏首页数据结构与算法洛谷P5245 【模板】多项式快速幂(多项式ln 多项式exp)

洛谷P5245 【模板】多项式快速幂(多项式ln 多项式exp)

题意

题目链接

Sol

\(B(x) = \exp(K\ln(A(x)))\)

做完了。。。

复杂度\(O(n\log n)\)

// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 4e5 + 10, INF = 1e9 + 10, INV2 = 499122177;
const double eps = 1e-9, pi = acos(-1);
const int G = 3, mod = 998244353;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = (1ll * x * 10 + c - '0') % mod, c = getchar();
    return x * f;
}
int N, K, a[MAXN], b[MAXN];
namespace Poly {
    int rev[MAXN], GPow[MAXN], A[MAXN], B[MAXN], C[MAXN], lim;
    
    template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
    template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
    int fp(int a, int p, int P = mod) {
        int base = 1;
        for(; p; p >>= 1, a = 1ll * a * a % P) if(p & 1) base = 1ll * base *  a % P;
        return base;
    }
    int GetLen(int x) {
        int lim = 1;
        while(lim <= x) lim <<= 1;
        return lim;
    }
    int GetLen2(int x) {
        int lim = 1; 
        while(lim <= x) lim <<= 1;
        return lim;
    }
    int GetOrigin(int x) {//¼ÆËãÔ­¸ù 
        static int q[MAXN]; int tot = 0, tp = x - 1;
        for(int i = 2; i * i <= tp; i++) if(!(tp % i)) {q[++tot] = i;while(!(tp % i)) tp /= i;}
        if(tp > 1) q[++tot] = tp;
        for(int i = 2, j; i <= x - 1; i++) {
            for(j = 1; j <= tot; j++) if(fp(i, (x - 1) / q[j], x) == 1) break;
            if(j == tot + 1) return i;
        }
    }
    void Init(int Lim) {
        for(int i = 1; i <= Lim; i++) GPow[i] = fp(G, (mod - 1) / i);
    }
    void NTT(int *A, int lim, int opt) {
        int len = 0; for(int N = 1; N < lim; N <<= 1) ++len; 
        for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
        for(int i = 0; i <= lim; i++) if(i < rev[i]) swap(A[i], A[rev[i]]);
        for(int mid = 1; mid < lim; mid <<= 1) {
            int Wn = GPow[mid << 1];
            for(int i = 0; i < lim; i += (mid << 1)) {
                for(int j = 0, w = 1; j < mid; j++, w = mul(w, Wn)) {
                    int x = A[i + j], y = mul(w, A[i + j + mid]);
                    A[i + j] = add(x, y), A[i + j + mid] = add(x, -y);
                }
            }
        }
        if(opt == -1) {
            reverse(A + 1, A + lim);
            int Inv = fp(lim, mod - 2);
            for(int i = 0; i <= lim; i++) mul2(A[i], Inv);
        }
    }
    void Mul(int *a, int *b, int N, int M) {
        memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B));
        int lim = 1, len = 0; 
        while(lim <= N + M) len++, lim <<= 1;
        for(int i = 0; i <= N; i++) A[i] = a[i]; 
        for(int i = 0; i <= M; i++) B[i] = b[i];
        NTT(A, lim, 1); NTT(B, lim, 1);
        for(int i = 0; i <= lim; i++) B[i] = mul(B[i], A[i]);
        NTT(B, lim, -1);
        for(int i = 0; i <= N + M; i++) b[i] = B[i];
        memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B));
    }
    void Inv(int *a, int *b, int len) {//B1 = 2B - A1 * B^2 
        if(len == 1) {b[0] = fp(a[0], mod - 2); return ;}
        Inv(a, b, len >> 1);
        for(int i = 0; i < len; i++) A[i] = a[i], B[i] = b[i];
        NTT(A, len << 1, 1); NTT(B, len << 1, 1);
        for(int i = 0; i < (len << 1); i++) mul2(A[i], mul(B[i], B[i]));
        NTT(A, len << 1, -1);
        for(int i = 0; i < len; i++) add2(b[i], add(b[i], -A[i]));
        for(int i = 0; i < (len << 1); i++) A[i] = B[i] = 0;
    }
    void Dao(int *a, int *b, int len) {
        for(int i = 1; i < len; i++) b[i - 1] = mul(i, a[i]); b[len - 1] = 0;
    }
    void Ji(int *a, int *b, int len) {
        for(int i = 1; i < len; i++) b[i] = mul(a[i - 1], fp(i, mod - 2)); b[0] = 0;
    }
    void Ln(int *a, int *b, int len) {//G(A) = \frac{A}{A'} qiudao zhihou jifen 
        static int A[MAXN], B[MAXN];
        Dao(a, A, len); 
        Inv(a, B, len);
        NTT(A, len << 1, 1); NTT(B, len << 1, 1);
        for(int i = 0; i < (len << 1); i++) B[i] = mul(A[i], B[i]);
        NTT(B, len << 1, -1); 
        Ji(B, b, len << 1);
        memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B));
    }
    void Exp(int *a, int *b, int len) {//F(x) = F_0 (1 - lnF_0 + A) but code ..why....
        if(len == 1) return (void) (b[0] = 1);
        Exp(a, b, len >> 1); Ln(b, C, len);
        C[0] = add(a[0] + 1, -C[0]);
        for(int i = 1; i < len; i++) C[i] = add(a[i], -C[i]);
        NTT(C, len << 1, 1); NTT(b, len << 1, 1);
        for(int i = 0; i < (len << 1); i++) mul2(b[i], C[i]);
        NTT(b, len << 1, -1);
        for(int i = len; i < (len << 1); i++) C[i] = b[i] = 0;
    }
    void Sqrt(int *a, int *b, int len) {
        static int B[MAXN];
        Ln(a, B, len);
        for(int i = 0; i < len; i++) B[i] = mul(B[i], INV2);
        Exp(B, b, len); 
    }
    void Div(int *F, int *G, int *Q, int *R, int N, int M) {//F(n) = G(m) * Q(n - m + 1) + R(m) 
        static int Ginv[MAXN]; memset(Ginv, 0, sizeof(Ginv));
        reverse(F, F + N + 1); reverse(G, G + M + 1);
        Inv(G, Ginv, GetLen2(N - M));//why not M
        Mul(F, Ginv, N - M, N - M);
        for(int i = 0; i <= N - M; i++) Q[i] = Ginv[i];
        reverse(Q, Q + N - M + 1);
        reverse(F, F + N + 1); reverse(G, G + M + 1);
        Mul(Q, G, N - M, M);
        for(int i = 0; i < M; i++) R[i] = add(F[i], -G[i]);
    }
    void Pow(int *a, int *b, int P, int N, int len) {
        static int tx[MAXN], ty[MAXN]; memset(tx, 0, sizeof(tx)); memset(ty, 0, sizeof(ty));
        Ln(a, tx, len);
        for(int i = 0; i < N; i++) ty[i] = mul(P, tx[i]);
        Exp(ty, b, len);
    }
};
using namespace Poly; 
signed main() {
    N = read(); K = read();
    Init(4 * N);
    for(int i = 0; i < N; i++) a[i] = read();
    Pow(a, b, K, N, GetLen(N));
    for(int i = 0; i < N; i++) cout << b[i] << ' ';
    return 0;
}
/*
4 1242412412412412412421421
1 1 0 0

*/

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • BZOJ5249: [2018多省省队联测]IIIDX(线段树 贪心)

    不难发现题目给出的是一个树,其中\(\frac{i}{K}\)是\(i\)的父亲节点

    attack
  • cf1037D. Valid BFS?(BFS?)

    可以这样想,在BFS序中较早出现的一定是先访问的,所以把每个点连出去的边按出现的前后顺序排个序

    attack
  • loj#6436. 「PKUSC2018」神仙的游戏(生成函数)

    我们考虑枚举一个长度len。有一个结论是如果我们按N - len的余数分类,若同一组内的全为0或全为1(?不算),那么存在一个长度为len的border。

    attack
  • 一遍记住Java常用的八种排序算法与代码实现

    (如果每次比较都交换,那么就是交换排序;如果每次比较完一个循环再交换,就是简单选择排序。)

    田维常
  • 你必须知道的指针基础-7.void指针与函数指针

      void *表示一个“不知道类型”的指针,也就不知道从这个指针地址开始多少字节为一个数据。和用int表示指针异曲同工,只是更明确是“指针”。

    Edison Zhou
  • ICPC Asia Shenyang 2019 Dudu's maze

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    用户2965768
  • LeetCode 第 210 场周赛 解题报告

    那么在遍历过程中,栈中元素数量的最大值即为答案。栈中的(可以理解为还没遍历到匹配的),即那些嵌套的(。

    ACM算法日常
  • LeetCode 164. Maximum Gap (排序)

    题解:首先,当然我们可以用快排,排完序之后,遍历一遍数组,就能得到答案了。但是快速排序的效率是O(n* logn),不是题目要求的线性效率,也就是O(n)的效率...

    ShenduCC
  • 图论--拓扑排序--判断一个图能否被拓扑排序

    拓扑排序的实现条件,以及结合应用场景,我们都能得到拓扑排序适用于DAG图(Directed Acyclic Graph简称DAG)有向无环图, 根据关系我们能得...

    风骨散人Chiam
  • Educational Codeforces Round 67 (Rated for Div. 2) A~E 贪心,构造,线段树,树的子树

    Educational Codeforces Round 67 (Rated for Div. 2)

    用户2965768

扫码关注云+社区

领取腾讯云代金券