QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#346353 | #8306. Boring Problem | hos_lyric | WA | 1ms | 3804kb | C++14 | 8.8kb | 2024-03-08 13:26:57 | 2024-03-08 13:26:57 |
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;
}
////////////////////////////////////////////////////////////////////////////////
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];
}
}
vector<vector<vector<char>>> presuf(N, vector<vector<char>>(N, vector<char>(M + 1, 0)));
/*
F[i](x) := \sum[n] Pr[end with T[i] at n] x^n
G(x) := \sum[n] E[yet] x^n
system for F[i](1), G(1)
*/
Mat A = zero(N + 1);
for (int i = 0; i < N; ++i) {
// force terminate by T[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)
presuf[i][j][y] = 1;
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<vector<int>> ma(RLen + 1, vector<int>(N));
// separate probability for match using R[0, q)
for (int j = 0; j < N; ++j) {
vector<Mint> fs(M + 1, 0);
for (int i = 0; i < N; ++i) {
/*
i-th row changes
early match: T[j] = R[q-z, q) [____] T[i][0, y)
R[0, q) [____]: yet
*/
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]) {
fs[M - y] += invA[N][i] * tail[i][0];
}
}
for (int z = M; --z >= 0; ) {
fs[z] += P[T[j][z] - 'a'] * fs[z + 1];
}
// cerr<<"j = "<<j<<": fs = "<<fs<<endl;
{
int y = 0;
for (int q = 1; q <= RLen; ++q) {
for (; ~y && T[j][y] != R[q - 1]; y = fail[j][y]) {}
++y;
ma[q][j] = y;
for (int z = y; z; z = fail[j][z]) {
ans[q] -= fs[z];
}
}
}
}
// Pr[match using R[0, q)]
for (int q = 1; q <= RLen; ++q) {
Mint prob = 0;
for (int i = 0; i < N; ++i) if (ma[q][i]) {
bool ok = true;
for (int j = 0; j < N; ++j) {
if (ma[q][i] < ma[q][j] && presuf[i][j][M - (ma[q][j] - ma[q][i])]) {
ok = false;
}
}
if (ok) {
cerr<<"ok q = "<<q<<", i = "<<i<<", ma[q][i] = "<<ma[q][i]<<endl;
prob += tail[i][ma[q][i]];
}
}
ans[q] -= invA[N][N] * prob;
}
// cerr<<"ans = "<<ans<<endl;
// cerr<<"ma = "<<ma<<endl;
for (int q = 0; q <= RLen; ++q) {
if (*max_element(ma[q].begin(), ma[q].end()) >= M) {
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: 100
Accepted
time: 1ms
memory: 3760kb
input:
2 2 2 50 50 aa bb ababaa
output:
3 4 5 6 7 6
result:
ok 6 numbers
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3804kb
input:
3 3 3 25 25 50 abc bac cab ababbabbcaaa
output:
13 333333343 333333344 333333345 17 333333347 333333348 20 333333358 708333359 23 24
result:
wrong answer 10th numbers differ - expected: '666666692', found: '708333359'