#include<bits/stdc++.h> using namespace std; #ifdef LOC template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif #define fwd(i, a, b) for(int i=(a);i<(int)(b);i++) #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define sz(X) ((int)(X).size()) #define mp make_pair #define st first #define nd second template <int32_t _MODD> struct modint { int32_t value; modint() = default; modint(int32_t value_) : value(value_) {} inline modint<_MODD> operator + (modint<_MODD> other) const { int32_t c = this->value + other.value; return modint<_MODD>(c >= _MODD ? c - _MODD : c); } inline modint<_MODD> operator - (modint<_MODD> other) const { int32_t c = this->value - other.value; return modint<_MODD>(c < 0 ? c + _MODD : c); } inline modint<_MODD> operator * (modint<_MODD> other) const { int32_t c = (int64_t)this->value * other.value % _MODD; return modint<_MODD>(c < 0 ? c + _MODD : c); } inline modint<_MODD> & operator += (modint<_MODD> other) { this->value += other.value; if (this->value >= _MODD) this->value -= _MODD; return *this; } inline modint<_MODD> & operator -= (modint<_MODD> other) { this->value -= other.value; if (this->value < 0) this->value += _MODD; return *this; } inline modint<_MODD> & operator *= (modint<_MODD> other) { this->value = (int64_t)this->value * other.value % _MODD; if (this->value < 0) this->value += _MODD; return *this; } inline modint<_MODD> operator - () const { return modint<_MODD>(this->value ? _MODD - this->value : 0); } modint<_MODD> pow(uint64_t k) const { modint<_MODD> x = *this, y = 1; for (; k; k >>= 1) { if (k & 1) y *= x; x *= x; } return y; } modint<_MODD> inv() const { return pow(_MODD - 2); } // _MODD must be a prime inline modint<_MODD> operator / (modint<_MODD> other) const { return *this * other.inv(); } inline modint<_MODD> operator /= (modint<_MODD> other) { return *this *= other.inv(); } inline bool operator == (modint<_MODD> other) const { return value == other.value; } inline bool operator != (modint<_MODD> other) const { return value != other.value; } inline bool operator < (modint<_MODD> other) const { return value < other.value; } inline bool operator > (modint<_MODD> other) const { return value > other.value; } }; template <int32_t _MODD> modint<_MODD> operator * (int64_t value, modint<_MODD> n) { return modint<_MODD>(value) * n; } template <int32_t _MODD> modint<_MODD> operator * (int32_t value, modint<_MODD> n) { return modint<_MODD>(value % _MODD) * n; } template <int32_t _MODD> istream & operator >> (istream & in, modint<_MODD> &n) { return in >> n.value; } template <int32_t _MODD> ostream & operator << (ostream & out, modint<_MODD> n) { return out << n.value; } typedef modint<(int)(1e9+7)> mint; typedef vector<mint> vm; struct DPs { vm up; vm down; vm prevLUp; vm prevPDown; }; DPs dp2(const string& s) { int n = sz(s); vm dpUp(n+1); vm dpDown(n+1); dpUp[0] = 1; vm prevLUp(n+1), prevPDown(n+1); fwd(i,1,n+1) { if(s[i-1] == 'L') { for(int l=sz(dpUp)-1; l>=1; l--) dpUp[l] += dpUp[l-1] + dpDown[l-1] - prevLUp[l]; prevLUp = dpUp; } else { for(int l=0; l<sz(dpUp)-1; l++) dpDown[l] += dpUp[l+1] + dpDown[l+1] - prevPDown[l]; prevPDown = dpDown; } } return {dpUp, dpDown, prevLUp, prevPDown}; } vm unextendible(const DPs& dp, const string& s, char dir) { vm res(sz(dp.up)); rep(i, sz(res)) res[i] = dp.up[i] + dp.down[i]; if(count(all(s), dir) > 0) { if(dir == 'L') rep(i,sz(res)-1) res[i] -= dp.prevLUp[i+1]; else fwd(i,1,sz(res)) res[i] -= dp.prevPDown[i-1]; } return res; } mint solveJoint(const DPs& dpb, const vm& ual, const vm& uap) { mint res=0; rep(i,min(sz(ual), sz(dpb.down))) res += ual[i]*dpb.down[i] + uap[i]*dpb.up[i]; return res; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<string> tab(n); rep(i,n) { cin >> tab[i]; } vector<DPs> dpsInv(n); rep(i,n) { string cp = tab[i]; reverse(all(cp)); for(auto &a:cp) a = (a == 'L' ? 'P' : 'L'); dpsInv[i] = dp2(cp); } rep(i,n) { auto dp = dp2(tab[i]); auto uL = unextendible(dp, tab[i], 'L'); auto uR = unextendible(dp, tab[i], 'P'); rep(j,n) cout<<solveJoint(dpsInv[j], uL, uR)-mint(1)<<" "; cout<<"\n"; } }
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 | #include<bits/stdc++.h> using namespace std; #ifdef LOC template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif #define fwd(i, a, b) for(int i=(a);i<(int)(b);i++) #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define sz(X) ((int)(X).size()) #define mp make_pair #define st first #define nd second template <int32_t _MODD> struct modint { int32_t value; modint() = default; modint(int32_t value_) : value(value_) {} inline modint<_MODD> operator + (modint<_MODD> other) const { int32_t c = this->value + other.value; return modint<_MODD>(c >= _MODD ? c - _MODD : c); } inline modint<_MODD> operator - (modint<_MODD> other) const { int32_t c = this->value - other.value; return modint<_MODD>(c < 0 ? c + _MODD : c); } inline modint<_MODD> operator * (modint<_MODD> other) const { int32_t c = (int64_t)this->value * other.value % _MODD; return modint<_MODD>(c < 0 ? c + _MODD : c); } inline modint<_MODD> & operator += (modint<_MODD> other) { this->value += other.value; if (this->value >= _MODD) this->value -= _MODD; return *this; } inline modint<_MODD> & operator -= (modint<_MODD> other) { this->value -= other.value; if (this->value < 0) this->value += _MODD; return *this; } inline modint<_MODD> & operator *= (modint<_MODD> other) { this->value = (int64_t)this->value * other.value % _MODD; if (this->value < 0) this->value += _MODD; return *this; } inline modint<_MODD> operator - () const { return modint<_MODD>(this->value ? _MODD - this->value : 0); } modint<_MODD> pow(uint64_t k) const { modint<_MODD> x = *this, y = 1; for (; k; k >>= 1) { if (k & 1) y *= x; x *= x; } return y; } modint<_MODD> inv() const { return pow(_MODD - 2); } // _MODD must be a prime inline modint<_MODD> operator / (modint<_MODD> other) const { return *this * other.inv(); } inline modint<_MODD> operator /= (modint<_MODD> other) { return *this *= other.inv(); } inline bool operator == (modint<_MODD> other) const { return value == other.value; } inline bool operator != (modint<_MODD> other) const { return value != other.value; } inline bool operator < (modint<_MODD> other) const { return value < other.value; } inline bool operator > (modint<_MODD> other) const { return value > other.value; } }; template <int32_t _MODD> modint<_MODD> operator * (int64_t value, modint<_MODD> n) { return modint<_MODD>(value) * n; } template <int32_t _MODD> modint<_MODD> operator * (int32_t value, modint<_MODD> n) { return modint<_MODD>(value % _MODD) * n; } template <int32_t _MODD> istream & operator >> (istream & in, modint<_MODD> &n) { return in >> n.value; } template <int32_t _MODD> ostream & operator << (ostream & out, modint<_MODD> n) { return out << n.value; } typedef modint<(int)(1e9+7)> mint; typedef vector<mint> vm; struct DPs { vm up; vm down; vm prevLUp; vm prevPDown; }; DPs dp2(const string& s) { int n = sz(s); vm dpUp(n+1); vm dpDown(n+1); dpUp[0] = 1; vm prevLUp(n+1), prevPDown(n+1); fwd(i,1,n+1) { if(s[i-1] == 'L') { for(int l=sz(dpUp)-1; l>=1; l--) dpUp[l] += dpUp[l-1] + dpDown[l-1] - prevLUp[l]; prevLUp = dpUp; } else { for(int l=0; l<sz(dpUp)-1; l++) dpDown[l] += dpUp[l+1] + dpDown[l+1] - prevPDown[l]; prevPDown = dpDown; } } return {dpUp, dpDown, prevLUp, prevPDown}; } vm unextendible(const DPs& dp, const string& s, char dir) { vm res(sz(dp.up)); rep(i, sz(res)) res[i] = dp.up[i] + dp.down[i]; if(count(all(s), dir) > 0) { if(dir == 'L') rep(i,sz(res)-1) res[i] -= dp.prevLUp[i+1]; else fwd(i,1,sz(res)) res[i] -= dp.prevPDown[i-1]; } return res; } mint solveJoint(const DPs& dpb, const vm& ual, const vm& uap) { mint res=0; rep(i,min(sz(ual), sz(dpb.down))) res += ual[i]*dpb.down[i] + uap[i]*dpb.up[i]; return res; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<string> tab(n); rep(i,n) { cin >> tab[i]; } vector<DPs> dpsInv(n); rep(i,n) { string cp = tab[i]; reverse(all(cp)); for(auto &a:cp) a = (a == 'L' ? 'P' : 'L'); dpsInv[i] = dp2(cp); } rep(i,n) { auto dp = dp2(tab[i]); auto uL = unextendible(dp, tab[i], 'L'); auto uR = unextendible(dp, tab[i], 'P'); rep(j,n) cout<<solveJoint(dpsInv[j], uL, uR)-mint(1)<<" "; cout<<"\n"; } } |