Submission #5075702


Source Code Expand

Copy
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

cat pw(cat a, cat e, cat mod) {
	if(e <= 0) return 1;
	cat x = pw(a, e/2, mod);
	x = x * x % mod;
	if(e & 1) x = x * a % mod;
	return x;
}

vector< vector<cat> > prod(vector< vector<cat> > & A, vector< vector<cat> > & B, cat mod) {
	int N = A.size();
	vector< vector<cat> > C(N, vector<cat>(N, 0));
	for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) {
		for(int k = 0; k < N; k++) C[i][j] += A[i][k] * B[k][j] % mod;
		C[i][j] %= mod;
	}
	return C;
}

vector< vector<cat> > pw_mat(vector< vector<cat> > & A, cat e, cat mod) {
	int N = A.size();
	if(e <= 0) {
		vector< vector<cat> > ret(N, vector<cat>(N, 0));
		for(int i = 0; i < N; i++) ret[i][i] = 1;
		return ret;
	}
	vector< vector<cat> > x = pw_mat(A, e/2, mod);
	x = prod(x, x, mod);
	if(e & 1) x = prod(x, A, mod);
	return x;
}

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int H, W;
	cat K, mod = 1000000007;
	cin >> H >> W >> K;
	if(K <= 1) {
		cout << "1\n";
		return 0;
	}
	vector<string> S(H);
	for(int i = 0; i < H; i++) cin >> S[i];
	bool conn_h = false, conn_w = false;
	for(int i = 0; i < H; i++)
		if(S[i][0] == '#' && S[i][W-1] == '#') conn_w = true;
	for(int i = 0; i < W; i++)
		if(S[0][i] == '#' && S[H-1][i] == '#') conn_h = true;
	if(conn_h && conn_w) {
		cout << "1\n";
		return 0;
	}
	if(!conn_h && !conn_w) {
		cat cnt = 0;
		for(int i = 0; i < H; i++) for(int j = 0; j < W; j++)
			if(S[i][j] == '#') cnt++;
		cat ans = pw(cnt, K-1, mod);
		cout << ans << "\n";
		return 0;
	}
	vector<int> seg0;
	vector< pair<int, int> > seg1;
	int seg2 = 0;
	if(conn_h) {
		vector<string> St(W);
		for(int i = 0; i < H; i++) for(int j = 0; j < W; j++)
			St[j] += S[i][j];
		S = St;
		swap(H, W);
	}
	for(int i = 0; i < H; i++) {
		int l0 = 0, r0 = 0;
		if(S[i][0] == '#' && S[i][W-1] == '#') {
			for(int j = 0; j < W; j++) {
				if(S[i][j] == '.') break;
				l0++;
			}
			for(int j = W-1; j >= 0; j--) {
				if(S[i][j] == '.') break;
				r0++;
			}
			if(l0 == W) {
				seg2++;
				continue;
			}
			seg1.push_back({l0, r0});
		}
		int l = -1;
		for(int j = 0; j < W; j++) {
			if(S[i][j] == '#') continue;
			if(l != j-1)
				if(l != -1 || (S[i][0] == '.' || S[i][W-1] == '.'))
					seg0.push_back(j-1-l);
			l = j;
		}
		if(l != W-1) 
			if(S[i][0] == '.' || S[i][W-1] == '.') seg0.push_back(W-1-l);
	}
	map<int, int> L;
	L[1] = L[W] = 0;
	ALL_THE(seg0, it) L[*it] = 0;
	ALL_THE(seg1, it) {
		L[it->ff] = 0;
		L[it->ss] = 0;
		L[it->ff+it->ss] = 0;
	}
	int NL = 0;
	ALL_THE(L, it) it->ss = NL++;
	vector< vector<cat> > M(NL+2, vector<cat>(NL+2, 0));
	ALL_THE(L, it) {
		ALL_THE(seg0, jt) M[L[*jt]][it->ss] += it->ff;
		ALL_THE(seg1, jt) {
			M[L[jt->ff+jt->ss]][it->ss] += it->ff-1;
			M[L[jt->ff]][it->ss] += 1;
			M[L[jt->ss]][it->ss] += 1;
		}
		M[NL][it->ss] += seg2;
		M[NL+1][it->ss] = 1LL * seg2 * W * it->ff % mod;
	}
	ALL_THE(seg0, jt) M[L[*jt]][NL+1]++;
	ALL_THE(seg1, jt) {
		M[L[jt->ff+jt->ss]][NL+1]++;
		M[L[jt->ff+jt->ss]][NL]--;
		M[L[jt->ff]][NL]++;
		M[L[jt->ss]][NL]++;
	}
	M[NL][NL] += seg2;
	M[NL+1][NL+1] += W * seg2;
	/* n_l = n; l < W:
		n'_(seg0[i]) += l*n
		n'_(seg1[i]) += l*n
		n'_(seg1[i]) -= n
		n'_(segl[i]) += n
		n'_(segr[i]) += n
		n_(l >= W)   += seg2*n
		s_(l >= W)   += seg2*n*W*l

	   n_(l >= W) = n, sum_(l >= W) = s:
		n'_(seg0[i]) += s
		n'_(seg1[i]) += s
		n'_(seg1[i]) -= n
		n'_(segl[i]) += n
		n'_(segr[i]) += n
		n            += seg2*n
		s            += W*seg2*s
	*/
	vector< vector<cat> > MP = pw_mat(M, K-1, mod);
	cat ans = 0;
	for(int i = 0; i <= NL; i++) ans += MP[i][0];
	ans %= mod;
	if(ans < 0) ans += mod;
	cout << ans << "\n";
	return 0;
}

