(A) Number Transformation
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../template/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int x, y;
cin >> x >> y;
if (y % x != 0) {
cout << "0 0\n";
continue;
}
int d = y / x;
bool found = false;
for (int a = 1; a <= 100; a++) {
for (int b = 1; b <= 7; b++) {
long long c = 1;
for (int i = 0; i < b; i++) {
c *= a;
}
if (c > y) continue;
if (c == d) {
cout << b << ' ' << a << '\n';
found = true;
break;
}
if (found) break;
}
if (found) break;
}
if (!found) {
cout << "0 0\n";
}
}
return 0;
}
(B) Dictionary
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../template/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
map<string, int> mp;
int w = 1;
for (char a = 'a'; a <= 'z'; a++) {
for (char b = 'a'; b <= 'z'; b++) {
if (a == b) continue;
string t = string(1, a) + b;
mp[t] = w++;
}
}
while (tt--) {
string s;
cin >> s;
cout << mp[s] << '\n';
}
return 0;
}
(C) Infinite Replacement
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../template/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
string s, t;
cin >> s >> t;
if (t.size() > 1 && t.find('a') != string::npos) {
cout << "-1\n";
continue;
}
bool has = s.find(t) != string::npos;
if (has) {
cout << "1\n";
} else {
cout << (1ll << (int) s.size()) << '\n';
}
}
return 0;
}
(D) A-B-C Sort
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../template/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 1; i -= 2) {
if (a[i - 1] > a[i]) {
swap(a[i - 1], a[i]);
}
}
cout << (is_sorted(a.begin(), a.end()) ? "YES" : "NO") << '\n';
}
return 0;
}
(F) Desktop Rearrangement
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../template/debug.hpp"
#else
#define debug(...) 42
#endif
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) { fenw.resize(n); }
inline void modify(int x, T v) {
assert(x >= 0 && x < n);
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
inline T get(int x) {
assert(x >= 0 && x < n);
T res{};
while (x >= 0) {
res += fenw[x];
x = (x & (x + 1)) - 1;
}
return res;
}
inline T get(int l, int r) {
assert(l >= 0 && l < n);
assert(r >= 0 && r < n);
T res = get(r);
if (l - 1 >= 0) {
res -= get(l - 1);
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
int cnt = 0;
vector<string> g(n);
for (int i = 0; i < n; i++) {
cin >> g[i];
}
fenwick<int> fenw(n * m + n + 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] == '*') {
cnt++;
fenw.modify(j * n + i + 1, 1);
}
}
}
while (q--) {
int a, b;
cin >> a >> b;
--a, --b;
int x = b * n + a + 1;
if (fenw.get(x) - fenw.get(x - 1) != 0) {
cnt--;
fenw.modify(x, -1);
int old = fenw.get(cnt);
cout << cnt - old << '\n';
} else {
cnt++;
fenw.modify(x, 1);
int old = fenw.get(cnt);
cout << cnt - old << '\n';
}
}
return 0;
}
(G) Remove Directed Edges
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../template/debug.hpp"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
vector<int> in(n);
vector<int> out(n);
vector<int> cur(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
in[b]++;
out[a]++;
cur[b]++;
}
queue<int> que;
for (int i = 0; i < n; i++) {
if (!cur[i]) {
que.push(i);
}
}
int ans = 1;
vector<int> f(n, 1);
while (!que.empty()) {
int t = que.front();
que.pop();
ans = max(ans, f[t]);
for (auto j : adj[t]) {
if (out[t] > 1 && in[j] > 1) {
f[j] = max(f[j], f[t] + 1);
}
if (!--cur[j]) {
que.push(j);
}
}
}
cout << ans << '\n';
return 0;
}