//Konrad Paluszek,University of Warsaw(former XIV LO Staszic) //#STAY AT HOME #ifndef LOCAL #pragma GCC optimize("O3") #endif #define TIME (chrono::steady_clock::now().time_since_epoch().count()) #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define xfm(a,b)a##b #define xwb(a,b)xfm(a,b) #define _ xwb(nvj,__LINE__) #define __ xwb(kjy,__LINE__) #define ___ xwb(cjm,__LINE__) #define REP(i,n)for(urs(n)i=0;i<(n);++i) #define PER(r...)for(bool _=1;_||next_permutation(r);_=false) #define FS(r)r.first,r.second #define ALL(r)(r).begin(),(r).end() #define M0(r) memset(r,0,sizeof(r)) #define sim template<class c #define forbits(i,m)if(m)for(urs(m)i=ctz(m),i##nvj=m;i##nvj;i##nvj^=((urs(m))1<<i),i=i##nvj?ctz(i##nvj):0) #define fordbits(i,m)if(m)for(urs(m)i=8*sizeof(m)-clz(m)-1,i##nxd=m;i##nxd;i##nxd^=((urs(m))1<<i),i=i##nxd?8*sizeof(m)-clz(i##nxd)-1:0) #define ksets(t, m, k, n)for(t m=(((t)1<<(k))-1);m<((t)1<<(n));m=nux(m)) #define urs(r...)typename decay<decltype(r)>::type #define hur(f,g)sim>int f(c a){if(sizeof(c)==8)return g##ll(a);return g(a);} using namespace __gnu_pbds;using namespace std;using ll=long long;using ld=long double;using ull=unsigned long long;using vi=vector<int>;using vll=vector<ll>;using pii=pair<int,int>;using pll=pair<ll,ll>;using vpii=vector<pii>;using unt=unsigned int;sim>using min_queue=priority_queue<c,vector<c>,greater<>>;sim,class b,class cmp=less<c>>using ordered_map=tree<c,b,cmp,rb_tree_tag,tree_order_statistics_node_update>;sim,class cmp=less<c>>using ordered_set=ordered_map<c,null_type,cmp>;hur(popc,__builtin_popcount)hur(ctz,__builtin_ctz)hur(clz,__builtin_clz)sim,class N>bool mini(c&o,const N&h){if(o>h)return o=h,1;return 0;}sim,class N>bool maxi(c&o,const N&h){if(o<h)return o=h,1;return 0;} #ifdef LOCAL #include </home/konrad/cp/headers/debuglib.hpp> #else #define loc(...) #define onl(r...)r #define debug(...) #define print_stack(...) #define mark_stack(...) #define set_pre(...) #define reg_it(...) #define def_op(...) struct _{}; #define mask_set(...) #define exit my_exit void __resources(); void my_exit(int x) {cout.flush(); #ifdef LOCAL2 __resources(); #endif _Exit(x);} #endif #define next nexT #define prev preV #define tree trEE #define left lefT #define right righT #define div diV #define y1 y_1 #define pow don't' ull mix(ull o){o+=0x9e3779b97f4a7c15;o=(o^(o>>30))*0xbf58476d1ce4e5b9;o=(o^(o>>27))*0x94d049bb133111eb;return o^(o>>31);}ull SALT=0x7a14a4b0881ebf9,tqu=0x7a14a4b0881ebf9;ull my_rand(){return tqu=mix(tqu);}void my_srand(ull x){SALT=tqu=x;}const int inf=1023400000;const ll llinf=1234567890000000000ll;ll fix(ll o, ll m){o%=m;if(o<0)o+=m;return o;} #define rand my_rand #define srand my_srand #define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;}) sim>inline c nux(c m){if(!m)return numeric_limits<c>::max();c A=m&-m;c B=~((A-1)^m);c C=B&-B;c D=(C>>(1+ctz(A)))-1;return C|(m&~(C-1))|D;}sim>void unq(c&x){x.resize(unique(ALL(x))-x.begin());} //#STAY AT HOME const int mod = 1e9 + 7; struct pref_res { ll full; vector <ll> aft_plus, aft_minus; pref_res() : full(), aft_plus(), aft_minus() {} }; struct suf_res { vector <ll> with_plus, with_minus; }; #define show(f) #f " = " << x.f << "; " loc ( ostream &operator<<(ostream &o, const pref_res &x) { return o << "[" show(full) show(aft_plus) show(aft_minus) << "]"; } ostream &operator<<(ostream &o, const suf_res &x) { return o; } ) void add(ll &a, ll b) { a += b; if (a >= mod) a -= mod; } void add_pro(ll &a, ll b, ll c) { (a += b * c) %= mod; } const char PLUS = 'L', MINUS = 'P'; vector <vll> calc_dp(const string &s) { int n = s.size(); vector <vll> dp(n + 1, vll(n + 1)); dp[0][0] = 1; vi next_plus(n + 1, -1), next_minus(n + 1, -1); for (int i = n - 1; i >= 0; --i) { next_plus[i] = next_plus[i + 1]; next_minus[i] = next_minus[i + 1]; if (s[i] == PLUS) next_plus[i] = i + 1; else next_minus[i] = i + 1; } REP(i, n) { debug(arr(dp, i)); auto serve = [&](int to, int delta) { if (to == -1) return; for (int curr_bal = 0; curr_bal <= i; ++curr_bal) { int would = curr_bal + delta; if (would >= 0) { add(dp[to][would], dp[i][curr_bal]); } } }; serve(next_plus[i], +1); serve(next_minus[i], -1); } return dp; } pref_res solve_pref(const string &s) { debug(imie(s)); pref_res r; int n = s.size(); int last_plus = -1, last_minus = -1; auto dp = calc_dp(s); REP(i, n) { if (s[i] == PLUS) last_plus = i; else last_minus = i; } REP(i, n + 1) add(r.full, dp[i][0]); r.aft_plus.resize(n + 1); r.aft_minus.resize(n + 1); REP(b, n + 1) { REP(i, n + 1) { if (i > last_plus) add(r.aft_plus[b], dp[i][b]); if (i > last_minus) add(r.aft_minus[b], dp[i][b]); } } debug(arr(dp, n)); return r; } suf_res solve_suf(string s) { suf_res r; int n = s.size(); reverse(ALL(s)); for (char &x : s) x ^= PLUS ^ MINUS; auto dp = calc_dp(s); r.with_plus.resize(n + 1); r.with_minus.resize(n + 1); for (int bal = 0; bal <= n; ++bal) { REP(i, n) { if (s[i] == PLUS) add(r.with_minus[bal], dp[i + 1][bal]); else add(r.with_plus[bal], dp[i + 1][bal]); } } return r; } ll extract(const pref_res &pref, const suf_res &suf) { ll ans = pref.full; REP(i, min(pref.aft_plus.size(), suf.with_plus.size())) { add_pro(ans, pref.aft_plus[i], suf.with_plus[i]); } REP(i, min(pref.aft_minus.size(), suf.with_minus.size())) { add_pro(ans, pref.aft_minus[i], suf.with_minus[i]); } return fix(ans - 1, mod); } void solve() { int n; cin >> n; vector <string> a(n); for (auto &x : a) cin >> x; vector <pref_res> prefs; vector <suf_res> sufs; for (auto &s : a) { prefs.push_back(solve_pref(s)); debug(imie(s), prefs.back()); } for (auto &s : a) { sufs.push_back(solve_suf(s)); debug(imie(s), sufs.back()); } REP(i, n) { REP(j, n) cout << extract(prefs[i], sufs[j]) << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // unt seed = TIME; debug(imie(seed));srand(seed); int t = 1; // scanf("%d", &t); REP(_, t) solve(); exit(0); } //#STAY AT HOME
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | //Konrad Paluszek,University of Warsaw(former XIV LO Staszic) //#STAY AT HOME #ifndef LOCAL #pragma GCC optimize("O3") #endif #define TIME (chrono::steady_clock::now().time_since_epoch().count()) #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define xfm(a,b)a##b #define xwb(a,b)xfm(a,b) #define _ xwb(nvj,__LINE__) #define __ xwb(kjy,__LINE__) #define ___ xwb(cjm,__LINE__) #define REP(i,n)for(urs(n)i=0;i<(n);++i) #define PER(r...)for(bool _=1;_||next_permutation(r);_=false) #define FS(r)r.first,r.second #define ALL(r)(r).begin(),(r).end() #define M0(r) memset(r,0,sizeof(r)) #define sim template<class c #define forbits(i,m)if(m)for(urs(m)i=ctz(m),i##nvj=m;i##nvj;i##nvj^=((urs(m))1<<i),i=i##nvj?ctz(i##nvj):0) #define fordbits(i,m)if(m)for(urs(m)i=8*sizeof(m)-clz(m)-1,i##nxd=m;i##nxd;i##nxd^=((urs(m))1<<i),i=i##nxd?8*sizeof(m)-clz(i##nxd)-1:0) #define ksets(t, m, k, n)for(t m=(((t)1<<(k))-1);m<((t)1<<(n));m=nux(m)) #define urs(r...)typename decay<decltype(r)>::type #define hur(f,g)sim>int f(c a){if(sizeof(c)==8)return g##ll(a);return g(a);} using namespace __gnu_pbds;using namespace std;using ll=long long;using ld=long double;using ull=unsigned long long;using vi=vector<int>;using vll=vector<ll>;using pii=pair<int,int>;using pll=pair<ll,ll>;using vpii=vector<pii>;using unt=unsigned int;sim>using min_queue=priority_queue<c,vector<c>,greater<>>;sim,class b,class cmp=less<c>>using ordered_map=tree<c,b,cmp,rb_tree_tag,tree_order_statistics_node_update>;sim,class cmp=less<c>>using ordered_set=ordered_map<c,null_type,cmp>;hur(popc,__builtin_popcount)hur(ctz,__builtin_ctz)hur(clz,__builtin_clz)sim,class N>bool mini(c&o,const N&h){if(o>h)return o=h,1;return 0;}sim,class N>bool maxi(c&o,const N&h){if(o<h)return o=h,1;return 0;} #ifdef LOCAL #include </home/konrad/cp/headers/debuglib.hpp> #else #define loc(...) #define onl(r...)r #define debug(...) #define print_stack(...) #define mark_stack(...) #define set_pre(...) #define reg_it(...) #define def_op(...) struct _{}; #define mask_set(...) #define exit my_exit void __resources(); void my_exit(int x) {cout.flush(); #ifdef LOCAL2 __resources(); #endif _Exit(x);} #endif #define next nexT #define prev preV #define tree trEE #define left lefT #define right righT #define div diV #define y1 y_1 #define pow don't' ull mix(ull o){o+=0x9e3779b97f4a7c15;o=(o^(o>>30))*0xbf58476d1ce4e5b9;o=(o^(o>>27))*0x94d049bb133111eb;return o^(o>>31);}ull SALT=0x7a14a4b0881ebf9,tqu=0x7a14a4b0881ebf9;ull my_rand(){return tqu=mix(tqu);}void my_srand(ull x){SALT=tqu=x;}const int inf=1023400000;const ll llinf=1234567890000000000ll;ll fix(ll o, ll m){o%=m;if(o<0)o+=m;return o;} #define rand my_rand #define srand my_srand #define random_shuffle(r...)random_shuffle(r,[](int _){return my_rand()%_;}) sim>inline c nux(c m){if(!m)return numeric_limits<c>::max();c A=m&-m;c B=~((A-1)^m);c C=B&-B;c D=(C>>(1+ctz(A)))-1;return C|(m&~(C-1))|D;}sim>void unq(c&x){x.resize(unique(ALL(x))-x.begin());} //#STAY AT HOME const int mod = 1e9 + 7; struct pref_res { ll full; vector <ll> aft_plus, aft_minus; pref_res() : full(), aft_plus(), aft_minus() {} }; struct suf_res { vector <ll> with_plus, with_minus; }; #define show(f) #f " = " << x.f << "; " loc ( ostream &operator<<(ostream &o, const pref_res &x) { return o << "[" show(full) show(aft_plus) show(aft_minus) << "]"; } ostream &operator<<(ostream &o, const suf_res &x) { return o; } ) void add(ll &a, ll b) { a += b; if (a >= mod) a -= mod; } void add_pro(ll &a, ll b, ll c) { (a += b * c) %= mod; } const char PLUS = 'L', MINUS = 'P'; vector <vll> calc_dp(const string &s) { int n = s.size(); vector <vll> dp(n + 1, vll(n + 1)); dp[0][0] = 1; vi next_plus(n + 1, -1), next_minus(n + 1, -1); for (int i = n - 1; i >= 0; --i) { next_plus[i] = next_plus[i + 1]; next_minus[i] = next_minus[i + 1]; if (s[i] == PLUS) next_plus[i] = i + 1; else next_minus[i] = i + 1; } REP(i, n) { debug(arr(dp, i)); auto serve = [&](int to, int delta) { if (to == -1) return; for (int curr_bal = 0; curr_bal <= i; ++curr_bal) { int would = curr_bal + delta; if (would >= 0) { add(dp[to][would], dp[i][curr_bal]); } } }; serve(next_plus[i], +1); serve(next_minus[i], -1); } return dp; } pref_res solve_pref(const string &s) { debug(imie(s)); pref_res r; int n = s.size(); int last_plus = -1, last_minus = -1; auto dp = calc_dp(s); REP(i, n) { if (s[i] == PLUS) last_plus = i; else last_minus = i; } REP(i, n + 1) add(r.full, dp[i][0]); r.aft_plus.resize(n + 1); r.aft_minus.resize(n + 1); REP(b, n + 1) { REP(i, n + 1) { if (i > last_plus) add(r.aft_plus[b], dp[i][b]); if (i > last_minus) add(r.aft_minus[b], dp[i][b]); } } debug(arr(dp, n)); return r; } suf_res solve_suf(string s) { suf_res r; int n = s.size(); reverse(ALL(s)); for (char &x : s) x ^= PLUS ^ MINUS; auto dp = calc_dp(s); r.with_plus.resize(n + 1); r.with_minus.resize(n + 1); for (int bal = 0; bal <= n; ++bal) { REP(i, n) { if (s[i] == PLUS) add(r.with_minus[bal], dp[i + 1][bal]); else add(r.with_plus[bal], dp[i + 1][bal]); } } return r; } ll extract(const pref_res &pref, const suf_res &suf) { ll ans = pref.full; REP(i, min(pref.aft_plus.size(), suf.with_plus.size())) { add_pro(ans, pref.aft_plus[i], suf.with_plus[i]); } REP(i, min(pref.aft_minus.size(), suf.with_minus.size())) { add_pro(ans, pref.aft_minus[i], suf.with_minus[i]); } return fix(ans - 1, mod); } void solve() { int n; cin >> n; vector <string> a(n); for (auto &x : a) cin >> x; vector <pref_res> prefs; vector <suf_res> sufs; for (auto &s : a) { prefs.push_back(solve_pref(s)); debug(imie(s), prefs.back()); } for (auto &s : a) { sufs.push_back(solve_suf(s)); debug(imie(s), sufs.back()); } REP(i, n) { REP(j, n) cout << extract(prefs[i], sufs[j]) << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // unt seed = TIME; debug(imie(seed));srand(seed); int t = 1; // scanf("%d", &t); REP(_, t) solve(); exit(0); } //#STAY AT HOME |