A-Don't Sit Next To Me
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7, N = 1e6 + 10;
int f[N], g[N];
int power(int a, int b) {
int res = 1;
for(; b; b >>= 1, a = 1LL * a * a % P)
if(b & 1)
res = 1LL * res * a % P;
return res;
}
void init() {
for(int i = 3; i < N; ++ i)
f[i] = (f[(i - 1) / 2] + f[i - 1 - (i - 1) / 2] + 1) % P;
for(int i = 2; i < N; ++ i)
g[i] = (f[i - 1] + 1) % P;
for(int i = 1; i < N; ++ i)
g[i] = (g[i] + g[i - 1]) % P;
}
int main() {
init();
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T;
cin >> T;
while(T -- ) {
int n;
cin >> n;
int up = (2LL * g[n - 1] + n) % P, down = power(n, P - 2);
cout << 1LL * up * down % P << endl;
}
return 0;
}
B-Counting The Stars
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, lenx, leny, a[N], ans[N];
int cnt[N], cnt_block[N];
int get_x(int x) { return x / lenx; }
int get_y(int x) { return x / leny; }
struct Query {
int id, l, r, d, u;
bool operator < (const Query& t) const {
int i = get_x(l), j = get_x(t.l);
if(i != j) return i < j;
if(i & 1) return r < t.r;
return r > t.r;
}
}q[N];
void add(int x) { if(!cnt[a[x]]) cnt_block[get_y(a[x])] ++; cnt[a[x]] ++; }
void del(int x) { cnt[a[x]] --; if(!cnt[a[x]]) cnt_block[get_y(a[x])] --; }
int query(int l, int r) {
int res = 0;
if(get_y(l) == get_y(r)) {
for(int i = l; i <= r; ++ i)
res += (cnt[i] ? 1 : 0);
}
else {
int i = l, j = r;
while(get_y(i) == get_y(l)) { res += (cnt[i] ? 1 : 0); ++ i; }
while(get_y(j) == get_y(r)) { res += (cnt[j] ? 1 : 0); -- j; }
for(int k = get_y(i); k <= get_y(j); ++ k) res += cnt_block[k];
}
return res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T;
cin >> T;
while(T -- ) {
for(int i = 0; i < N; ++ i) cnt[i] = cnt_block[i] = 0;
cin >> n >> m;
for(int i = 1; i <= n; ++ i) cin >> a[i];
lenx = sqrt(n);
leny = sqrt(100000);
for(int i = 1; i <= m; ++ i) {
int x0, x1, y0, y1;
cin >> x0 >> y0 >> x1 >> y1;
q[i] = {i, x0, x1, y0, y1};
}
sort(q + 1, q + m + 1);
for(int k = 1, r = 0, l = 1; k <= m; ++ k) {
while(r < q[k].r) add(++ r);
while(r > q[k].r) del(r --);
while(l < q[k].l) del(l ++);
while(l > q[k].l) add(-- l);
ans[q[k].id] = query(q[k].d, q[k].u);
}
for(int i = 1; i <= m; ++ i) cout << ans[i] << "\n";
}
return 0;
}
C-Building Roads
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int, int>;
using PLI = pair<LL, int>;
const LL INF = 1e18;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n, m, s1, s2, t;
cin >> n >> m >> s1 >> s2 >> t;
vector<vector<PII>> G(n), rG(n);
for(int i = 0; i < m; ++ i) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
rG[v].push_back({u, w});
}
auto dijkstra = [&](int s, vector<vector<PII>> &g) -> vector<LL> {
vector<bool> vis(n, 0);
vector<LL> d(n, INF);
priority_queue<PLI, vector<PLI>, greater<PLI>> Q;
d[s] = 0;
Q.push({0, s});
while(!Q.empty()) {
auto u = Q.top().second; Q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(auto &[v, w] : g[u]) {
if(d[v] > d[u] + w) {
d[v] = d[u] + w;
Q.push({d[v], v});
}
}
}
return d;
};
auto d1 = dijkstra(s1, G);
auto d2 = dijkstra(s2, G);
auto d3 = dijkstra(t, rG);
LL ans = INF;
for(int i = 0; i < n; ++ i)
ans = min(ans, d1[i] + d2[i] + d3[i]);
cout << (ans >= INF ? -1 : ans) << "\n";
return 0;
}
D-Money Tree
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 510;
int n, m, w[N];
LL dp[N][N];
vector<int> G[N];
int DFS(int u, int f) {
int p = 1;
dp[u][1] = w[u];
for(auto v : G[u]) {
if(v == f) continue;
int siz = DFS(v, u);
for(int i = min(m, p); i; -- i)
for(int j = 1; j <= siz && i + j <= m; ++ j)
dp[u][i + j] = min(dp[u][i + j], dp[u][i] + dp[v][j]);
p += siz;
}
return p;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
LL sum = 0;
m = n - m;
for(int i = 1; i < n; ++ i) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1; i <= n; ++ i) cin >> w[i], sum += w[i];
memset(dp, 0x3f, sizeof dp);
DFS(1, 1);
cout << sum - dp[1][m] << "\n";
return 0;
}
E-Max Min Count
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 200010;
LL n, a[N];
LL ans;
void solve(LL x, LL y, LL t) {
int lst = 1;
for(int i = 1; i <= n; ++ i) {
if(a[i] > x or a[i] < y) {
lst = i + 1;
}
ans += (i - lst + 1) * t;
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
LL x, y;
cin >> n >> x >> y;
for(int i = 1; i <= n; ++ i) cin >> a[i];
solve(x, y, 1);
solve(x - 1, y, -1);
solve(x, y + 1, -1);
solve(x - 1, y + 1, 1);
cout << ans << "\n";
return 0;
}
F-Shoot Balloon
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
template <typename T>void read(T &x) {
x = 0; register int f = 1;
register char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-')f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();}
x *= f;
}
namespace Polynomial {
using Poly = std::vector<int>;
constexpr int P(998244353), G(3), L(1 << 18); // L : max length of dft
inline void inc(int &x, int y) { (x += y) >= P ? x -= P : 0; }
inline int mod(int64_t x) { return x % P; }
inline int fpow(int x, int k = P - 2) {
int r = 1;
for (; k; k >>= 1, x = 1LL * x * x % P)
if (k & 1) r = 1LL * r * x % P;
return r;
}
int w[L], _ = [] {
w[L / 2] = 1;
for (int i = L / 2 + 1, x = fpow(G, (P - 1) / L); i < L; i++) w[i] = 1LL * w[i - 1] * x % P;
for (int i = L / 2 - 1; i >= 0; i--) w[i] = w[i << 1];
return 0;
}();
void dft(int *a, int n) {
assert((n & n - 1) == 0);
for (int k = n >> 1; k; k >>= 1) {
for (int i = 0; i < n; i += k << 1) {
for (int j = 0; j < k; j++) {
int y = a[i + j + k];
a[i + j + k] = 1LL * (a[i + j] - y + P) * w[k + j] % P;
inc(a[i + j], y);
}
}
}
}
void idft(int *a, int n) {
assert((n & n - 1) == 0);
for (int k = 1; k < n; k <<= 1) {
for (int i = 0; i < n; i += k << 1) {
for (int j = 0; j < k; j++) {
int x = a[i + j], y = 1LL * a[i + j + k] * w[k + j] % P;
a[i + j + k] = x - y < 0 ? x - y + P : x - y;
inc(a[i + j], y);
}
}
}
for (int i = 0, inv = P - (P - 1) / n; i < n; i++)
a[i] = 1LL * a[i] * inv % P;
std::reverse(a + 1, a + n);
}
// expand dft of length n to 2n
void dftDoubling(int *a, int n) {
std::copy_n(a, n, a + n);
idft(a + n, n);
for (int i = 0; i < n; i++) a[n + i] = 1LL * w[n + i] * a[n + i] % P;
dft(a + n, n);
}
int norm(int n) { return 1 << std::__lg(n * 2 - 1); }
void norm(Poly &a) {
if (!a.empty()) a.resize(norm(a.size()));
}
void dft(Poly &a) { dft(a.data(), a.size()); }
void idft(Poly &a) { idft(a.data(), a.size()); }
inline Poly &dotEq(Poly &a, Poly b) {
assert(a.size() == b.size());
for (int i = 0; i < a.size(); i++) a[i] = 1LL * a[i] * b[i] % P;
return a;
}
inline Poly dot(Poly a, Poly b) { return dotEq(a, b); }
Poly operator*(const Poly &a, const Poly &b) {
int len = a.size() + b.size() - 1;
if (a.size() <= 16 || b.size() <= 16) {
Poly c(len);
for (size_t i = 0; i < a.size(); i++)
for (size_t j = 0; j < b.size(); j++)
c[i + j] = (c[i + j] + 1LL * a[i] * b[j]) % P;
return c;
}
int n = norm(len);
Poly foo = a;
foo.resize(n);
dft(foo);
if (&a == &b) {
dotEq(foo, foo);
} else {
Poly bar = b;
bar.resize(n);
dft(bar);
dotEq(foo, bar);
}
idft(foo);
foo.resize(len);
return foo;
}
} // namespace Polynomial
using namespace Polynomial;
using LL = long long;
const int dlt = 30000;
int main() {
int nu, nm, nl;
read(nu);
int N = 60000;
Poly A(N + 1), C(N + 1);
for(int i = 0, x; i < nu; ++ i) {
read(x);
A[x + dlt] += 1;
}
read(nm);
vector<int> B;
for(int i = 0, x; i < nm; ++ i) {
read(x);
B.push_back(x + dlt);
}
read(nl);
for(int i = 0, x; i < nl; ++ i) {
read(x);
C[x + dlt] += 1;
}
A = A * C;
LL ans = 0;
for(int &x : B) ans += A[x * 2];
cout << ans << "\n";
return 0;
}
G-String Value
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
vector<int> pos(26, -1);
long long ans = 0;
for(int i = 0; i < n; ++ i) {
ans += 1LL * (i - pos[s[i] - 'a']) * (n - i);
pos[s[i] - 'a'] = i;
}
cout << ans << "\n";
return 0;
}
H-Flip Matrix
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T;
cin >> T;
while(T -- ) {
int n;
cin >> n;
vector<vector<int>> A(n, vector<int> (n));
for(int i = 0; i < n; ++ i)
for(int j = 0; j < n; ++ j)
cin >> A[i][j];
vector<int> row, col;
auto solve = [&]() -> bool {
row.clear();
col.clear();
for(int i = 1; i < n; ++ i) {
int c = 0;
for(int j = 0; j < n; ++ j)
c += (A[i][j] ^ A[0][j]);
if(c > 0 and c < n) return 0;
if(c == n) row.push_back(i);
}
for(int i = 0; i < n; ++ i)
if(A[0][i]) col.push_back(i);
return 1;
};
if(solve()) {
cout << "YES\n";
cout << row.size() + col.size() << "\n";
for(int &i : row) cout << "row " << i + 1 << "\n";
for(int &i : col) cout << "col " << i + 1 << "\n";
continue;
};
for(int i = 0; i < n; ++ i) A[i][i] ^= 1;
if(solve()) {
cout << "YES\n";
cout << row.size() + col.size() + 1 << "\n";
cout << "diagonal\n";
for(int &i : row) cout << "row " << i + 1 << "\n";
for(int &i : col) cout << "col " << i + 1 << "\n";
continue;
};
cout << "NO\n";
}
return 0;
}
I-Exam Week
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
int f[55][510];
int g[55][510][5];
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T;
cin >> T;
while(T -- ) {
map<string, int> mp;
memset(g, -0x3f, sizeof g);
memset(f, 0, sizeof f);
int n, m; cin >> n;
string str;
int idx = 0;
for(int i = 1; i <= n; ++ i) cin >> str, mp[str] = ++ idx;
cin >> m;
int w, c;
for(int i = 1; i <= m; ++ i) {
cin >> str >> w >> c;
int id = mp[str];
for(int j = 500; j >= c; -- j) {
f[id][j] = max(f[id][j - c] + w, f[id][j]);
f[id][j] = min(f[id][j], 100);
}
}
int t, p;
cin >> t >> p;
g[0][0][0] = 0;
for(int i = 1; i <= n; ++ i)
for(int j = t; j >= 0; -- j)
for(int k = 0; k <= t; ++ k) {
if(k > j) break;
for(int q = 0; q <= p; ++ q) {
if(f[i][k] >= 60) g[i][j][q] = max(g[i - 1][j - k][q] + f[i][k], g[i][j][q]);
else g[i][j][q + 1] = max(g[i - 1][j - k][q] + f[i][k], g[i][j][q + 1]);
}
}
int res = -1;
for(int i = 0; i <= p; ++ i) res = max(res, g[n][t][i]);
cout << res << "\n";
}
return 0;
}
J-Dangerous Maze
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n, m, x, y;
cin >> n >> m >> x >> y;
if((n * m - x - y) % 2 == 0) {
cout << "-1\n";
return 0;
}
int a = 1, b = 1, ok = 0;
if(n % 2 == 0) {
while(b < y) {
for(int i = 1; i < n; ++ i) cout << (ok ? 'L' : 'R');
cout << 'U';
ok ^= 1;
b ++;
a = n + 1 - a;
}
ok = 0;
for(int i = 1; i <= n; ++ i) {
if((a == 1 and i == x) or (a == n and i == n - x + 1)) m --;
for(int j = b; j < m; ++ j) cout << (ok ? 'D' : 'U');
ok ^= 1;
if(i == n) break;
else cout << (a == 1 ? 'R' : 'L');
}
a = n + 1 - a;
cout << 'D';
if(a == 1) for(int i = 1; i < x; ++ i) cout << 'R';
else for(int i = n; i > x; -- i) cout << 'L';
}
else if(m % 2 == 0) {
swap(m, n);
swap(x, y);
while(b < y) {
for(int i = 1; i < n; ++ i) cout << (ok ? 'D' : 'U');
cout << 'R';
ok ^= 1;
b ++;
a = n + 1 - a;
}
ok = 0;
for(int i = 1; i <= n; ++ i) {
if((a == 1 and i == x) or (a == n and i == n - x + 1)) m --;
for(int j = b; j < m; ++ j) cout << (ok ? 'L' : 'R');
ok ^= 1;
if(i == n) break;
else cout << (a == 1 ? 'U' : 'D');
}
a = n + 1 - a;
cout << 'L';
if(a == 1) for(int i = 1; i < x; ++ i) cout << 'U';
else for(int i = n; i > x; -- i) cout << 'D';
}
else {
while(b < y) {
for(int i = 1; i < n; ++ i) cout << (ok ? 'L' : 'R');
cout << 'U';
ok ^= 1;
b ++;
a = n + 1 - a;
}
if(a == 1) {
while(b <= m) {
for(int i = 1; i < (b == m ? x : x - 1); ++ i) cout << (ok ? 'L' : 'R');
if(b == m) break;
cout << 'U';
ok ^= 1;
b ++;
}
ok = 0;
for(int i = x; i < n; ++ i) {
for(int j = m; j > y + 1; -- j) cout << (ok ? 'U' : 'D');
ok ^= 1;
cout << 'R';
}
for(int i = m; i > y; -- i) cout << 'D';
for(int i = n; i > x; -- i) cout << 'L';
}
else {
ok = 0;
for(int i = n; i > x; -- i) {
for(int j = y; j < m; ++ j) cout << (ok ? 'D' : 'U');
cout << 'L';
ok ^= 1;
}
ok = 0;
for(int i = m; i >= y; -- i) {
for(int j = 1; j < x; ++ j) cout << (ok ? 'R' : 'L');
if(i != y) cout << 'D';
ok ^= 1;
}
}
}
return 0;
}
K-A Simple Question
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int T;
cin >> T;
while(T -- ) {
LL n;
cin >> n;
n --;
LL t = log2(n);
cout << (1ll << t) - 1 << "\n";
}
return 0;
}
L-Polynomial Division
Sample Code (C++)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> a(n + 1), b(m + 1, 0), c(n + m + 1);
for(int i = 0; i < n + 1; ++ i) cin >> a[i];
for(int i = 0; i < n + m + 1; ++ i) cin >> c[i];
for(int x = 0; x < n + 1; ++ x) {
if(a[x] == 0) continue;
for(int i = x; i - x < m + 1; ++ i) {
int t = c[i];
for(int j = x + 1; j <= i and j < n + 1; ++ j)
t -= a[j] * b[i - j];
b[i - x] = t / a[x];
}
break;
}
for(int i = 0; i < m + 1; ++ i) cout << b[i] << " \n"[i == m];
return 0;
}