题目
Atcoder
思路
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
const int N = 1010, mod = 1e9 + 7;
int n, m, k, U[2], T[2];
struct MATRIX { int a[2][2]; } A, B;
char g[N][N];
int qmi_num(int a, int b) {
int res = 1;
for (; b; a = a * a % mod, b >>= 1)
if (b & 1) res = res * a % mod;
return res;
}
MATRIX operator*(MATRIX A, MATRIX B) {
static MATRIX C;
memset(C.a, 0, sizeof C.a);
for (int i = 0; i <= 1; i++)
for (int j = 0; j <= 1; j++)
for (int k = 0; k <= 1; k++)
C.a[i][j] = (C.a[i][j] + A.a[i][k] * B.a[k][j]) % mod;
return C;
}
MATRIX qmi(MATRIX A, int k) {
static MATRIX C;
memset(C.a, 0, sizeof C.a);
for (int i = 0; i <= 1; i++) C.a[i][i] = 1;
for (; k; A = A * A, k >>= 1)
if (k & 1) C = C * A;
return C;
}
signed main() {
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) cin >> g[i] + 1;
int cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] == '#') cnt++;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m - 1; j++)
if (g[i][j] == '#' && g[i][j] == g[i][j + 1]) T[0]++;
for (int i = 1; i <= n - 1; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] == '#' && g[i][j] == g[i + 1][j]) T[1]++;
for (int i = 1; i <= n; i++)
if (g[i][1] == '#' && g[i][1] == g[i][m]) U[0]++;
for (int i = 1; i <= m; i++)
if (g[1][i] == '#' && g[1][i] == g[n][i]) U[1]++;
if (U[0] && U[1]) return cout << 1 << endl, 0;
if (!U[0] && !U[1]) return cout << qmi_num(cnt, k - 1) << endl, 0;
int flag = 1;
U[0] ? flag = 0 : flag = 1;
A = (MATRIX) { { { 1, 1 }, { 0, 0 } } };
B = (MATRIX) { { { cnt, 0 }, { -T[flag], U[flag] } } };
cout << ((A * qmi(B, k - 1)).a[0][0] % mod + mod) % mod << endl;
return 0;
}