#include <bits/stdc++.h> using namespace std; typedef long long int lli; const int MAXN = 3005; const lli MOD = 1'000'000'007; char in[MAXN]; vector<int> t[MAXN]; // lli unsafe_mod(lli x) { // while (x >= MOD) // x -= MOD; // // return x; // } #define unsafe_mod(x) ((x) % MOD) struct Node { lli no=0, nl=0, np=0, ok=0; // void mod() { // no = unsafe_mod(no); // nl = unsafe_mod(nl); // np = unsafe_mod(np); // ok = unsafe_mod(ok); // } }; Node res[2 * MAXN]; int numl; Node rem_res[2 * MAXN]; int rem_numl; void update(int a) { if (a == 1) { for (int i=numl+1; i>=0; i--) { res[i].ok = unsafe_mod(res[i].ok + res[i].nl); res[i].nl = 0; res[i].np = unsafe_mod(res[i].np + res[i].no); res[i].no = 0; if (i > 0) res[i].no = unsafe_mod(res[i].no + res[i-1].nl + res[i-1].no); } } else { for (int i=0; i<=numl+1; i++) { res[i].ok = unsafe_mod(res[i].ok + res[i].np); res[i].np = 0; res[i].nl = unsafe_mod(res[i].nl + res[i].no); res[i].no = 0; res[i].no = unsafe_mod(res[i].no + res[i+1].np + res[i+1].no); } } } lli compute_score(int x, int y, bool reuse) { if (reuse) { numl = rem_numl; for (int i=0; i<=numl; i++) res[i] = rem_res[i]; } else { numl = 1; res[0].no = 1; for (int a : t[x]) { if (a == 1) numl++; update(a); } for (int i=0; i<=numl; i++) rem_res[i] = res[i]; rem_numl = numl; } for (int a : t[y]) { if (a == 1) numl++; update(a); } lli w = unsafe_mod(res[0].no + res[0].nl + res[0].np + res[0].ok - 1 + MOD); for (int i=0; i<=numl; i++) res[i] = Node(); return w; } int main() { int n; scanf("%d%*c", &n); for (int i=0; i<n; i++) { scanf("%s", in); int l = 0; for (int j=0; in[j]!=0; j++) { l++; if (in[j] == 'L') { t[i].push_back(1); } else { t[i].push_back(-1); } } } for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { printf("%lld ", compute_score(i, j, (j!=0))); } 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 | #include <bits/stdc++.h> using namespace std; typedef long long int lli; const int MAXN = 3005; const lli MOD = 1'000'000'007; char in[MAXN]; vector<int> t[MAXN]; // lli unsafe_mod(lli x) { // while (x >= MOD) // x -= MOD; // // return x; // } #define unsafe_mod(x) ((x) % MOD) struct Node { lli no=0, nl=0, np=0, ok=0; // void mod() { // no = unsafe_mod(no); // nl = unsafe_mod(nl); // np = unsafe_mod(np); // ok = unsafe_mod(ok); // } }; Node res[2 * MAXN]; int numl; Node rem_res[2 * MAXN]; int rem_numl; void update(int a) { if (a == 1) { for (int i=numl+1; i>=0; i--) { res[i].ok = unsafe_mod(res[i].ok + res[i].nl); res[i].nl = 0; res[i].np = unsafe_mod(res[i].np + res[i].no); res[i].no = 0; if (i > 0) res[i].no = unsafe_mod(res[i].no + res[i-1].nl + res[i-1].no); } } else { for (int i=0; i<=numl+1; i++) { res[i].ok = unsafe_mod(res[i].ok + res[i].np); res[i].np = 0; res[i].nl = unsafe_mod(res[i].nl + res[i].no); res[i].no = 0; res[i].no = unsafe_mod(res[i].no + res[i+1].np + res[i+1].no); } } } lli compute_score(int x, int y, bool reuse) { if (reuse) { numl = rem_numl; for (int i=0; i<=numl; i++) res[i] = rem_res[i]; } else { numl = 1; res[0].no = 1; for (int a : t[x]) { if (a == 1) numl++; update(a); } for (int i=0; i<=numl; i++) rem_res[i] = res[i]; rem_numl = numl; } for (int a : t[y]) { if (a == 1) numl++; update(a); } lli w = unsafe_mod(res[0].no + res[0].nl + res[0].np + res[0].ok - 1 + MOD); for (int i=0; i<=numl; i++) res[i] = Node(); return w; } int main() { int n; scanf("%d%*c", &n); for (int i=0; i<n; i++) { scanf("%s", in); int l = 0; for (int j=0; in[j]!=0; j++) { l++; if (in[j] == 'L') { t[i].push_back(1); } else { t[i].push_back(-1); } } } for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { printf("%lld ", compute_score(i, j, (j!=0))); } printf("\n"); } return 0; } |