#include <bits/stdc++.h> #include <unistd.h> using namespace std; constexpr int MOD = 1'000'000'007; static inline void add(int& a, const int b) { a += b; if (a >= MOD) { a -= MOD; } } constexpr int MAXN = 607; string s[MAXN]; int sz[MAXN]; int cntL[MAXN], cntP[MAXN]; int dp[MAXN][MAXN][8]; // pref, sum, bitmask(last, canL, canP) int last_row[2][MAXN][MAXN][8]; void clear(int id) { for (int i = 0; i <= sz[id]; i++) { for (int sum = 0; sum <= i; sum++) { for (int bitmask = 0; bitmask < 8; bitmask++) { dp[i][sum][bitmask] = 0; } } } } void calc(int id) { for (int rep = 0; rep < 2; rep++) { clear(id); dp[0][0][2] = 1; int max_sum = 0; for (int i = 0; i < sz[id]; i++) { if (s[id][i] == 'L') { for (int bitmask = 0; bitmask < 8; bitmask++) { for (int sum = 0; sum <= max_sum; sum++) { add(dp[i + 1][sum][bitmask & 5], dp[i][sum][bitmask]); } if (bitmask & 2) { // if (canL) for (int sum = 0; sum <= max_sum; sum++) { add(dp[i + 1][sum + 1][3], dp[i][sum][bitmask]); } } } max_sum++; } else { for (int bitmask = 0; bitmask < 8; bitmask++) { for (int sum = 0; sum <= max_sum; sum++) { add(dp[i + 1][sum][bitmask & 6], dp[i][sum][bitmask]); } if (bitmask & 1) { // if (canP && sum > 0) for (int sum = 1; sum <= max_sum; sum++) { add(dp[i + 1][sum - 1][7], dp[i][sum][bitmask]); } } } } } if (rep == 0) { for (int sum = 0; sum <= sz[id]; sum++) { for (int bitmask = 0; bitmask < 8; bitmask++) { last_row[rep][id][sum][bitmask] = dp[sz[id]][sum][bitmask]; } } reverse(s[id].begin(), s[id].end()); for (int i = 0; i < sz[id]; i++) { if (s[id][i] == 'L') { s[id][i] = 'P'; } else { s[id][i] = 'L'; } } } else { for (int sum = 0; sum <= sz[id]; sum++) { for (int bitmask = 0; bitmask < 8; bitmask++) { int new_bitmask = bitmask ^ 7; if ((bitmask & 1) != (bitmask >> 1 & 1)) { new_bitmask ^= 3; } last_row[rep][id][sum][new_bitmask] = dp[sz[id]][sum][bitmask]; } } } } } int result[MAXN][MAXN]; char bufor[MAXN * MAXN * 13]; int bufor_last = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; // n = 600; for (int i = 0; i < n; i++) { cin >> s[i]; // for (int j = 0; j < 600; j++) { // if (rand() % 2 == 0) { // s[i] += 'L'; // } // else { // s[i] += 'P'; // } // } sz[i] = s[i].size(); for (int j = 0; j < sz[i]; j++) { if (s[i][j] == 'L') { cntL[i]++; } else { cntP[i]++; } } } for (int i = 0; i < n; i++) { calc(i); } int tmp; int sum; long long totalL, totalP, chuj1, chuj2; for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { tmp = 0; sum = 0; totalL = ((long long) last_row[1][j][sum][0] + last_row[1][j][sum][1] + last_row[1][j][sum][2] + last_row[1][j][sum][3]) % MOD; add(tmp, ((last_row[0][i][sum][6] + last_row[0][i][sum][7]) * totalL) % MOD); add(tmp, ((long long) last_row[0][i][sum][4] + last_row[0][i][sum][5] + last_row[0][i][sum][6] + last_row[0][i][sum][7]) % MOD); if (cntL[i] == 0) { add(tmp, totalL); } int bound = min(cntL[i], cntP[j]); for (sum = 1; sum <= bound; sum++) { totalL = ((long long) last_row[1][j][sum][0] + last_row[1][j][sum][1] + last_row[1][j][sum][2] + last_row[1][j][sum][3]) % MOD; totalP = ((long long) last_row[1][j][sum][4] + last_row[1][j][sum][5] + last_row[1][j][sum][6] + last_row[1][j][sum][7]) % MOD; chuj1 = ((long long) last_row[0][i][sum][2] + last_row[0][i][sum][3] + last_row[0][i][sum][6] + last_row[0][i][sum][7]) % MOD; chuj2 = ((long long) last_row[0][i][sum][1] + last_row[0][i][sum][3] + last_row[0][i][sum][5] + last_row[0][i][sum][7]) % MOD; add(tmp, (chuj1 * totalL) % MOD); add(tmp, (chuj2 * totalP) % MOD); } result[i][j] = tmp; } } for (int i = n - 1; i >= 0; i--) { bufor[bufor_last++] = '\n'; for (int j = n - 1; j >= 0; j--) { bufor[bufor_last++] = ' '; int tmp = result[i][j]; do { bufor[bufor_last++] = (tmp % 10 + '0'); tmp /= 10; } while (tmp > 0); } } // cerr << bufor_last << '\n'; reverse(bufor, bufor + bufor_last); write(1, bufor, bufor_last); return 0; }
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <bits/stdc++.h> #include <unistd.h> using namespace std; constexpr int MOD = 1'000'000'007; static inline void add(int& a, const int b) { a += b; if (a >= MOD) { a -= MOD; } } constexpr int MAXN = 607; string s[MAXN]; int sz[MAXN]; int cntL[MAXN], cntP[MAXN]; int dp[MAXN][MAXN][8]; // pref, sum, bitmask(last, canL, canP) int last_row[2][MAXN][MAXN][8]; void clear(int id) { for (int i = 0; i <= sz[id]; i++) { for (int sum = 0; sum <= i; sum++) { for (int bitmask = 0; bitmask < 8; bitmask++) { dp[i][sum][bitmask] = 0; } } } } void calc(int id) { for (int rep = 0; rep < 2; rep++) { clear(id); dp[0][0][2] = 1; int max_sum = 0; for (int i = 0; i < sz[id]; i++) { if (s[id][i] == 'L') { for (int bitmask = 0; bitmask < 8; bitmask++) { for (int sum = 0; sum <= max_sum; sum++) { add(dp[i + 1][sum][bitmask & 5], dp[i][sum][bitmask]); } if (bitmask & 2) { // if (canL) for (int sum = 0; sum <= max_sum; sum++) { add(dp[i + 1][sum + 1][3], dp[i][sum][bitmask]); } } } max_sum++; } else { for (int bitmask = 0; bitmask < 8; bitmask++) { for (int sum = 0; sum <= max_sum; sum++) { add(dp[i + 1][sum][bitmask & 6], dp[i][sum][bitmask]); } if (bitmask & 1) { // if (canP && sum > 0) for (int sum = 1; sum <= max_sum; sum++) { add(dp[i + 1][sum - 1][7], dp[i][sum][bitmask]); } } } } } if (rep == 0) { for (int sum = 0; sum <= sz[id]; sum++) { for (int bitmask = 0; bitmask < 8; bitmask++) { last_row[rep][id][sum][bitmask] = dp[sz[id]][sum][bitmask]; } } reverse(s[id].begin(), s[id].end()); for (int i = 0; i < sz[id]; i++) { if (s[id][i] == 'L') { s[id][i] = 'P'; } else { s[id][i] = 'L'; } } } else { for (int sum = 0; sum <= sz[id]; sum++) { for (int bitmask = 0; bitmask < 8; bitmask++) { int new_bitmask = bitmask ^ 7; if ((bitmask & 1) != (bitmask >> 1 & 1)) { new_bitmask ^= 3; } last_row[rep][id][sum][new_bitmask] = dp[sz[id]][sum][bitmask]; } } } } } int result[MAXN][MAXN]; char bufor[MAXN * MAXN * 13]; int bufor_last = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; // n = 600; for (int i = 0; i < n; i++) { cin >> s[i]; // for (int j = 0; j < 600; j++) { // if (rand() % 2 == 0) { // s[i] += 'L'; // } // else { // s[i] += 'P'; // } // } sz[i] = s[i].size(); for (int j = 0; j < sz[i]; j++) { if (s[i][j] == 'L') { cntL[i]++; } else { cntP[i]++; } } } for (int i = 0; i < n; i++) { calc(i); } int tmp; int sum; long long totalL, totalP, chuj1, chuj2; for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { tmp = 0; sum = 0; totalL = ((long long) last_row[1][j][sum][0] + last_row[1][j][sum][1] + last_row[1][j][sum][2] + last_row[1][j][sum][3]) % MOD; add(tmp, ((last_row[0][i][sum][6] + last_row[0][i][sum][7]) * totalL) % MOD); add(tmp, ((long long) last_row[0][i][sum][4] + last_row[0][i][sum][5] + last_row[0][i][sum][6] + last_row[0][i][sum][7]) % MOD); if (cntL[i] == 0) { add(tmp, totalL); } int bound = min(cntL[i], cntP[j]); for (sum = 1; sum <= bound; sum++) { totalL = ((long long) last_row[1][j][sum][0] + last_row[1][j][sum][1] + last_row[1][j][sum][2] + last_row[1][j][sum][3]) % MOD; totalP = ((long long) last_row[1][j][sum][4] + last_row[1][j][sum][5] + last_row[1][j][sum][6] + last_row[1][j][sum][7]) % MOD; chuj1 = ((long long) last_row[0][i][sum][2] + last_row[0][i][sum][3] + last_row[0][i][sum][6] + last_row[0][i][sum][7]) % MOD; chuj2 = ((long long) last_row[0][i][sum][1] + last_row[0][i][sum][3] + last_row[0][i][sum][5] + last_row[0][i][sum][7]) % MOD; add(tmp, (chuj1 * totalL) % MOD); add(tmp, (chuj2 * totalP) % MOD); } result[i][j] = tmp; } } for (int i = n - 1; i >= 0; i--) { bufor[bufor_last++] = '\n'; for (int j = n - 1; j >= 0; j--) { bufor[bufor_last++] = ' '; int tmp = result[i][j]; do { bufor[bufor_last++] = (tmp % 10 + '0'); tmp /= 10; } while (tmp > 0); } } // cerr << bufor_last << '\n'; reverse(bufor, bufor + bufor_last); write(1, bufor, bufor_last); return 0; } |