洛谷 P7016 [CERC2013]Captain Obvious and the Rabbit-Man 题解

一、题目:

洛谷原题

codeforces原题

二、题目:

这道题的官方题解秀了我一脸。

设多项式

\[\begin{aligned} A(x)&=(x-1)(x-2)(x-3)(x-5)\cdots(x-F_k)\\ &=x^k+b_1x^{k-1}+b_2x^{k-2}+\cdots+b_{k-1}x+b_k \end{aligned} \]

注意到

\[\begin{aligned} A(1)=1^k+b_1\times1^{k-1}+b_2\times1^{k-2}+\cdots+b_k=0\\ A(2)=2^k+b_1\times2^{k-1}+b_2\times2^{k-2}+\cdots+b_k=0\\ A(3)=3^k+b_1\times 3^{k-1}+b_2\times 3^{k-2}+\cdots +b_k=0\\ A(5)=5^k+b_1\times 5^{k-1}+b_2\times 5^{k-2}+\cdots +b_k=0\\ \end{aligned} \]

\[\forall i\in[1,k],A(F_i)=F_i^k+b_1F_i^{k-1}+b_2F_i^{k-2}+\cdots+b_k=0 \]

等式两边同时乘以 \(a_iF_i\),得

\[a_iF_iA(F_i)=a_iF_i^{k+1}+b_1a_iF_i^{k}+b_2a_iF_i^{k-1}+\cdots+b_ka_iF_i=0 \]

累加,得

\[\sum\limits_{i=1}^kF_ia_iA(F_i)=\sum\limits_{i=1}^{k}a_iF_i^{k+1}+b_1\sum\limits_{i=1}^ka_iF_i^{k}+\cdots+b_k\sum\limits_{i=1}^ka_iF_i=0 \]

\[p(k+1)+b_1p(k)+b_2p(k-1)+\cdots+b_kp(1)=0 \]

这题结束了,时间复杂度就是求出 \(A(x)\) 的系数 \(b\) 的时间复杂度,即 \(O(k^2)\)

三、代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
#define FILEIN(s) freopen(s, "r", stdin)
#define FILEOUT(s) freopen(s, "w", stdout)
#define mem(s, v) memset(s, v, sizeof s)

inline int read(void) {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return f * x;
}

const int MAXK = 4005;

int MOD, K;
long long f[MAXK];

struct Polynomial {
    int n;
    long long a[MAXK];
    Polynomial() { n = 0; mem(a, 0); }
    inline friend Polynomial operator *(const Polynomial &A, const Polynomial &B) {
        Polynomial res; res.n = A.n + B.n;
        for (int i = 0; i <= A.n; ++ i)
            for (int j = 0; j <= B.n; ++ j)
                (res.a[i + j] += A.a[i] * B.a[j] % MOD) %= MOD;

        return res;
    }
};

inline void prework(void) {
    f[1] = 1; f[2] = 2;
    for (int i = 3; i <= K; ++ i) f[i] = (f[i - 1] + f[i - 2]) % MOD;
}

int main() {
    int T = read();
    while (T --) {
        K = read(); MOD = read();
        prework();
        Polynomial res; res.n = 0; res.a[0] = 1;
        for (int i = 1; i <= K; ++ i) {
            Polynomial now; now.n = 1; now.a[0] = -f[i]; now.a[1] = 1;
            res = res * now;
        }
        long long ans = 0;
        for (int i = 1; i <= K; ++ i) {
            long long p = read();
            (ans += p * res.a[i - 1] % MOD) %= MOD;
        }
        ans = -ans;
        if (ans < 0) ans += MOD;
        printf("%lld\n", ans);
    }
    return 0;
}

posted @ 2021-07-03 16:38  蓝田日暖玉生烟  阅读(96)  评论(0编辑  收藏  举报