// look at my code
// my code is amazing

Submission Info

Submission Time
Task F - Fraction of Fractal
User xellos
Language C++14 (GCC 5.4.1)
Score 1700
Code Size 4691 Byte
Status AC
Exec Time 15 ms
Memory 2432 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 3 ms 1280 KB
02.txt AC 5 ms 1280 KB
03.txt AC 15 ms 2432 KB
04.txt AC 5 ms 1280 KB
05.txt AC 15 ms 2432 KB
06.txt AC 4 ms 1280 KB
07.txt AC 4 ms 1280 KB
08.txt AC 3 ms 1280 KB
09.txt AC 5 ms 1280 KB
10.txt AC 15 ms 2432 KB
11.txt AC 3 ms 1280 KB
12.txt AC 5 ms 1280 KB
13.txt AC 5 ms 1280 KB
14.txt AC 4 ms 1280 KB
15.txt AC 4 ms 1280 KB
16.txt AC 4 ms 1280 KB
17.txt AC 10 ms 2424 KB
18.txt AC 5 ms 1280 KB
19.txt AC 5 ms 1280 KB
20.txt AC 15 ms 2432 KB
21.txt AC 14 ms 2432 KB
22.txt AC 4 ms 1280 KB
23.txt AC 15 ms 2432 KB
24.txt AC 15 ms 2432 KB
25.txt AC 14 ms 2432 KB
26.txt AC 4 ms 1280 KB
27.txt AC 15 ms 2432 KB
28.txt AC 5 ms 1280 KB
29.txt AC 1 ms 256 KB
30.txt AC 1 ms 256 KB
31.txt AC 1 ms 256 KB
32.txt AC 1 ms 256 KB
33.txt AC 1 ms 256 KB
34.txt AC 1 ms 256 KB
35.txt AC 1 ms 256 KB
36.txt AC 1 ms 256 KB
37.txt AC 1 ms 256 KB
38.txt AC 1 ms 256 KB
39.txt AC 1 ms 256 KB
40.txt AC 1 ms 256 KB
41.txt AC 1 ms 256 KB
42.txt AC 1 ms 256 KB
43.txt AC 1 ms 256 KB
44.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB
s3.txt AC 2 ms 256 KB