m = (int) strlen(S + 1); for (int i = 0; i < MAXM; ++i) for (int j = 0; j < MAXM; ++j) f.g[i][j] = 1; for (int i = 1; i < m; ++i) { int c1 = S[i] - 'a', c2 = S[i + 1] - 'a'; f.g[c1][c2] = 0; } g = fpow(f, n - 1);
int ans = 0; for (int i = 0; i < MAXM; ++i) for (int j = 0; j < MAXM; ++j) ans = (ans + g.g[i][j]) % P; printf("%d\n", ans); return0; }
inlineintwhich(constint& u){ return pre[u]? ch[1][pre[u]] == u: 0; } inlinevoidconnect(int u, int fa, constint& w){ if (u) pre[u] = fa; if (fa) ch[w][fa] = u; else root = u; } inlinevoidrotate(constint& u){ int fa = pre[u], w = which(u); connect(u, pre[fa], which(fa)); connect(ch[w ^ 1][u], fa, w), connect(fa, u, w ^ 1); maintain(fa); }
voidsplay(constint& u, int v){ v = pre[v]; while (pre[u] != v) { int fa = pre[u]; if (pre[fa] != v) which(u) == which(fa)? rotate(fa): rotate(u); rotate(u); } maintain(u); }
if (a + b + c + d < n) returnputs("0"), 0; PolyPre(2 * n); int N = min(min(a, min(b, min(c, d))), n / 4), ans = 0; for (int i = 0; i <= N; ++i) { int s = g(n - 4 * i, a - i, b - i, c - i, d - i) % P; ans = (ans + 1LL * binom(n - 3 * i, i) * ((i & 1)? P - s: s) % P) % P; }
namespace Graph { structEdge { int nxt, to, w; } edges[MAXM << 1]; int head[MAXN], eidx;
inlinevoidinit(){ memset(head, -1, sizeof head), eidx = 1; } inlinevoidAddEdge(int from, int to, int w){ edges[++eidx] = (Edge){ head[from], to, w }, head[from] = eidx; }
structNode { int u, k, d; Node(int _u, int _k, int _d): u(_u), k(_k), d(_d) { } booloperator < (const Node& rhs) const { return d > rhs.d; } };
intDijkstra(){ priority_queue<Node> PQ; memset(d, 0x3f, sizeof d); PQ.push(Node(s, K + A[s], d[s][K + A[s]] = 0)); while (!PQ.empty()) { int u = PQ.top().u, k = PQ.top().k, w = PQ.top().d; PQ.pop(); if (d[u][k] != w) continue; for (int i = head[u]; ~i; i = edges[i].nxt) { int v = edges[i].to, vk = k + A[v]; if (0 <= vk && vk <= 2 * K && d[v][vk] > d[u][k] + edges[i].w) PQ.push(Node(v, vk, d[v][vk] = d[u][k] + edges[i].w)); } } int ret = INF; for (int k = 0; k <= 2 * K; ++k) ret = min(ret, d[t][k]); return ret < INF? ret: -1; } }
intmain(){ #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif int Ti; read(Ti); while (Ti--) { Graph::init();
read(n), read(m), read(K); for (int i = 1; i <= n; ++i) read(A[i]), A[i] = (A[i] == 1)? 1: -1; for (int u, v, w, i = 1; i <= m; ++i) { read(u), read(v), read(w); Graph::AddEdge(u, v, w), Graph::AddEdge(v, u, w); } read(s), read(t);
for (int i = 1; i <= nidx; ++i) ++cnt[len[i]]; for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1]; for (int i = nidx; i; --i) idx[cnt[len[i]]--] = i; for (int u, i = nidx; i; --i) { u = idx[i], size[lnk[u]] += size[u]; if (size[u] == K) Mdy(len[lnk[u]] + 1, len[u]); }
for (int i = 1; i <= n; ++i) f[i] += f[i - 1]; int ret = -1, Mx = 0; for (int i = n; i; --i) if (f[i] > Mx) Mx = f[i], ret = i; return ret; } }
intmain(){ #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif int Ti; scanf("%d", &Ti); while (Ti--) { SAM::init(), scanf("%s%d", S + 1, &K); n = (int) strlen(S + 1); for (int i = 1; i <= n; ++i) SAM::Ins(S[i] - 'a');
// LOJ #3109 // DeP #include<cstdio> #include<algorithm> usingnamespace std;
typedeflonglong LL; constint MAXD = 5e2 + 5;
int d; LL f[MAXD << 1][MAXD << 1][2];
inline LL Dist(const LL& x){ return2 * x - __builtin_popcountll(x); } inline LL LCA(LL u, LL v){ while (u != v) { if (u < v) swap(u, v); u >>= 1; } return u; }
inlineintlg2(LL s){ int ret = 0; while (s > 0) s >>= 1, ++ret; return ret; }
LL solve(const LL& s, constint& c, constint& a, constint& b){ int limit = max(lg2(s), max(a - 1, b)); for (int i = 0; i <= limit; ++i) for (int j = 0; j <= c; ++j) f[i][j][0] = f[i][j][1] = 0;
for (int p = 0; p <= 1; ++p) { if (a <= 1 && p == 1) continue; for (int q = 0; q <= 1; ++q) if (p + q <= c) { if (((p + q) & 1) != (s & 1)) continue; if ((b < 1 && q == 1) || (b == 1 && q == 0)) continue; ++f[0][p + q][(p + q) / 2]; } } for (int i = 1; i <= limit; ++i) for (int j = 0; j <= c; ++j) for (int k = 0; k <= 1; ++k) if (f[i - 1][j][k] > 0) { for (int p = 0; p <= 1; ++p) { if (i >= a - 1 && p == 1) continue; for (int q = 0; q <= 1; ++q) if (j + p + q <= c) { if (((k + p + q) & 1) != ((s >> i) & 1)) continue; if ((i > b - 1 && q == 1) || (i == b - 1 && q == 0)) continue; f[i][j + p + q][(k + p + q) / 2] += f[i - 1][j][k]; } } }
return f[limit][c][0]; }
intmain(){ #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif int Ti, c; scanf("%d", &Ti); while (Ti--) { LL x, y; scanf("%d%lld%lld%d", &d, &x, &y, &c); LL z = LCA(x, y), s = Dist(x) + Dist(y) - Dist(z) - Dist(z >> 1); if (c == 1) printf("%lld\n", s); else { LL ans = 0; for (int a = 0; a < d; ++a) for (int b = 0; b < d; ++b) { LL v = (1LL << (a + 1)) + (1LL << (b + 1)) - 3; if (v == 0) continue; z = s / v; if (z <= 0 || lg2(z) + max(a, b) > d) continue; LL k = s - v * z; for (int i = 0; i <= a + b; ++i) if ((k + i) % 2 == 0) ans += solve((k + i) / 2, i, a, b); } printf("%lld\n", ans - 1); } } return0; }