AtCoder Beginner Contest 419
A - AtCoder Language
点击查看代码
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
std::string s;
std::cin >> s;
if (s == "red") {
std::cout << "SSS\n";
} else if (s == "blue") {
std::cout << "FFF\n";
} else if (s == "green") {
std::cout << "MMM \n";
} else {
std::cout << "Unknown\n";
}
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
int t = 1;
// std::cin >> t;
while (t -- ) {
solve();
}
return 0;
}
B - Get Min
点击查看代码
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
std::multiset<int> s;
while (n -- ) {
int op;
std::cin >> op;
if (op == 1) {
int x;
std::cin >> x;
s.insert(x);
} else {
std::cout << *s.begin() << "\n";
s.erase(s.begin());
}
}
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
int t = 1;
// std::cin >> t;
while (t -- ) {
solve();
}
return 0;
}
C - King's Summit
题意:平面上\(n\)个人,每个人一步可以走到相邻的八个格子上也可以不走。求所有人走到同一个格子的最小步数。
容易看出两个格子的距离是\(\max(|x_i - x_j|, |y_i - y_j|)\)。
那么求出最大和最小的\(x\)和\(y\),则距离最远的两个人距离为\(\max(max_x -min_x, max_y - min_y)\)。两个人可以双向奔赴,所有答案除二向上取整。
点击查看代码
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::cin >> n;
int minx = 2e9, maxx = -2e9, miny = 2e9, maxy = -2e9;
for (int i = 0; i < n; ++ i) {
int x, y;
std::cin >> x >> y;
minx = std::min(minx, x);
miny = std::min(miny, y);
maxx = std::max(maxx, x);
maxy = std::max(maxy, y);
}
std::cout << (std::max(maxx - minx, maxy - miny) + 1) / 2 << "\n";
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
int t = 1;
// std::cin >> t;
while (t -- ) {
solve();
}
return 0;
}
D - Substr Swap
题意:两个字符串\(s, t\),每次交换它们的\([l_i, r_i]\)这一部分。求最后的\(s\)。
注意到\(s_i\)只会和\(t_i\)交换,且交换偶数次又会换回来。那么可以差分。
点击查看代码
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, m;
std::cin >> n >> m;
std::string s, t;
std::cin >> s >> t;
std::vector<int> d(n + 2);
for (int i = 0; i < m; ++ i) {
int l, r;
std::cin >> l >> r;
d[l] += 1;
d[r + 1] -= 1;
}
for (int i = 1; i <= n; ++ i) {
d[i] += d[i - 1];
if (d[i] & 1) {
std::swap(s[i - 1], t[i - 1]);
}
}
std::cout << s << "\n";
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
int t = 1;
// std::cin >> t;
while (t -- ) {
solve();
}
return 0;
}
E - Subarray Sum Divisibility
题意:长度为\(n\)的数字,你每次可以让一个数加一。要使得每个长度为\(L\)的子数组的和都是\(m\)的倍数。求最小操作数。
\(a_i, a_{i+L}, a_{i+2L}, ...\)对\(m\)取模后相同。因为\([i, i + L - 1]\)这一部分是\(m\)的倍数,\([i + 1, i + L]\)也是,也就是两个部分模\(m\)都是\(0\),则\(a_i + a_{i+1} + ... + a_{i+L-1} \equiv a_{i+1} + ... + a_{i+L-1} + a_{i+L} \pmod{m}\),得到\(a_i \equiv a_{i+L} \pmod{m}\).
那么可以求出\(w[i][k]\)表示下标模\(L\)等于\(i\)的所有位置模\(m\)都等于\(k\)的花费。然后我们只需要确定前\(L\)个数的值就确定了整个数组,那么记\(f[i][j]\)为前\(i\)个数总和模\(m\)为\(j\)时的最小操作数,那么有\(f[i][(j + k) \% m] = \min(f[i - 1][(j + k) \% m], f[i - 1][j] + w[i][k])\)。这是一个背包。
点击查看代码
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n, m, L;
std::cin >> n >> m >> L;
std::vector<int> a(n);
for (int i = 0; i < n; ++ i) {
std::cin >> a[i];
}
std::vector w(L, std::vector<int>(m));
for (int i = 0; i < L; ++ i) {
for (int j = i; j < n; j += L) {
for (int k = 0; k < m; ++ k) {
w[i][(a[j] + k) % m] += k;
}
}
}
const int inf = 1e9;
std::vector<int> f(m, inf);
f[0] = 0;
for (int i = 0; i < L; ++ i) {
std::vector<int> g(m, inf);
for (int j = 0; j < m; ++ j) {
for (int k = 0; k < m; ++ k) {
g[(j + k) % m] = std::min(g[(j + k) % m], f[j] + w[i][k]);
}
}
f = g;
}
std::cout << f[0] << "\n";
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
int t = 1;
// std::cin >> t;
while (t -- ) {
solve();
}
return 0;
}
F - All Included
题意:有\(n\)个字符串,求有多少长度为\(L\)的字符串使得这\(n\)个字符串都作为子串出现过。
状压\(dp\)加\(ac\)自动机。
记\(f[i][j][k]\)表示长度为\(i\)的字符串包含这\(n\)个字符串的状态二进制表示为\(j\)时,匹配到了\(ac\)自动机上的第\(k\)个节点的方案数。\(ac\)自动机里提前记录每个节点\(fail\)链上可以包含多少个字符串,枚举下一个字符转移即可。
点击查看代码
#include <bits/stdc++.h>
using i64 = long long;
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 = a * b - i64(1.L * a * b / p) * p;
res %= p;
if (res < 0) {
res += p;
}
return res;
}
template<i64 P>
struct MLong {
i64 x;
constexpr MLong() : x{} {}
constexpr MLong(i64 x) : x{norm(x % getMod())} {}
static i64 Mod;
constexpr static i64 getMod() {
if (P > 0) {
return P;
} else {
return 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 MLong operator-() const {
MLong res;
res.x = norm(getMod() - x);
return res;
}
constexpr MLong inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MLong &operator*=(MLong rhs) & {
x = mul(x, rhs.x, getMod());
return *this;
}
constexpr MLong &operator+=(MLong rhs) & {
x = norm(x + rhs.x);
return *this;
}
constexpr MLong &operator-=(MLong rhs) & {
x = norm(x - rhs.x);
return *this;
}
constexpr MLong &operator/=(MLong rhs) & {
return *this *= rhs.inv();
}
friend constexpr MLong operator*(MLong lhs, MLong rhs) {
MLong res = lhs;
res *= rhs;
return res;
}
friend constexpr MLong operator+(MLong lhs, MLong rhs) {
MLong res = lhs;
res += rhs;
return res;
}
friend constexpr MLong operator-(MLong lhs, MLong rhs) {
MLong res = lhs;
res -= rhs;
return res;
}
friend constexpr MLong operator/(MLong lhs, MLong rhs) {
MLong res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {
i64 v;
is >> v;
a = MLong(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {
return os << a.val();
}
friend constexpr bool operator==(MLong lhs, MLong rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MLong lhs, MLong rhs) {
return lhs.val() != rhs.val();
}
};
template<>
i64 MLong<0LL>::Mod = i64(1E18) + 9;
template<int P>
struct MInt {
int x;
constexpr MInt() : x{} {}
constexpr MInt(i64 x) : x{norm(x % getMod())} {}
static int Mod;
constexpr static int getMod() {
if (P > 0) {
return P;
} else {
return Mod;
}
}
constexpr static void setMod(int Mod_) {
Mod = Mod_;
}
constexpr int norm(int x) const {
if (x < 0) {
x += getMod();
}
if (x >= getMod()) {
x -= getMod();
}
return x;
}
constexpr int val() const {
return x;
}
explicit constexpr operator int() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(getMod() - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, getMod() - 2);
}
constexpr MInt &operator*=(MInt rhs) & {
x = 1LL * 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();
}
};
template<>
int MInt<0>::Mod = 998244353;
template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr int P = 998244353;
using Z = MInt<P>;
constexpr int SIZE = 26;
constexpr int N = 100;
int tr[N][SIZE], len[N], end[N], next[N];
int idx;
struct ACAM {
ACAM() {
init();
}
int newNode() {
memset(tr[idx], 0, sizeof tr[idx]);
end[idx] = 0;
next[idx] = 0;
len[idx] = 0;
return idx ++ ;
}
void init() {
idx = 0;
newNode();
}
int insert(const std::string & s, int id) {
int p = 0;
for (auto & c : s) {
int x = c - 'a';
if (!tr[p][x]) {
tr[p][x] = newNode();
len[tr[p][x]] = len[p] + 1;
}
p = tr[p][x];
}
end[p] |= 1 << id;
return p;
}
void build() {
std::queue<int> q;
for (int i = 0; i < SIZE; ++ i) {
if (tr[0][i]) {
q.push(tr[0][i]);
}
}
while (q.size()) {
int u = q.front(); q.pop();
for (int i = 0; i < SIZE; ++ i) {
int v = tr[u][i];
if (!v) {
tr[u][i] = tr[next[u]][i];
} else {
next[v] = tr[next[u]][i];
q.push(v);
}
}
end[u] |= end[next[u]];
}
}
};
void solve() {
int n, L;
std::cin >> n >> L;
ACAM ac;
for (int i = 0; i < n; ++ i) {
std::string s;
std::cin >> s;
ac.insert(s, i);
}
ac.build();
int m = idx;
std::vector f(1 << n, std::vector<Z>(m));
f[0][0] = 1;
for (int i = 0; i < L; ++ i) {
std::vector g(1 << n, std::vector<Z>(m));
for (int s = 0; s < 1 << n; ++ s) {
for (int j = 0; j < m; ++ j) {
if (f[s][j] == 0) {
continue;
}
for (int c = 0; c < 26; ++ c) {
int nj = tr[j][c];
int ns = s | end[nj];
g[ns][nj] += f[s][j];
}
}
}
f = g;
}
Z ans = 0;
for (int i = 0; i < m; ++ i) {
ans += f[(1 << n) - 1][i];
}
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
int t = 1;
// std::cin >> t;
while (t -- ) {
solve();
}
return 0;
}