QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#812545 | #884. Joining Treasure Hunt | tarjen | AC ✓ | 233ms | 33576kb | C++20 | 4.4kb | 2024-12-13 16:33:41 | 2024-12-13 16:33:41 |
Judging History
answer
#include <bits/stdc++.h>
#define fp(i, a, b) for (int i = (a), i##_ = (b) + 1; i < i##_; ++i)
#define fd(i, a, b) for (int i = (a), i##_ = (b) - 1; i > i##_; --i)
using namespace std;
const int maxn = 2e5 + 5, P = 998244353;
using ll = int64_t;
#define ADD(a, b) (((a) += (b)) >= P ? (a) -= P : 0)
#define SUB(a, b) (((a) -= (b)) < 0 ? (a) += P : 0)
#define MUL(a, b) (ll(a) * (b) % P)
int POW(ll a, int b = P - 2, ll x = 1)
{
for (; b; b >>= 1, a = a * a % P)
if (b & 1)
x = x * a % P;
return x;
}
namespace NTT
{
const int g = 3;
vector<int> Omega(int L)
{
int wn = POW(g, P / L);
vector<int> w(L);
w[L >> 1] = 1;
fp(i, L / 2 + 1, L - 1) w[i] = MUL(w[i - 1], wn);
fd(i, L / 2 - 1, 1) w[i] = w[i << 1];
return w;
}
auto W = Omega(1 << 21);
void DIF(int *a, int n)
{
for (int k = n >> 1; k; k >>= 1)
{
for (int i = 0, y; i < n; i += k << 1)
fp(j, 0, k - 1)
y = a[i + j + k],
a[i + j + k] = MUL(a[i + j] - y + P, W[k + j]), ADD(a[i + j], y);
}
}
void IDIT(int *a, int n)
{
for (int k = 1; k < n; k <<= 1)
{
for (int i = 0, x, y; i < n; i += k << 1)
fp(j, 0, k - 1)
x = a[i + j],
y = MUL(a[i + j + k], W[k + j]),
a[i + j + k] = x - y < 0 ? x - y + P : x - y, ADD(a[i + j], y);
}
int Inv = P - (P - 1) / n;
fp(i, 0, n - 1) a[i] = MUL(a[i], Inv);
reverse(a + 1, a + n);
}
}
namespace Polynomial
{
using Poly = std::vector<int>;
Poly &operator*=(Poly &a, int b)
{
for (auto &x : a)
x = MUL(x, b);
return a;
}
Poly operator*(Poly a, int b) { return a *= b; }
Poly operator*(int a, Poly b) { return b * a; }
void DFT(Poly &a) { NTT::DIF(a.data(), a.size()); }
void IDFT(Poly &a) { NTT::IDIT(a.data(), a.size()); }
int norm(int n) { return 1 << (32 - __builtin_clz(n - 1)); }
void norm(Poly &a)
{
if (!a.empty())
a.resize(norm(a.size()), 0);
}
Poly &dot(Poly &a, Poly &b)
{
fp(i, 0, a.size() - 1) a[i] = MUL(a[i], b[i]);
return a;
}
Poly operator*(Poly a, Poly b)
{
int n = a.size() + b.size() - 1, L = norm(n);
if (a.size() <= 8 || b.size() <= 8)
{
Poly c(n);
fp(i, 0, a.size() - 1) fp(j, 0, b.size() - 1)
c[i + j] = (c[i + j] + (ll)a[i] * b[j]) % P;
return c;
}
a.resize(L), b.resize(L);
DFT(a), DFT(b), dot(a, b), IDFT(a);
return a.resize(n), a;
}
}
using namespace Polynomial;
mt19937_64 rnd(time(0));
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
map<pair<char, int>, int> ma;
Poly a1(n), a2(n), a3(n), b1(m), b2(m), b3(m);
for (int i = 0; i < n; i++)
{
int v = 0;
char c;
cin >> c;
if (c != '?')
{
int t;
cin >> t;
if (ma.find({c, t}) == ma.end())
ma[{c, t}] = rnd() % P;
v = ma[{c, t}];
}
a1[i] = (ll)v * v % P * v % P;
a2[i] = ((ll)P - 2) * v % P * v % P;
a3[i] = v;
}
for (int i = 0; i < m; i++)
{
int v = 0;
char c;
cin >> c;
if (c != '?')
{
int t;
cin >> t;
if (ma.find({c, t}) == ma.end())
ma[{c, t}] = rnd() % P;
v = ma[{c, t}];
}
b1[i] = v;
b2[i] = (ll)v * v % P;
b3[i] = (ll)v * v % P * v % P;
}
reverse(b1.begin(), b1.end());
reverse(b2.begin(), b2.end());
reverse(b3.begin(), b3.end());
auto g1 = a1 * b1;
auto g2 = a2 * b2;
auto g3 = a3 * b3;
// for (auto it : g1)
// cout << it << " ";
// cout << endl;
// for (auto it : g2)
// cout << it << " ";
// cout << endl;
// for (auto it : g3)
// cout << it << " ";
// cout << endl;
for (int i = 0; i < g1.size(); i++)
{
ADD(g1[i], g2[i]);
ADD(g1[i], g3[i]);
}
int ans = 0;
for (int i = m - 1; i <= n - 1; i++)
ans += (g1[i] == 0);
cout << ans << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 11280kb
input:
4 3 n 4 e 1 ? s 5 ? e 1 ?
output:
2
result:
ok answer is '2'
Test #2:
score: 0
Accepted
time: 7ms
memory: 11324kb
input:
4 3 n 4 e 1 w 3 s 5 ? e 1 ?
output:
1
result:
ok answer is '1'
Test #3:
score: 0
Accepted
time: 233ms
memory: 33576kb
input:
400000 200000 ? ? e 7 e 7 ? ? ? e 7 e 7 ? ? e 7 ? e 7 e 7 ? e 7 ? ? ? e 7 ? ? e 7 ? ? ? ? e 7 ? e 7 ? ? e 7 ? ? e 7 e 7 e 7 ? ? ? ? ? ? e 7 ? e 7 ? ? ? ? ? ? ? ? ? ? e 7 e 7 ? ? e 7 ? e 7 e 7 ? ? e 7 e 7 e 7 ? e 7 e 7 ? ? ? ? ? ? e 7 e 7 ? ? e 7 ? ? ? ? e 7 ? ? ? e 7 ? e 7 ? e 7 ? ? ? ? ? e 7 ? ? ? ...
output:
133037
result:
ok answer is '133037'
Test #4:
score: 0
Accepted
time: 62ms
memory: 18952kb
input:
200000 10000 e 1 s 1 w 1 n 2 e 2 s 2 w 2 n 3 e 3 s 3 w 3 n 4 e 4 s 4 w 4 n 5 e 5 s 5 w 5 n 6 e 6 s 6 w 6 n 7 e 7 s 7 w 7 n 1 e 1 s 1 w 1 n 2 e 2 s 2 w 2 n 3 e 3 s 3 w 3 n 4 e 4 s 4 w 4 n 5 e 5 s 5 w 5 n 6 e 6 s 6 w 6 n 7 e 7 s 7 w 7 n 1 e 1 s 1 w 1 n 2 e 2 s 2 w 2 n 3 e 3 s 3 w 3 n 4 e 4 s 4 w 4 n 5...
output:
6786
result:
ok answer is '6786'
Test #5:
score: 0
Accepted
time: 58ms
memory: 18880kb
input:
200000 10000 e 1 s 1 w 1 n 2 e 2 ? w 2 n 3 e 3 s 3 w 3 n 4 e 4 s 4 w 4 n 5 e 5 s 5 w 5 n 6 e 6 s 6 w 6 n 7 e 7 s 7 w 7 ? e 1 s 1 w 1 n 2 e 2 s 2 w 2 ? e 3 s 3 w 3 n 4 e 4 s 4 w 4 n 5 e 5 s 5 w 5 n 6 e 6 s 6 w 6 n 7 e 7 s 7 w 7 n 1 e 1 s 1 w 1 n 2 e 2 s 2 w 2 n 3 e 3 s 3 w 3 n 4 e 4 s 4 w 4 n 5 e 5 s...
output:
6786
result:
ok answer is '6786'
Test #6:
score: 0
Accepted
time: 61ms
memory: 17024kb
input:
100000 50000 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1 n 1...
output:
523
result:
ok answer is '523'
Test #7:
score: 0
Accepted
time: 28ms
memory: 16788kb
input:
200000 4 s 3 e 2 ? n 5 s 2 e 2 e 5 w 2 w 6 w 5 w 2 n 4 e 3 ? n 5 e 4 n 5 ? s 2 w 1 e 4 s 3 ? ? n 1 e 3 w 7 n 5 w 5 e 1 ? w 3 s 6 e 7 e 4 e 5 n 5 w 3 w 7 e 2 s 2 w 2 e 1 s 3 s 4 w 7 n 3 w 6 e 4 s 7 w 5 n 5 ? ? s 2 e 1 s 3 ? s 1 e 6 n 6 e 7 ? s 6 s 3 w 6 w 2 e 7 e 3 w 3 e 6 e 4 n 2 n 7 w 3 s 5 w 7 n 2...
output:
52
result:
ok answer is '52'
Test #8:
score: 0
Accepted
time: 116ms
memory: 22656kb
input:
200000 100000 ? e 6 e 7 n 5 e 5 w 4 w 1 e 3 e 3 n 6 n 6 w 1 e 2 e 6 s 4 s 2 e 4 s 6 w 6 e 6 n 2 e 1 n 5 s 5 e 3 e 4 w 7 n 1 s 6 n 6 n 5 s 6 s 2 w 7 e 6 e 4 e 4 n 1 ? w 2 n 1 n 3 e 2 w 1 e 1 s 6 w 7 s 1 e 2 e 3 e 3 e 7 s 1 e 2 w 1 w 6 s 1 s 7 e 4 e 1 s 1 w 5 ? s 5 n 4 w 2 ? e 3 n 6 n 2 s 4 s 1 n 7 n ...
output:
0
result:
ok answer is '0'
Test #9:
score: 0
Accepted
time: 111ms
memory: 22516kb
input:
200000 100000 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e 7 e ...
output:
100001
result:
ok answer is '100001'