#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } const int MOD = 1e9 + 7; void add(int& a, int b) { a += b; if (a >= MOD) { a -= MOD; } } int want[605][605][2]; pair<int, vector<vector<int>>> solve(vector<bool> s, bool do_reverse) { if (do_reverse) { reverse(s.begin(), s.end()); for (int i = 0; i < (int) s.size(); i++) { s[i] = !s[i]; } } int n = s.size(); for (int i = 0; i <= n; i++) { for (int b = 0; b <= i; b++) { want[i][b][0] = want[i][b][1] = 0; } } // vector<vector<vector<int>>> want(n+1, vector<vector<int>>(n+1, vector<int>(2))); // want[i][balance][next_bit]; int answer = 0; want[0][0][0] = want[0][0][1] = 1; vector<vector<int>> already(n+1, vector<int>(2)); int MAX = 0; for (int i = 0; i < n; i++) { for (int bal = 0; bal <= MAX; bal++) { // 1) do nothing add(want[i+1][bal][s[i]^1], want[i][bal][s[i]^1]); // 2) take s[i] const int new_bal = bal + (s[i] == 0 ? +1 : -1); if (new_bal < 0) { continue; } const int ways = want[i][bal][s[i]]; if (ways == 0) { continue; } if (do_reverse) { add(already[new_bal][s[i]], ways); } if (new_bal == 0) { add(answer, ways); } for (int z : {0, 1}) { add(want[i+1][new_bal][z], ways); } } if (s[i] == 0) MAX++; } vector<vector<int>> ret = already; if (!do_reverse) { for (int i = 0; i <= n; i++) { ret[i] = {want[n][i][0], want[n][i][1]}; } } while (!ret.empty() && ret.back() == vector<int>{0, 0}) { ret.pop_back(); } return {answer, ret}; } #define want local_want int main() { int n; scanf("%d", &n); vector<vector<bool>> all(n); for (vector<bool>& v : all) { static char s[1005]; scanf("%s", s); int len = strlen(s); v.resize(len); for (int i = 0; i < len; i++) { v[i] = (s[i] == 'P'); } } vector<int> inside(n); vector<vector<vector<int>>> want(n); vector<vector<vector<int>>> got(n); for (int i = 0; i < n; i++) { auto p = solve(all[i], false); inside[i] = p.first; want[i] = p.second; } for (int i = 0; i < n; i++) { got[i] = solve(all[i], true).second; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { unsigned long long total = inside[i]; const int S = min(want[i].size(), got[j].size()); for (int bal = 0; bal < S; bal++) { total += (unsigned long long) want[i][bal][0] * got[j][bal][1] + (unsigned long long) want[i][bal][1] * got[j][bal][0]; if (bal % 8 == 0) { total %= MOD; } } printf("%llu ", total % MOD); } puts(""); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; } const int MOD = 1e9 + 7; void add(int& a, int b) { a += b; if (a >= MOD) { a -= MOD; } } int want[605][605][2]; pair<int, vector<vector<int>>> solve(vector<bool> s, bool do_reverse) { if (do_reverse) { reverse(s.begin(), s.end()); for (int i = 0; i < (int) s.size(); i++) { s[i] = !s[i]; } } int n = s.size(); for (int i = 0; i <= n; i++) { for (int b = 0; b <= i; b++) { want[i][b][0] = want[i][b][1] = 0; } } // vector<vector<vector<int>>> want(n+1, vector<vector<int>>(n+1, vector<int>(2))); // want[i][balance][next_bit]; int answer = 0; want[0][0][0] = want[0][0][1] = 1; vector<vector<int>> already(n+1, vector<int>(2)); int MAX = 0; for (int i = 0; i < n; i++) { for (int bal = 0; bal <= MAX; bal++) { // 1) do nothing add(want[i+1][bal][s[i]^1], want[i][bal][s[i]^1]); // 2) take s[i] const int new_bal = bal + (s[i] == 0 ? +1 : -1); if (new_bal < 0) { continue; } const int ways = want[i][bal][s[i]]; if (ways == 0) { continue; } if (do_reverse) { add(already[new_bal][s[i]], ways); } if (new_bal == 0) { add(answer, ways); } for (int z : {0, 1}) { add(want[i+1][new_bal][z], ways); } } if (s[i] == 0) MAX++; } vector<vector<int>> ret = already; if (!do_reverse) { for (int i = 0; i <= n; i++) { ret[i] = {want[n][i][0], want[n][i][1]}; } } while (!ret.empty() && ret.back() == vector<int>{0, 0}) { ret.pop_back(); } return {answer, ret}; } #define want local_want int main() { int n; scanf("%d", &n); vector<vector<bool>> all(n); for (vector<bool>& v : all) { static char s[1005]; scanf("%s", s); int len = strlen(s); v.resize(len); for (int i = 0; i < len; i++) { v[i] = (s[i] == 'P'); } } vector<int> inside(n); vector<vector<vector<int>>> want(n); vector<vector<vector<int>>> got(n); for (int i = 0; i < n; i++) { auto p = solve(all[i], false); inside[i] = p.first; want[i] = p.second; } for (int i = 0; i < n; i++) { got[i] = solve(all[i], true).second; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { unsigned long long total = inside[i]; const int S = min(want[i].size(), got[j].size()); for (int bal = 0; bal < S; bal++) { total += (unsigned long long) want[i][bal][0] * got[j][bal][1] + (unsigned long long) want[i][bal][1] * got[j][bal][0]; if (bal % 8 == 0) { total %= MOD; } } printf("%llu ", total % MOD); } puts(""); } } |