QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#710894 | #9536. Athlete Welcome Ceremony | WA_automaton# | TL | 848ms | 62068kb | C++23 | 5.2kb | 2024-11-04 22:54:01 | 2024-11-04 22:54:01 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back
void debug() {std::cerr << "\n";}
template<class T, class... OtherArgs>
void debug(T &&var, OtherArgs &&... args) {
std::cerr << std::forward<T>(var) << " ";
debug(std::forward<OtherArgs>(args)...);
}
#define SZ(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
using i64 = long long;
using u64 = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
constexpr int N = 200010;
constexpr int P = 1e9 + 7;
template<class T>
constexpr T power(T a, i64 b) { T res {1}; for (; b; b /= 2, a *= a) if (b % 2) res *= a; return res; }
constexpr i64 mul(i64 a, i64 b, i64 p) { i64 res = (LD)a * b - i64((LD)a * b / p) * p; res %= p; if (res < 0) res += p; return res; }
template<i64 P>
struct MInt {
i64 x;
constexpr MInt() : x {0} {}
constexpr MInt(i64 x) : x {norm(x % getMod())} {}
static i64 Mod;
constexpr static i64 getMod() { return P ? P : Mod; }
constexpr static void setMod(i64 Mod_) { Mod = Mod_; }
constexpr i64 norm(i64 x) const { if (x < 0) x += getMod(); if (x >= getMod()) x -= getMod(); return x; }
constexpr i64 val() const { return x; }
explicit constexpr operator i64() const { return x; }
constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; }
constexpr MInt inv() const { return power(*this, getMod() - 2); }
constexpr MInt &operator*=(MInt rhs) & { if (getMod() < (1ULL << 31)) { x = x * rhs.x % i64(getMod()); } else { x = mul(x, rhs.x, getMod()); } return *this; }
constexpr MInt &operator+=(MInt rhs) & { x = norm(x + rhs.x); return *this; }
constexpr MInt &operator-=(MInt rhs) & { x = norm(x - rhs.x); return *this; }
constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); }
friend constexpr MInt operator*(MInt lhs, MInt rhs) { MInt res = lhs; res *= rhs; return res; }
friend constexpr MInt operator+(MInt lhs, MInt rhs) { MInt res = lhs; res += rhs; return res; }
friend constexpr MInt operator-(MInt lhs, MInt rhs) { MInt res = lhs; res -= rhs; return res; }
friend constexpr MInt operator/(MInt lhs, MInt rhs) { MInt res = lhs; res /= rhs; return res; }
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) { i64 v; is >> v; a = MInt(v); return is; }
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); }
friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
};
// using Z = MInt<0>;
using Z = MInt<P>;
void solve() {
int n, q;
cin >> n >> q;
string s;
cin >> s;
int m = 0;
vector f(3, vector(n + 1, vector(n + 1, vector<Z>(n + 1))));
f[0][0][0][0] = f[1][0][0][0] = f[2][0][0][0] = 1;
for (int i = 0; i < n; i++) {
vector g(3, vector(n + 1, vector(n + 1, vector<Z>(n + 1))));
if (s[i] == '?') {
m++;
for (int x = 0; x < m; x++) {
for (int y = 0; x + y < m; y++) {
int z = m - 1 - x - y;
g[0][x + 1][y][z] += f[1][x][y][z];
g[0][x + 1][y][z] += f[2][x][y][z];
g[1][x][y + 1][z] += f[0][x][y][z];
g[1][x][y + 1][z] += f[2][x][y][z];
g[2][x][y][z + 1] += f[0][x][y][z];
g[2][x][y][z + 1] += f[1][x][y][z];
}
}
} else {
int t = s[i] - 'a';
for (int x = 0; x <= m; x++) {
for (int y = 0; x + y <= m; y++) {
int z = m - x - y;
g[t][x][y][z] += f[(t + 1) % 3][x][y][z];
g[t][x][y][z] += f[(t + 2) % 3][x][y][z];
}
}
}
f = move(g);
}
for (int t = 0; t < 3; t++) {
for (int x = 1; x <= n; x++) {
for (int y = 0; y <= n; y++) {
for (int z = 0; z <= n; z++) {
f[t][x][y][z] += f[t][x - 1][y][z];
}
}
}
for (int x = 0; x <= n; x++) {
for (int y = 1; y <= n; y++) {
for (int z = 0; z <= n; z++) {
f[t][x][y][z] += f[t][x][y - 1][z];
}
}
}
for (int x = 0; x <= n; x++) {
for (int y = 0; y <= n; y++) {
for (int z = 1; z <= n; z++) {
f[t][x][y][z] += f[t][x][y][z - 1];
}
}
}
}
while (q--) {
int x, y, z;
cin >> x >> y >> z;
Z ans = 0;
if (s.back() == '?') {
ans += f[0][x][y][z] + f[1][x][y][z] + f[2][x][y][z];
} else {
ans = f[s.back() - 'a'][x][y][z];
}
cout << ans / 2 << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(20);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3652kb
input:
6 3 a?b??c 2 2 2 1 1 1 1 0 2
output:
3 1 1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3664kb
input:
6 3 ?????? 2 2 2 2 3 3 3 3 3
output:
30 72 96
result:
ok 3 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
1 1 ? 0 1 1
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3944kb
input:
10 10 acab?cbaca 0 2 0 1 1 2 4 2 3 1 1 1 3 5 1 0 5 2 2 2 0 1 2 5 4 3 0 1 1 3
output:
0 1 1 1 1 0 1 1 1 1
result:
ok 10 lines
Test #5:
score: 0
Accepted
time: 0ms
memory: 3732kb
input:
10 10 ?c?c?cbac? 10 5 1 5 8 7 9 2 6 5 7 1 5 2 6 5 6 5 5 10 3 9 1 10 2 5 9 1 2 9
output:
16 16 11 16 11 16 16 5 11 0
result:
ok 10 lines
Test #6:
score: 0
Accepted
time: 20ms
memory: 11128kb
input:
50 100 ?abacbacab?cbcbcb?acabcbabcbcacbababc?caba?acacbca 8 3 8 2 4 8 8 7 3 0 9 2 10 8 7 7 6 5 4 10 2 6 9 3 3 6 6 9 10 8 2 5 8 8 1 0 3 5 0 1 0 6 5 0 8 6 5 5 1 7 9 7 7 10 4 7 5 6 6 4 10 1 2 4 1 7 10 0 8 7 6 3 1 9 1 4 7 2 8 4 0 8 6 1 5 10 4 5 8 2 5 8 4 4 5 9 5 2 1 1 10 9 4 10 1 8 4 3 8 9 9 8 0 1 0 8 0...
output:
8 8 8 0 8 8 6 8 8 8 8 0 0 0 1 8 4 8 8 8 2 4 1 8 1 6 0 2 8 6 8 8 1 4 2 8 8 0 0 8 2 0 8 8 8 4 8 8 8 8 2 0 0 4 8 8 1 8 7 6 7 0 8 8 8 0 4 7 8 4 0 8 0 4 8 8 8 7 8 4 7 2 8 8 8 0 2 2 8 8 8 4 4 0 8 0 8 8 1 1
result:
ok 100 lines
Test #7:
score: 0
Accepted
time: 28ms
memory: 11092kb
input:
50 100 b????????bca?????c?b??ca?acac?b?b???ca?ab???a?a??? 35 43 36 12 49 47 7 11 34 38 44 22 42 17 10 49 8 38 18 26 44 6 18 14 28 29 6 48 32 47 29 15 48 1 5 33 24 17 18 10 27 32 19 10 34 2 23 9 14 24 39 46 12 34 9 49 26 21 8 46 43 43 3 31 16 2 8 27 7 24 41 35 17 25 31 0 13 47 24 31 23 33 40 30 36 39...
output:
34272000 31599360 497244 34272000 17637520 12290752 34272000 93044 415832 34272000 34272000 0 34272000 16360704 27933952 0 34272000 33886976 7896832 12290752 718 24 0 34272000 34272000 0 34272000 34272000 34272000 32254720 0 5666944 34256640 34272000 34272000 12290752 30493248 34256640 20630016 0 10...
result:
ok 100 lines
Test #8:
score: 0
Accepted
time: 787ms
memory: 61972kb
input:
100 1000 c?cbababcabacbacbacacbacabcbabababacababcbcab?cbabacbacbcbcacbab?bcabcbcababcacbabacbcb?babcbab?baca 13 11 4 4 17 20 14 5 2 16 14 15 8 12 17 19 5 11 5 17 12 20 7 6 19 10 1 6 5 0 13 1 9 7 17 1 20 4 16 11 12 18 19 2 16 18 1 11 19 16 3 7 1 0 6 9 16 6 9 16 6 20 7 0 16 20 1 2 8 16 5 20 18 14 18 ...
output:
16 15 14 16 16 16 16 16 8 2 16 8 16 16 16 16 16 2 16 16 16 0 1 16 16 5 1 5 16 16 16 16 16 15 16 13 16 15 2 16 16 1 8 16 16 16 15 0 16 15 16 16 16 16 8 8 16 16 16 16 16 16 8 16 16 1 8 8 16 16 1 16 1 0 16 2 2 16 7 16 16 8 16 16 16 16 1 16 14 16 16 16 16 5 16 16 14 16 11 16 15 11 2 1 8 16 16 7 16 5 16 ...
result:
ok 1000 lines
Test #9:
score: 0
Accepted
time: 848ms
memory: 62068kb
input:
100 1000 ?????c??????????????????????????b???a????a?????????????????????????c???????????????????????????????? 43 38 20 27 40 32 39 27 33 28 50 43 50 3 46 38 46 14 42 48 10 45 25 28 49 10 49 38 17 42 41 49 22 41 18 44 46 47 25 17 44 35 34 43 22 47 42 32 32 44 40 36 41 24 45 38 45 49 44 18 42 34 32 43...
output:
490475656 143989836 119661929 707864467 10104 219100551 479284703 764218529 903846231 659694548 204287063 105920502 191779504 182802705 215438611 938692318 797581204 903917420 893995828 287222624 578695829 95654849 457810426 709349795 85961844 923330494 783007506 111119718 295402274 241594071 551680...
result:
ok 1000 lines
Test #10:
score: 0
Accepted
time: 764ms
memory: 62040kb
input:
100 1000 c???cacacbcab?cb?acb???ac?bab?bcbcbc?c?bcbcaba??b?ba?c?aca?a?bac?cbcbcba??ca?b????ac?baba?ab?cba?c?c 99 70 32 52 98 84 12 78 77 84 8 87 16 36 0 48 70 100 25 4 15 95 54 35 33 35 90 20 4 69 6 11 76 27 96 48 16 24 18 99 48 1 43 54 35 9 81 75 27 58 52 50 94 14 29 67 27 59 68 53 42 31 46 12 90 2...
output:
380160 380160 226896 64156 0 380160 92 380160 380160 92 0 380160 379648 0 380160 22500 380160 380160 380160 380160 380160 226896 380160 380160 226896 0 380160 380160 380160 380160 152672 380160 5624 226896 380160 380160 379648 0 380160 380160 366848 380160 226896 380160 92 374912 5624 380160 380160 ...
result:
ok 1000 lines
Test #11:
score: -100
Time Limit Exceeded
input:
300 100000 abcacacbabacbcababcacacb?babacbcbacbcababcbcbabcacbcbacacabacacacbacbcbcacbabacbcbcbabcacbababcabcabcabababacbcacbacabcbacbacacacbababababacbcababcbcacacbcbabacabcabababcabacbcbcbabcabacbabacacbcbcacacacbcabcbabcbcabababababcacabcabababcbcbcbcbcabacbabacbabcacbcababacacbcbababababcacacaba...