The number of bzoj2476 battlefield matrix fast power

Topic Portal

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

answer

This question will have to face questions to see what you want to do on a question of 2333

First, to meet the requirements of the subject is large convex block, and one block each of the following must be square (or on the ground floor); this last is not a complete rectangle.

Let's ignore the last condition.

It is easy to find, if we delete the far left, far right or the uppermost row or a square, the perimeter will just reduce \ (2 \) . Unfortunately, this conclusion seems irrelevant, ah, because the need to guarantee that deleted the next line or less than the number of blocks of the next column of that row or delete the row.

Consider how to address this limitation. Why is the impact of this restriction? If the row is deleted only one block, then there is no such influence ah. So we only consider removing the rightmost or leftmost column that only one box.

So eventually the formation of the bottom two lines of the same block of the number of cases - this case is also a good solution, since all the same, delete the last line is just to reduce the circumference \ (2 \) .

So we let \ (f (i) \) represents the circumference \ (i \) the number of squares.

Then we have \ (f (i) = 3F (i-1) \) . He notes that the leftmost and rightmost box of the situation will have to be re-counted. It also needs to be subtracted \ (f (i-2) \) . Thus the final recursion is \ (F (I) = 3F (-I. 1) -f (I-2) \) .

Matrix can be accelerated.

Finally, the \ (f (n) \) results in a minus \ (the n--1 \) - The impact of the last condition before.


#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 int P = 987654321;

int n;

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
    return ans;
}

struct Matrix {
    int a[2][2];
    
    inline Matrix() { memset(a, 0, sizeof(a)); }
    inline Matrix(const int &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] = ((ll)a[0][0] * b.a[0][0] + (ll)a[0][1] * b.a[1][0]) % P;
        c.a[0][1] = ((ll)a[0][0] * b.a[0][1] + (ll)a[0][1] * b.a[1][1]) % P;
        c.a[1][0] = ((ll)a[1][0] * b.a[0][0] + (ll)a[1][1] * b.a[1][0]) % P;
        c.a[1][1] = ((ll)a[1][0] * b.a[0][1] + (ll)a[1][1] * b.a[1][1]) % P;
        return c;
    }
} A, B;

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

inline void work() {
    if (n & 1) return (void)puts("0");
    n >>= 1;
    if (n == 1) return (void)puts("0");
    if (n == 2) return (void)puts("1");
    if (n == 3) return (void)puts("2");
    B.a[0][0] = 2, B.a[1][0] = 1;
    A.a[0][0] = 3, A.a[0][1] = -1;
    A.a[1][0] = 1, A.a[1][1] = 0;
    B = fpow(A, n - 3) * B;
    int ans = smod(B.a[0][0] - n + 1 + P);
    printf("%d\n", ans);
}

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

Guess you like

Origin www.cnblogs.com/hankeke/p/bzoj2476.html