bzoj4002 [JLOI2015] + meaningful string characteristic root matrix Fast Power

Topic Portal

https://lydsy.com/JudgeOnline/problem.php?id=4002

answer

Immortal title.

Tip according to one of the following:
\ [B ^ 2 \ Leq D \ Leq (B +. 1) ^ 2 \]
That \ (-. 1 <B - \ sqrt D \ Leq 0 \) .

So if we construct a series \ (F \) , which is the general term formula
\ [f_n = (\ frac { b + \ sqrt d} {2}) ^ n + (\ frac {b - \ sqrt d} { 2}) ^ n \]
because the back \ ((\ frac {b - \ sqrt d} {2}) ^ n \) an absolute value of \ (<1 \) , (in the \ (2 | n \) and \ (b \ neq \ sqrt d \) when \ (> 0 \) , or \ (<0 \) ). So long as we can find this thing, we can very quickly obtain the required formula of the original title.


I found that this thing is very much like the general term formula by the characteristic root architecture. So we set \ (F_n = A \ CDOT F_ {+}. 1-n-C \ 2-n-CDOT F_ {} \) .
\ [X ^ 2 = ax +
c \\ x ^ 2-ax-c = 0 \\ x = \ frac {a \ pm \ sqrt {a ^ 2 + 4c}} {2} \] Accordingly Order \ (A B =, = C \ FRAC {D - B ^ 2}. 4 \) .

It is easy to verify correctness.


Matrix then click.

In \ (2 | n \) and \ (b \ neq \ sqrt d \) when the need to \ (A_N -. 1 \) .


#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
    int f = 0, c;
    while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
    x = c & 15;
    while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
    f ? x = -x : 0;
}

const ull P = 7528443412579576937;

ull n, b;
ull d;

inline ull smod(ull x) { return x >= P ? x - P : x; }
inline void sadd(ull &x, const ull &y) { x += y; x >= P ? x -= P : x; }

inline ull fmul(ull x, ull y) {
    ull ans = 0;
    for (; y; y >>= 1, sadd(x, x)) if (y & 1) sadd(ans, x);
    return ans;
}

struct Matrix {
    ull a[2][2];
    
    inline Matrix() { memset(a, 0, sizeof(a)); }
    inline Matrix(const ull &x) {
        memset(a, 0, sizeof(a));
        a[0][0] = a[1][1] = x;
    }
    
    inline Matrix operator * (const Matrix &b) {
        Matrix c;
        c.a[0][0] = smod(fmul(a[0][0], b.a[0][0]) + fmul(a[0][1], b.a[1][0]));
        c.a[0][1] = smod(fmul(a[0][0], b.a[0][1]) + fmul(a[0][1], b.a[1][1]));
        c.a[1][0] = smod(fmul(a[1][0], b.a[0][0]) + fmul(a[1][1], b.a[1][0]));
        c.a[1][1] = smod(fmul(a[1][0], b.a[0][1]) + fmul(a[1][1], b.a[1][1]));
        return c;
    }
} A, B;

inline Matrix fpow(Matrix x, ull y) {
    Matrix ans(1);
    for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
    return ans;
}

inline void work() {
    if (n == 0) return (void)puts("1");
    B.a[0][0] = b, B.a[1][0] = 2;
    A.a[0][0] = b, A.a[0][1] = (d - (ull)b * b) / 4;
    A.a[1][0] = 1, A.a[1][1] = 0;
    B = fpow(A, n - 1) * B;
    if (n & 1) printf("%llu\n", B.a[0][0]);
    else printf("%llu\n", B.a[0][0] - !((ull)b * b == d));
}

inline void init() {
    read(b), read(d), read(n);
}

int main() {
#ifdef hzhkk
    freopen("hkk.in", "r", stdin);
#endif
    init();
    work();
    fclose(stdin), fclose(stdout);
    return 0;
}

Guess you like

Origin www.cnblogs.com/hankeke/p/bzoj4002.html
Recommended