#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define pb push_back #define eb emplace_back #define mp make_pair #define f first #define s second #define tcT template<class T #define tcTU tcT, class U #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define rrep(i,a,b) for(int i = (b) - 1; i >= (a); i--) const db PI = acos(-1); mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); tcT> bool ckmin(T& a, const T& b) { return b < a ? a=b, 1 : 0; } tcT> bool ckmax(T& a, const T& b) { return a < b ? a=b, 1 : 0; } tcT> void _dbg(const char *sdbg, T h){ cerr<<sdbg<<'='<<h<<endl; } tcT, class... TA> void _dbg(const char *sdbg, T h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } tcT> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } tcT, class V> ostream &operator<<(ostream &os, pair<T,V> P) { return os << "(" << P.f << "," << P.s << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } int v; explicit operator int() const { return v; } mint():v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; constexpr int MOD = 1e9+7; using mi = mint<MOD,5>; using vmi = vector<mi>; using pmi = pair<mi,mi>; using vpmi = vector<pmi>; ostream& operator<<(ostream &os, mi x) { return os << x.v; } constexpr int MAXN = 605; mi dpl[2][MAXN][MAXN]; mi dpr[2][MAXN][MAXN]; string s[MAXN]; int n; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; rep(i,0,n) cin >> s[i]; rep(side,0,2) { rep(id,0,n) { if (side == 1) { reverse(all(s[id])); rep(i,0,sz(s[id])) { s[id][i] = (s[id][i] == 'P' ? 'L' : 'P'); } } fill(dpl[side][id], dpl[side][id]+MAXN, 0); fill(dpr[side][id], dpr[side][id]+MAXN, 0); dpl[side][id][0] = 1; rep(i,0,sz(s[id])) { if (s[id][i] == 'L') { rrep(j,1,i+2) { dpl[side][id][j] = dpl[side][id][j-1] + dpr[side][id][j-1]; } } else { rep(j,0,i+2) { dpr[side][id][j] = dpl[side][id][j+1] + dpr[side][id][j+1]; } } } } } rep(id1,0,n) { rep(id2,0,n) { mi res = dpr[0][id1][0]; res += (dpl[0][id1][0] + dpr[0][id1][0] - dpl[0][id1][1]) * dpr[1][id2][0]; rep(k,1,sz(s[id1])+1) { res += (dpl[0][id1][k] + dpr[0][id1][k] - dpl[0][id1][k+1]) * dpr[1][id2][k]; res += (dpl[0][id1][k] + dpr[0][id1][k] - dpr[0][id1][k-1]) * dpl[1][id2][k]; } cout << res << " "; } cout << "\n"; } 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ull = unsigned long long; using pi = pair<int, int>; using pl = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define pb push_back #define eb emplace_back #define mp make_pair #define f first #define s second #define tcT template<class T #define tcTU tcT, class U #define sz(x) int((x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define rep(i,a,b) for(int i = (a); i < (b); i++) #define rrep(i,a,b) for(int i = (b) - 1; i >= (a); i--) const db PI = acos(-1); mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); tcT> bool ckmin(T& a, const T& b) { return b < a ? a=b, 1 : 0; } tcT> bool ckmax(T& a, const T& b) { return a < b ? a=b, 1 : 0; } tcT> void _dbg(const char *sdbg, T h){ cerr<<sdbg<<'='<<h<<endl; } tcT, class... TA> void _dbg(const char *sdbg, T h, TA... a) { while(*sdbg!=',') cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } tcT> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } tcT, class V> ostream &operator<<(ostream &os, pair<T,V> P) { return os << "(" << P.f << "," << P.s << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } int v; explicit operator int() const { return v; } mint():v(0) {} mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } bool operator==(const mint& o) const { return v == o.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } mint& operator+=(const mint& o) { if ((v += o.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& o) { if ((v -= o.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& o) { v = int((ll)v*o.v%MOD); return *this; } mint& operator/=(const mint& o) { return (*this) *= inv(o); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; constexpr int MOD = 1e9+7; using mi = mint<MOD,5>; using vmi = vector<mi>; using pmi = pair<mi,mi>; using vpmi = vector<pmi>; ostream& operator<<(ostream &os, mi x) { return os << x.v; } constexpr int MAXN = 605; mi dpl[2][MAXN][MAXN]; mi dpr[2][MAXN][MAXN]; string s[MAXN]; int n; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; rep(i,0,n) cin >> s[i]; rep(side,0,2) { rep(id,0,n) { if (side == 1) { reverse(all(s[id])); rep(i,0,sz(s[id])) { s[id][i] = (s[id][i] == 'P' ? 'L' : 'P'); } } fill(dpl[side][id], dpl[side][id]+MAXN, 0); fill(dpr[side][id], dpr[side][id]+MAXN, 0); dpl[side][id][0] = 1; rep(i,0,sz(s[id])) { if (s[id][i] == 'L') { rrep(j,1,i+2) { dpl[side][id][j] = dpl[side][id][j-1] + dpr[side][id][j-1]; } } else { rep(j,0,i+2) { dpr[side][id][j] = dpl[side][id][j+1] + dpr[side][id][j+1]; } } } } } rep(id1,0,n) { rep(id2,0,n) { mi res = dpr[0][id1][0]; res += (dpl[0][id1][0] + dpr[0][id1][0] - dpl[0][id1][1]) * dpr[1][id2][0]; rep(k,1,sz(s[id1])+1) { res += (dpl[0][id1][k] + dpr[0][id1][k] - dpl[0][id1][k+1]) * dpr[1][id2][k]; res += (dpl[0][id1][k] + dpr[0][id1][k] - dpr[0][id1][k-1]) * dpl[1][id2][k]; } cout << res << " "; } cout << "\n"; } return 0; } |