#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c* a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if <sizeof spk<c>(0) != 1, stream&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if <sizeof spk<c>(0) == 1, stream&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream& operator<<(pair <a,b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream& operator<<(rge<c> d) { *this << "["; for (auto it=d.b; it!=d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream& _dbg(const string& s, int i, int b) { return *this; } template <class c, class ... cs> stream& _dbg(const string& s, int i, int b, c arg, cs ... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i])._dbg(s, i + 1, b, arg, args...); } };} #define dout debug::stream() #define dbg(...) ((dout << ">> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #define f first #define s second #define sc scanf #define pr printf #define mp make_pair #define pb push_back #define all(x) x.begin(),x.end() #define ss(x) ((int)((x).size())) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(b);i>=(a);i--) #define upgrade cin.tie(0)->sync_with_stdio(0); #define lowb(v,x) (lower_bound(all(v),(x))-v.begin()) #define uppb(v,x) (upper_bound(all(v),(x))-v.begin()) #define erase_duplicates(x) {sort(all(x));x.resize(unique(all(x))-x.begin());} template <class p, class q> pair<p,q> operator-(pair<p,q> a, pair<p,q> b) { return mp(a.f - b.f, a.s - b.s); } template <class p, class q> pair<p,q> operator+(pair<p,q> a, pair<p,q> b) { return mp(a.f + b.f, a.s + b.s); } template <class p, class q> void umin(p &a, const q &b) { if (a > b) a = b; } template <class p, class q> void umax(p &a, const q &b) { if (a < b) a = b; } using lf=long double; using ll=long long; using cll=const ll; using cint=const int; using pll=pair<ll,ll>; using pii=pair<int,int>; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // const int mod = 1000000007; const int N = 605; char s[N]; int dp1[N][N][2][2][2]; int dp2[N][N][2][2][2]; int out[N][N]; int prel[N][N]; int prer[N][N]; int sufl[N][N]; int sufr[N][N]; void add(int& a, const int& b){ a += b; if (a >= mod){ a -= mod; } } void makedp(char* s, int who, bool reversed) { static int dp[N][2][2][2]; int len = strlen(s + 1); if (reversed){ reverse(s + 1, s + len + 1); rep(i, 1, len){ if (s[i] == 'L'){ s[i] = 'P'; }else{ s[i] = 'L'; } } } rep(bil, 0, len) rep(l, 0, 1) rep(r, 0, 1) rep(f, 0, 1){ dp[bil][l][r][f] = 0; } dp[0][0][0][0] = 1; rep(i, 0, len - 1){ char c = s[i + 1]; if (c == 'L') per(bil, 0, i) rep(f, 0, 1){ add(dp[bil][1][0][f], dp[bil][0][0][f]); add(dp[bil][1][1][f], dp[bil][0][1][f]); add(dp[bil + 1][0][0][0], dp[bil][0][0][f]); add(dp[bil + 1][0][0][0], dp[bil][0][1][f]); dp[bil][0][0][f] = 0; dp[bil][0][1][f] = 0; } if (c == 'P') rep(bil, 1, i) rep(f, 0, 1){ add(dp[bil][0][1][f], dp[bil][0][0][f]); add(dp[bil][1][1][f], dp[bil][1][0][f]); add(dp[bil - 1][0][0][1], dp[bil][0][0][f]); add(dp[bil - 1][0][0][1], dp[bil][1][0][f]); dp[bil][0][0][f] = 0; dp[bil][1][0][f] = 0; } } rep(bil, 0, len) rep(l, 0, 1) rep(r, 0, 1) rep(f, 0, 1){ if (dp[bil][l][r][f] == 0) continue; (reversed ? dp2 : dp1)[who][bil][l][r][f] = dp[bil][l][r][f]; } } int main() { int n; scanf("%d", &n); rep(i, 1, n){ scanf("%s", s + 1); makedp(s, i, 0); makedp(s, i, 1); } rep(i, 1, n) rep(bil, 0, 600) rep(l, 0, 1) rep(r, 0, 1) rep(f, 0, 1){ int val1 = dp1[i][bil][l][r][f]; int val2 = dp2[i][bil][l][r][f]; if (l == 0) add(prel[i][bil], val1); if (r == 0) add(prer[i][bil], val1); if (f == 0) add(sufr[i][bil], val2); if (f == 1) add(sufl[i][bil], val2); } rep(i, 1, n) rep(j, 1, n) rep(bil, 0, 600){ add(out[i][j], 1ll * prel[i][bil] * sufl[j][bil] % mod); add(out[i][j], 1ll * prer[i][bil] * sufr[j][bil] % mod); } rep(i, 1, n){ rep(j, 1, n){ add(out[i][j], mod - 1); printf("%d ", out[i][j]); } printf("\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 | #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char spk(...); template <class c> auto spk(c* a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if <sizeof spk<c>(0) != 1, stream&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if <sizeof spk<c>(0) == 1, stream&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream& operator<<(pair <a,b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream& operator<<(rge<c> d) { *this << "["; for (auto it=d.b; it!=d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream& _dbg(const string& s, int i, int b) { return *this; } template <class c, class ... cs> stream& _dbg(const string& s, int i, int b, c arg, cs ... args) { if (i == (int)(s.size())) return (*this << ": " << arg); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') - (s[i] == ']') - (s[i] == '}'); return (s[i] == ',' && b == 0) ? (*this << ": " << arg << " ")._dbg(s, i + 1, b, args...) : (s[i] == ' ' ? *this : *this << s[i])._dbg(s, i + 1, b, arg, args...); } };} #define dout debug::stream() #define dbg(...) ((dout << ">> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) #define f first #define s second #define sc scanf #define pr printf #define mp make_pair #define pb push_back #define all(x) x.begin(),x.end() #define ss(x) ((int)((x).size())) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(b);i>=(a);i--) #define upgrade cin.tie(0)->sync_with_stdio(0); #define lowb(v,x) (lower_bound(all(v),(x))-v.begin()) #define uppb(v,x) (upper_bound(all(v),(x))-v.begin()) #define erase_duplicates(x) {sort(all(x));x.resize(unique(all(x))-x.begin());} template <class p, class q> pair<p,q> operator-(pair<p,q> a, pair<p,q> b) { return mp(a.f - b.f, a.s - b.s); } template <class p, class q> pair<p,q> operator+(pair<p,q> a, pair<p,q> b) { return mp(a.f + b.f, a.s + b.s); } template <class p, class q> void umin(p &a, const q &b) { if (a > b) a = b; } template <class p, class q> void umax(p &a, const q &b) { if (a < b) a = b; } using lf=long double; using ll=long long; using cll=const ll; using cint=const int; using pll=pair<ll,ll>; using pii=pair<int,int>; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // const int mod = 1000000007; const int N = 605; char s[N]; int dp1[N][N][2][2][2]; int dp2[N][N][2][2][2]; int out[N][N]; int prel[N][N]; int prer[N][N]; int sufl[N][N]; int sufr[N][N]; void add(int& a, const int& b){ a += b; if (a >= mod){ a -= mod; } } void makedp(char* s, int who, bool reversed) { static int dp[N][2][2][2]; int len = strlen(s + 1); if (reversed){ reverse(s + 1, s + len + 1); rep(i, 1, len){ if (s[i] == 'L'){ s[i] = 'P'; }else{ s[i] = 'L'; } } } rep(bil, 0, len) rep(l, 0, 1) rep(r, 0, 1) rep(f, 0, 1){ dp[bil][l][r][f] = 0; } dp[0][0][0][0] = 1; rep(i, 0, len - 1){ char c = s[i + 1]; if (c == 'L') per(bil, 0, i) rep(f, 0, 1){ add(dp[bil][1][0][f], dp[bil][0][0][f]); add(dp[bil][1][1][f], dp[bil][0][1][f]); add(dp[bil + 1][0][0][0], dp[bil][0][0][f]); add(dp[bil + 1][0][0][0], dp[bil][0][1][f]); dp[bil][0][0][f] = 0; dp[bil][0][1][f] = 0; } if (c == 'P') rep(bil, 1, i) rep(f, 0, 1){ add(dp[bil][0][1][f], dp[bil][0][0][f]); add(dp[bil][1][1][f], dp[bil][1][0][f]); add(dp[bil - 1][0][0][1], dp[bil][0][0][f]); add(dp[bil - 1][0][0][1], dp[bil][1][0][f]); dp[bil][0][0][f] = 0; dp[bil][1][0][f] = 0; } } rep(bil, 0, len) rep(l, 0, 1) rep(r, 0, 1) rep(f, 0, 1){ if (dp[bil][l][r][f] == 0) continue; (reversed ? dp2 : dp1)[who][bil][l][r][f] = dp[bil][l][r][f]; } } int main() { int n; scanf("%d", &n); rep(i, 1, n){ scanf("%s", s + 1); makedp(s, i, 0); makedp(s, i, 1); } rep(i, 1, n) rep(bil, 0, 600) rep(l, 0, 1) rep(r, 0, 1) rep(f, 0, 1){ int val1 = dp1[i][bil][l][r][f]; int val2 = dp2[i][bil][l][r][f]; if (l == 0) add(prel[i][bil], val1); if (r == 0) add(prer[i][bil], val1); if (f == 0) add(sufr[i][bil], val2); if (f == 1) add(sufl[i][bil], val2); } rep(i, 1, n) rep(j, 1, n) rep(bil, 0, 600){ add(out[i][j], 1ll * prel[i][bil] * sufl[j][bil] % mod); add(out[i][j], 1ll * prer[i][bil] * sufr[j][bil] % mod); } rep(i, 1, n){ rep(j, 1, n){ add(out[i][j], mod - 1); printf("%d ", out[i][j]); } printf("\n"); } return 0; } |