欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

洛谷P4723 【模板】线性递推(多项式取模 线性代数)

程序员文章站 2022-03-25 18:56:15
题意 "题目链接" Sol "Orz shadowice" 注意,下面的代码自带O(随时TLE)倍大常数。。 cpp include define Pair pair define MP(x, y) make_pair(x, y) define fi first define se second d ......

题意

题目链接

sol

orz shadowice

注意,下面的代码自带o(随时tle)倍大常数。。

#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 = x * 10 + c - '0', c = getchar();
    return x * f;
}
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], tf[maxn], tg[maxn]; memset(ginv, 0, sizeof(ginv));
        memcpy(tf, f, (n + 1) << 2); memcpy(tg, g, (m + 1) << 2);
        
        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]);
        memcpy(f, tf, (n + 1) << 2); memcpy(g, tg, (m + 1) << 2);
    }
    void pownum(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);
    }
    void mod(int *a,  int *b, int n, int m) {
        static int q[maxn], r[maxn];
        div(a, b, q, r, n, m);
        memcpy(a, r, m << 2);
    }
    void powpoly(int *base, ll p, int *mod, int m) {
        static int res[maxn], t[maxn]; res[0] = 1;
        while(p) {
            if(p & 1) {
                int lim = getlen(m << 1);
                memset(res + m, 0, lim - m << 2);;
                memcpy(t, base, m << 2); memset(t + m, 0, lim - m << 2);
                ntt(t, lim, 1); ntt(res, lim, 1);
                for(int i = 0; i < lim; i++) res[i] = mul(res[i], t[i]);
                ntt(res, lim, -1); 
                mod(res, mod, lim, m);
            }
            p >>= 1;
            if(p) {
                int lim = getlen(m << 1);
                memset(base + m, 0, lim - m << 2);
                ntt(base, lim, 1); 
                for(int i = 0; i < lim; i++) base[i] = mul(base[i], base[i]);
                ntt(base, lim, -1); 
                mod(base, mod, (m << 1) , m);
            }
            
        }   
        
        memcpy(base, res, m << 2);
    }
    int solve(int *f, int *a, ll n, int k) {
        static int aa[maxn], g[maxn];
        for(int i = 1; i <= k; i++) g[k - i] = (-f[i] + mod) % mod;
        g[k] = aa[1] = 1;
        powpoly(aa, n, g, k);
        int ans = 0;
        for(int i = 0; i < k; i++) add2(ans, mul(aa[i], a[i]) - mod);
        return ans;
    }
    int lrec(ll n, int k) {//a_n = \sum_{i=1}^k f_i * a_{n-i}
        static int f[maxn], a[maxn];
        init(8 * k);
        for(int i = 1; i <= k; i++) f[i] = read();
        for(int i = 0; i < k; i++)  a[i] = (read() + mod) % mod;
        return solve(f, a, n, k);
    }
};
using namespace poly; 
signed main() {
    ll n = read();int k = read();
    cout << lrec(n, k);
    return 0;
}