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
| #include <iostream> #include <vector> #include <stack>
typedef long long LL;
using namespace std;
const int maxN = 5e3 + 2; const int mod = 1e9 + 7;
int n, a, b, dfst, colcnt, ans; int dfn[maxN], low[maxN], col[maxN], minn[maxN], maxx[maxN]; int gcd[maxN], cnt[maxN]; int fac[maxN]; stack<int> stk; vector<int> G[maxN]; vector<bool> tmp, F[maxN]; string S[maxN];
inline bool Chkmin(int& x, int y) { return x > y ? x = y, true : false; }
inline void Mod(int& x) { x >= mod ? x -= mod : 0; }
inline int GCD(int x, int y) { while (y) std::swap(x, y), y %= x; return x; }
inline int Qpow(int a, int n) { if (a == 1) return 1; if (n == -1) return Qpow(a, mod - 2); int res = 1; while (n) { if (n & 1) res = (LL)res * a % mod; a = (LL)a * a % mod; n >>= 1; } return res; }
void Tarjan(int u) { dfn[u] = low[u] = ++dfst; stk.push(u); for (int v : G[u]) { if (!dfn[v]) Tarjan(v), Chkmin(low[u], low[v]); else if (!col[v]) Chkmin(low[u], dfn[v]); } if (low[u] == dfn[u]) { ++colcnt; col[u] = colcnt, gcd[colcnt] = S[u].size(); while (stk.top() != u) { int v = stk.top(); stk.pop(); col[v] = colcnt, gcd[colcnt] = GCD(gcd[colcnt], S[v].size()); } stk.pop(); } }
inline int C(int n, int m) { return (LL)fac[n] * Qpow(fac[m], -1) % mod * Qpow(fac[n - m], -1) % mod; }
int main() { #ifndef ONLINE_JUDGE freopen("wib.in", "r", stdin); freopen("wib.out", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> a >> b; for (int i = 1; i <= n; ++i) { cin >> S[1]; for (int j = 1; j <= n; ++j) if (S[1][j - 1] == '1') G[i].push_back(j); } for (int i = 1; i <= n; ++i) { int sth; cin >> sth >> S[i]; } for (int i = 1; i <= n; ++i) if (!dfn[i]) Tarjan(i); for (int i = 1; i <= colcnt; ++i) F[i].resize(gcd[i]); for (int i = 1; i <= n; ++i) for (int j = 0; j < S[i].size(); ++j) if (S[i][j] == '1') ++minn[i], F[col[i]][j % gcd[col[i]]] = true; for (int i = colcnt; i > 1; --i) { int curr = GCD(gcd[i], gcd[i - 1]); tmp.clear(), tmp.resize(curr); for (int j = 0; j < gcd[i]; ++j) if (F[i][j]) tmp[j % curr] = true; for (int j = 0; j < gcd[i - 1]; ++j) if (tmp[j % curr]) F[i - 1][j] = true; } for (int i = 1; i <= colcnt; ++i) for (int j = 0; j < gcd[i]; ++j) cnt[i] += F[i][j]; for (int i = 1; i <= n; ++i) maxx[i] = S[i].size() / gcd[col[i]] * cnt[col[i]]; fac[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (LL)fac[i - 1] * i % mod; for (int i = 1; i <= n; ++i) { int cnt1 = 0, cnt2 = 0; for (int j = 1; j <= n; ++j) { if (i == j) continue; if (minn[j] > maxx[i]) ++cnt1; else if (maxx[j] > maxx[i] or (maxx[j] == maxx[i] and j < i)) ++cnt2; } if (cnt1 >= a) continue; for (int x = min(b - 1, min(cnt2, a - 1 - cnt1)); x + cnt1 + 1 >= b and x >= 0; --x) Mod(ans += (LL)C(cnt1, b - x - 1) * C(cnt2, x) % mod); } cout << ans << endl; return 0; }
|