QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#346507 | #8306. Boring Problem | hos_lyric | WA | 0ms | 3856kb | C++14 | 7.8kb | 2024-03-08 16:57:29 | 2024-03-08 16:57:29 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;
// square matrix
using Mat = vector<vector<Mint>>;
Mat zero(int n) {
return Mat(n, vector<Mint>(n, 0));
}
Mat identity(int n) {
Mat a(n, vector<Mint>(n, 0));
for (int i = 0; i < n; ++i) a[i][i] = 1;
return a;
}
Mat inverse(Mat a) {
const int n = a.size();
Mat b(n, vector<Mint>(n, 0));
for (int i = 0; i < n; ++i) b[i][i] = 1;
for (int h = 0; h < n; ++h) {
for (int i = h; i < n; ++i) if (a[i][h]) {
swap(a[h], a[i]);
swap(b[h], b[i]);
break;
}
assert(a[h][h]);
const Mint s = a[h][h].inv();
for (int j = h + 1; j < n; ++j) a[h][j] *= s;
for (int j = 0; j < n; ++j) b[h][j] *= s;
for (int i = h + 1; i < n; ++i) {
const Mint t = a[i][h];
if (t) {
for (int j = h + 1; j < n; ++j) a[i][j] -= t * a[h][j];
for (int j = 0; j < n; ++j) b[i][j] -= t * b[h][j];
}
}
}
for (int h = n; --h >= 0; ) for (int i = 0; i < h; ++i) {
const Mint t = a[i][h];
if (t) for (int j = 0; j < n; ++j) b[i][j] -= t * b[h][j];
}
return b;
}
////////////////////////////////////////////////////////////////////////////////
/*
F[i](x) := \sum[n] Pr[end with T[i] exactly in n steps] x^n
G(x) := \sum[n] Pr[yet in n steps] x^n
force terminate by adding T[i]
G(x) Pr[T[i]] x^M + \sum[m] [S[$-m, $) = T[i][0, m)] Pr[T[i][m, M)] x^M
= \sum[j] \sum[m] [T[j][M-m, M) = T[i][0, m)] F[j](x) Pr[T[i][m, M)] x^(M-m)
system for F[j](1), G(1)
want (r.h.s) mod x^M |[x=1]
*/
char buf[10010];
int N, M, K;
vector<Mint> P;
vector<string> T;
string R;
int main() {
for (; ~scanf("%d%d%d", &N, &M, &K); ) {
P.resize(K);
for (int k = 0; k < K; ++k) {
int p;
scanf("%d", &p);
P[k] = p / Mint(100);
}
T.resize(N);
for (int i = 0; i < N; ++i) {
scanf("%s", buf);
T[i] = buf;
}
scanf("%s", buf);
R = buf;
const int RLen = R.size();
sort(T.begin(), T.end());
T.erase(unique(T.begin(), T.end()), T.end());
N = T.size();
vector<vector<int>> fail(N, vector<int>(M + 1));
for (int i = 0; i < N; ++i) {
int y = fail[i][0] = -1;
for (int x = 0; x < M; ++x) {
for (; ~y && T[i][y] != T[i][x]; y = fail[i][y]) {}
fail[i][x + 1] = ++y;
}
}
vector<vector<Mint>> tail(N, vector<Mint>(M + 1));
for (int i = 0; i < N; ++i) {
tail[i][M] = 1;
for (int x = M; --x >= 0; ) {
tail[i][x] = P[T[i][x] - 'a'] * tail[i][x + 1];
}
}
// sum in fail tree
auto tailSum = tail;
for (int i = 0; i < N; ++i) {
for (int x = 1; x <= M; ++x) {
tailSum[i][x] += tailSum[i][fail[i][x]];
}
}
Mat A = zero(N + 1);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
int y = 0;
for (int x = 0; x < M; ++x) {
for (; ~y && T[i][y] != T[j][x]; y = fail[i][y]) {}
++y;
}
for (; y; y = fail[i][y]) {
// T[j][M - y, M) = T[i][0, y)
A[i][j] += tail[i][y];
}
}
A[i][N] -= tail[i][0];
}
// \sum[j] F[j](1) = 1
for (int j = 0; j < N; ++j) {
A[N][j] += 1;
}
const auto invA = inverse(A);
// cerr<<"A = "<<A<<endl;
// cerr<<"A^-1 = "<<invA<<endl;
vector<Mint> ans(RLen + 1, invA[N][N]);
vector<int> ma(RLen + 1, 0);
for (int i = 0; i < N; ++i) {
// change in i-th row
int y = 0;
for (int q = 1; q <= RLen; ++q) {
for (; ~y && T[i][y] != R[q - 1]; y = fail[i][y]) {}
++y;
if (y == M) {
ma[q] = 1;
}
ans[q] += invA[N][i] * tailSum[i][y];
}
}
// cerr<<"ans = "<<ans<<endl;
// cerr<<"ma = "<<ma<<endl;
for (int q = 0; q <= RLen; ++q) if (ma[q]) {
fill(ans.begin() + q, ans.end(), 0);
break;
}
for (int q = 0; q <= RLen; ++q) {
ans[q] += q;
}
for (int q = 1; q <= RLen; ++q) {
printf("%u\n", ans[q].x);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3856kb
input:
2 2 2 50 50 aa bb ababaa
output:
2 3 4 5 6 6
result:
wrong answer 1st numbers differ - expected: '3', found: '2'