Submission #1013584


Source Code Expand

#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

typedef long long i64;

const int MOD = (int) 1e9 + 7;
const int K = 3;

int Pow(int x, int64_t y) {
    int ret = 1;
    for (; y > 0; y /= 2) {
        if (y & 1) {
            ret = (int64_t) ret * x % MOD;
        }
        x = (int64_t) x * x % MOD;
    }
    return ret;
}

struct Matrix {
    int a[K][K];
    Matrix():
        a{0} {}
    Matrix operator*(const Matrix& other) const {
        Matrix res;
        for (int i = 0; i < K; ++i) {
            for (int j = 0; j < K; ++j) {
                for (int k = 0; k < K; ++k) {
                    res.a[i][j] = (res.a[i][j] + (int64_t) a[i][k] * other.a[k][j]) % MOD;
                }
            }
        }
        return res;
    }

    static Matrix identity() {
        Matrix res;
        for (int i = 0; i < K; ++i) {
            res.a[i][i] = 1;
        }
        return res;
    }
};

Matrix Pow(Matrix x, int64_t y) {
    Matrix ret = Matrix::identity();
    for (; y > 0; y /= 2) {
        if (y & 1) {
            ret = ret * x;
        }
        x = x * x;
    }
    return ret;
}

vector<string> rotate(const vector<string>& a) {
    int n = SZ(a);
    int m = SZ(a[0]);
    vector<string> b(m, string(n, '.'));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            b[j][n - i - 1] = a[i][j];
        }
    }
    return b;
}

int main() {
    #ifdef LOCAL_RUN
    freopen("task.in", "r", stdin);
    freopen("task.out", "w", stdout);
    //freopen("task.err", "w", stderr);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);

    int h, w;
    cin >> h >> w;

    int64_t k;
    cin >> k;

    vector<string> mat(h);
    for (int i = 0; i < h; ++i) {
        cin >> mat[i];
    }

    bool connectedVerticaly = false, connectedHorizontaly = false;
    for (int i = 0; i < h; ++i) {
        if (mat[i][0] == '#' && mat[i][w - 1] == '#') {
            connectedHorizontaly = true;
        }
    }
    for (int j = 0; j < w; ++j) {
        if (mat[0][j] == '#' && mat[h - 1][j] == '#') {
            connectedVerticaly = true;
        }
    }
    if (k == 0) {
        cout << "1\n";
    }
    if (connectedVerticaly && connectedHorizontaly) {
        cout << "1\n";
    } else if (!connectedVerticaly && !connectedHorizontaly) {
        int cnt = 0;
        for (const string& p: mat) {
            cnt += count(p.begin(), p.end(), '#');
        }
        cout << Pow(cnt, k - 1) << '\n';
    } else {
        if (connectedHorizontaly) {
            mat = rotate(mat);
            swap(h, w);
        }
        int x = 0, y = 0, z = 0;
        for (const string& p: mat) {
            y += count(p.begin(), p.end(), '#');
        }
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                int add = mat[i][j] == '#' && (i == 0 || mat[i - 1][j] != '#');
                x += add;
                y -= add;
            }
        }
        for (int j = 0; j < w; ++j) {
            int add = mat[0][j] == '#' && mat[h - 1][j] == '#';
            z += add;
            x -= add;
        }
        Matrix ans;
        ans.a[0][0] = x + z;
        ans.a[0][1] = x;
        ans.a[0][2] = x;
        ans.a[1][0] = y;
        ans.a[1][1] = y + z;
        ans.a[1][2] = y;
        ans.a[2][0] = 0;
        ans.a[2][1] = 0;
        ans.a[2][2] = z;

        ans = Pow(ans, k - 1);
        int fans = (ans.a[0][2] + ans.a[2][2]) % MOD;
        cout << fans << '\n';
    }
}

Submission Info

Submission Time
Task F - Fraction of Fractal
User AndreiNet
Language C++14 (GCC 5.4.1)
Score 1700
Code Size 3656 Byte
Status AC
Exec Time 13 ms
Memory 2304 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1700 / 1700
Status
AC × 3
AC × 47
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 5 ms 1280 KB
02.txt AC 12 ms 2304 KB
03.txt AC 8 ms 1280 KB
04.txt AC 12 ms 2304 KB
05.txt AC 8 ms 1280 KB
06.txt AC 6 ms 1280 KB
07.txt AC 6 ms 1280 KB
08.txt AC 5 ms 1280 KB
09.txt AC 12 ms 2304 KB
10.txt AC 8 ms 1280 KB
11.txt AC 5 ms 1280 KB
12.txt AC 11 ms 2304 KB
13.txt AC 12 ms 2304 KB
14.txt AC 6 ms 1280 KB
15.txt AC 6 ms 1280 KB
16.txt AC 6 ms 1280 KB
17.txt AC 12 ms 2304 KB
18.txt AC 13 ms 2304 KB
19.txt AC 13 ms 2304 KB
20.txt AC 8 ms 1280 KB
21.txt AC 9 ms 1280 KB
22.txt AC 6 ms 1280 KB
23.txt AC 7 ms 1280 KB
24.txt AC 7 ms 1280 KB
25.txt AC 7 ms 1280 KB
26.txt AC 11 ms 2304 KB
27.txt AC 7 ms 1280 KB
28.txt AC 11 ms 2304 KB
29.txt AC 3 ms 256 KB
30.txt AC 3 ms 256 KB
31.txt AC 3 ms 256 KB
32.txt AC 3 ms 256 KB
33.txt AC 3 ms 256 KB
34.txt AC 3 ms 256 KB
35.txt AC 3 ms 256 KB
36.txt AC 3 ms 256 KB
37.txt AC 3 ms 256 KB
38.txt AC 3 ms 256 KB
39.txt AC 3 ms 256 KB
40.txt AC 3 ms 256 KB
41.txt AC 3 ms 256 KB
42.txt AC 3 ms 256 KB
43.txt AC 3 ms 256 KB
44.txt AC 3 ms 256 KB
s1.txt AC 3 ms 256 KB
s2.txt AC 3 ms 256 KB
s3.txt AC 3 ms 256 KB