[齐次常数线性递推式][多项式取模] LOJ #6017. Shlw loves matrix I

Solution S o l u t i o n

要求的东西是 Ank+1h A n − k + 1 h
记得以前数学竞赛的同桌大佬告诉我特征方程~

xkf(x)==i=1khixkixki=1khixki(5)(6) (5) x k = ∑ i = 1 k h i x k − i (6) f ( x ) = x k − ∑ i = 1 k h i x k − i
根据 Cayley–Hamilton theorem
f(A)Ank+1=|λIA|=0Ank+1(modf(A))(7)(8) (7) f ( A ) = | λ I − A | = 0 (8) A n − k + 1 ≡ A n − k + 1 ( mod f ( A ) )
可以通过多项式取模,搞出 xnk+1modf(x) x n − k + 1 mod f ( x ) 的多项式。即是 Ank+1modf(A) A n − k + 1 mod f ( A ) 的多项式。

#include 
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> Pairs;

const int N = 4040;
const int MOD = 1000000007;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

int n, k;
int a[N], h[N], b[N], c[N];
int mod[N];

inline int add(int &x, int a) {
    x += a; while (x >= MOD) x -= MOD;
}
inline void mul(int *a, int *b, int *c) {
    static int res[N];
    for (int i = 0; i <= k * 2; i++) res[i] = 0;
    for (int i = 0; i <= k; i++)
        for (int j = 0; j <= k; j++)
            add(res[i + j], (ll)a[i] * b[j] % MOD);
    for (int i = k * 2; i >= k; i--)
        for (int j = k - 1; ~j; j--)
            add(res[i - k + j], MOD - (ll)res[i] * mod[j] % MOD);
    for (int i = 0; i < k; i++) c[i] = res[i];
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    read(n); read(k);
    for (int i = 1; i <= k; i++) {
        read(a[i]); add(a[i], MOD);
    }
    for (int i = 1; i <= k; i++) {
        read(h[i]); add(h[i], MOD);
    }
    for (int i = 1; i <= k; i++) mod[k - i] = MOD - a[i];
    mod[k] = 1; c[0] = 1; b[1] = 1;
    for (int i = n - k + 1; i; i >>= 1) {
        if (i & 1) mul(b, c, c);
        mul(b, b, b);
    }
    for (int i = k + 1; i <= k * 2; i++)
        for (int j = 1; j <= k; j++)
            add(h[i], (ll)h[i - j] * a[j] % MOD);
    int ans = 0;
    for (int i = 0; i < k; i++)
        add(ans, (ll)c[i] * h[i + k] % MOD);
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(递推,矩阵)