牛客挑战赛44 斐波那契?数列!

斐波那契?数列!

https://ac.nowcoder.com/acm/contest/8051/F

第一问:
根据线性递推序列的特征方程理论,我们可以根据递推方程构造特征多项式,使用特征多项式的根来构造通项方程。
而an^2也可以直接对通项平方,我们发现an^2也是由某个特征多项式的根构成。构造这个特征多项式,进而可以构造出线性递推方程。
因此我们可以断言,an^2也是线性递推的。
对于线性递推方程,我们可以使用BM线性递推黑盒算法求解递推方程的某项。复杂度O(n^2logk),其中n是最少需要传入的项数,k是求k项,复杂度优于矩阵快速幂递推。
第二问:
结论题,答案是fib(n)*fib(n + 1)。矩阵快速幂求解即可。

#include <bits/stdc++.h>

using namespace std;
#define rep(i,a,n) for (long long i=a;i<n;i++)
#define per(i,a,n) for (long long i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((long long)(x).size())
typedef vector<long long> VI;
typedef long long ll;
typedef __int128 Int;
typedef pair<long long, long long> PII;
const ll mod = 998244353;
ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a%mod; a = a * a%mod; }return res; }
// head

Int _, n;
namespace linear_seq
{
    const long long N = 10010;
    ll res[N], base[N], _c[N], _md[N];

    vector<long long> Md;
    void mul(ll *a, ll *b, long long k)
    {
        rep(i, 0, k + k) _c[i] = 0;
        rep(i, 0, k) if (a[i]) rep(j, 0, k)
            _c[i + j] = (_c[i + j] + a[i] * b[j]) % mod;
        for (long long i = k + k - 1; i >= k; i--) if (_c[i])
            rep(j, 0, SZ(Md)) _c[i - k + Md[j]] = (_c[i - k + Md[j]] - _c[i] * _md[Md[j]]) % mod;
        rep(i, 0, k) a[i] = _c[i];
    }
    long long solve(Int n, VI a, VI b)
    { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
//        printf("%d\n",SZ(b));
        ll ans = 0;
        Int pnt = 0;
        Int k = SZ(a);
        assert(SZ(a) == SZ(b));
        rep(i, 0, k) _md[k - 1 - i] = -a[i]; _md[k] = 1;
        Md.clear();
        rep(i, 0, k) if (_md[i] != 0) Md.push_back(i);
        rep(i, 0, k) res[i] = base[i] = 0;
        res[0] = 1;
        while ((Int(1) << pnt) <= n) pnt++;
        for (Int p = pnt; p >= 0; p--)
        {
            mul(res, res, k);
            if ((n >> p) & 1)
            {
                for (long long i = k - 1; i >= 0; i--) res[i + 1] = res[i]; res[0] = 0;
                rep(j, 0, SZ(Md)) res[Md[j]] = (res[Md[j]] - res[k] * _md[Md[j]]) % mod;
            }
        }
        rep(i, 0, k) ans = (ans + res[i] * b[i]) % mod;
        if (ans < 0) ans += mod;
        return ans;
    }
    VI BM(VI s)
    {
        VI C(1, 1), B(1, 1);
        long long L = 0, m = 1, b = 1;
        rep(n, 0, SZ(s))
        {
            ll d = 0;
            rep(i, 0, L + 1) d = (d + (ll)C[i] * s[n - i]) % mod;
            if (d == 0) ++m;
            else if (2 * L <= n)
            {
                VI T = C;
                ll c = mod - d * powmod(b, mod - 2) % mod;
                while (SZ(C) < SZ(B) + m) C.pb(0);
                rep(i, 0, SZ(B)) C[i + m] = (C[i + m] + c * B[i]) % mod;
                L = n + 1 - L; B = T; b = d; m = 1;
            }
            else
            {
                ll c = mod - d * powmod(b, mod - 2) % mod;
                while (SZ(C) < SZ(B) + m) C.pb(0);
                rep(i, 0, SZ(B)) C[i + m] = (C[i + m] + c * B[i]) % mod;
                ++m;
            }
        }
        return C;
    }
    long long gao(VI a, Int n)
    {
        VI c = BM(a);
        c.erase(c.begin());
        rep(i, 0, SZ(c)) c[i] = (mod - c[i]) % mod;
        return solve(n, c, VI(a.begin(), a.begin() + SZ(c)));
    }
};

const int N = 1e5 + 100;

Int x, y, z;
Int aa[N], sa[N];

void read(Int &x) {
    x = 0; char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
}

const int M = 3;

int m = 2;

ll res[M][M], co[M][M], fu[M][M];

void cal(ll a[][M], ll b[][M]) {
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                fu[i][j] = (fu[i][j] + a[i][k] * b[k][j]) % mod;
            }
        }
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            a[i][j] = fu[i][j];
            fu[i][j] = 0;
        }
    }
}

ll qpow(ll n) {
    memset(res, 0, sizeof(res));
    res[0][0] = res[1][1] = 1;
    memset(co, 0, sizeof(co));
    co[0][0] = co[0][1] = co[1][0] = 1;
    while (n > 0) {
        if (n & 1) cal(res, co);
        cal(co, co);
        n /= 2;
    }
    return res[1][0];
}

int main()
{
    read(n); read(sa[1]); read(sa[2]); read(sa[3]); read(x); read(y); read(z);
    aa[1] = sa[1] * sa[1] % mod; aa[2] = (aa[1] + sa[2] * sa[2]) % mod; aa[3] = (aa[2] + sa[3] * sa[3]) % mod;
    for (int i = 4; i <= 500; i++) {
        sa[i] = x * sa[i - 1] + y * sa[i - 2] + z * sa[i - 3];
        sa[i] %= mod;
        aa[i] = aa[i - 1] + sa[i] * sa[i];
        aa[i] %= mod;
        //cout << i << " " << ll(sa[i]) << " " << ll(aa[i]) << endl;
    }
    VI v;
    for (int i = 1; i <= 500; i++) v.push_back(aa[i]);
    printf("%lld\n", linear_seq::gao(v, n - 1));
    int q, n;
    scanf("%d", &q);
    while (q--) {
        scanf("%d", &n);
        ll a = qpow(n), b = qpow(n + 1);
    //    cout << a << " " << b << endl;
        printf("%lld\n", a * b % mod); 
    }
}
全部评论

相关推荐

已老实求offer😫:有点像徐坤(没有冒犯的意思哈)
点赞 评论 收藏
分享
11-03 14:38
重庆大学 Java
AAA求offer教程:我手都抬起来了又揣裤兜了
点赞 评论 收藏
分享
评论
3
收藏
分享
牛客网
牛客企业服务