Tutorial
Tutorial is loading...
Solution
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
cnt0 = a.count(0)
if cnt0 >= 2:
print(a[0] + a[-1])
else:
print(-1)
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
void solve()
{
long long x, y, k;
cin >> x >> y >> k;
long long ans = 0;
for(long long i = 0; i < min(y, k); i++)
ans += (y + i) % (x + i);
long long full = max(0ll, k - y);
ans += full * (y - x);
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
2260C - Максимизируй XOR, минимизируй операции
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int x, y;
cin >> x >> y;
int s = x + y;
for(int d = (1 << 30); d >= 1; d >>= 1)
if((s & d) != 0 && x >= d)
x -= d;
cout << s << " " << x << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int K = 3;
const int INF = int(1e9);
pair<int, int> get_segment(char c)
{
if(c == '0') return {K, K};
if(c == '-') return {0, K - 1};
return {K + 1, K * 2};
}
void solve()
{
int n;
string s;
cin >> n >> s;
vector<vector<int>> dp(n + 1, vector<int>(2 * K + 1, INF));
dp[0][K] = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < 2 * K + 1; j++)
{
if(dp[i][j] == INF) continue;
auto [l, r] = get_segment(s[i]);
for(int k = l; k <= r; k++)
{
if(k == j) continue;
int& d = dp[i + 1][k];
d = min(d, max(dp[i][j], abs(k - j)));
}
}
int ans = *min_element(dp[n].begin(), dp[n].end());
if(ans == INF) ans = -1;
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
int get(int c00, int c11, int cd)
{
int len = c00 + c11 + cd;
int lf = len / 4;
int rg = 1e8;
int ans = 1e8;
while(rg >= lf)
{
int mid = (lf + rg) / 2;
int need_break = max(0, c00 - mid) + max(0, c11 - mid);
if(need_break + cd / 2 <= mid)
{
ans = mid;
rg = mid - 1;
}
else
lf = mid + 1;
}
return ans;
}
void solve()
{
int n, q;
cin >> n >> q;
string s;
cin >> s;
vector<int> pref1(n + 1), prefdiff(n);
for(int i = 0; i < n; i++)
pref1[i + 1] = pref1[i] + (s[i] - '0');
for(int i = 0; i + 1 < n; i++)
prefdiff[i + 1] = prefdiff[i] + (s[i] != s[i + 1]);
for(int i = 0; i < q; i++)
{
int l, r;
cin >> l >> r;
if(l == r)
cout << 3 << "\n";
else
{
int c1 = pref1[r] - pref1[l - 1];
int cd = prefdiff[r - 1] - prefdiff[l - 1];
if(s[l - 1] != s[r - 1])
cd++;
int c0 = (r - l + 1) - c1;
int c00 = c0 - (cd / 2);
int c11 = c1 - (cd / 2);
cout << get(c00, c11, cd) * 4 - (r - l + 1) << "\n";
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
for(int i = 0; i < t; i++)
solve();
}
2260F - Раскраска рёбер в три цвета
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int N = 3010;
vector<pair<int, int>> g[N];
int p[N];
int pe[N];
int used[N];
int n, m;
vector<int> bad_edges;
int cycle[N];
int xs[N], ys[N];
void dfs1(int x)
{
for(auto [y, i] : g[x])
if(p[y] == -1)
{
p[y] = x;
pe[y] = i;
dfs1(y);
}
else if(p[x] != y)
bad_edges.push_back(i);
}
void go_to_root(int x)
{
while(x != 0)
{
cycle[pe[x]] ^= 1;
x = p[x];
}
}
void dfs2(int x)
{
used[x] = true;
for(auto [y, i] : g[x])
if(cycle[i] == 0 && !used[y])
dfs2(y);
}
void solve()
{
cin >> n >> m;
bad_edges.clear();
for(int i = 0; i < n; i++)
{
g[i].clear();
p[i] = -1;
pe[i] = -1;
}
for(int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
--x;
--y;
xs[i] = x;
ys[i] = y;
g[x].push_back(make_pair(y, i));
g[y].push_back(make_pair(x, i));
}
p[0] = 0;
dfs1(0);
sort(bad_edges.begin(), bad_edges.end());
bad_edges.erase(unique(bad_edges.begin(), bad_edges.end()), bad_edges.end());
int k = bad_edges.size();
for(int mask = 1; mask < (1 << k); mask++)
{
for(int edge = 0; edge < m; edge++) cycle[edge] = 0;
for(int bit = 0; bit < k; bit++)
if((mask >> bit) & 1)
{
go_to_root(xs[bad_edges[bit]]);
go_to_root(ys[bad_edges[bit]]);
cycle[bad_edges[bit]] ^= 1;
}
for(int vertex = 0; vertex < n; vertex++)
used[vertex] = 0;
dfs2(0);
bool good = true;
for(int vertex = 0; vertex < n; vertex++)
if(!used[vertex])
good = false;
if(good)
{
cout << "YES\n";
return;
}
}
cout << "NO\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
2260G - Сортируемые перестановки
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(x < 0) x += MOD;
return x;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
int binpow(int x, int y)
{
int z = 1;
while(y > 0)
{
if(y % 2 == 1) z = mul(z, x);
x = mul(x, x);
y /= 2;
}
return z;
}
int inv(int x)
{
return binpow(x, MOD - 2);
}
const int N = 200043;
int mind[N];
int fact[N];
int ifact[N];
void precalc()
{
for(int i = 2; i < N; i++)
for(int j = i; j < N; j += i)
if(mind[j] == 0)
mind[j] = i;
fact[0] = 1;
for(int i = 1; i < N; i++)
fact[i] = mul(fact[i - 1], i);
ifact[N - 1] = inv(fact[N - 1]);
for(int i = N - 1; i >= 1; i--)
ifact[i - 1] = mul(ifact[i], i);
}
int choose(int n, int k)
{
if(n < 0 || n < k || k < 0) return 0;
return mul(fact[n], mul(ifact[k], ifact[n - k]));
}
int n;
vector<int> factorize(int x)
{
vector<int> res;
while(x != 1)
{
res.push_back(mind[x]);
x /= mind[x];
}
return res;
}
void gen(const vector<int>& d, int i, long long cur, int m, vector<pair<long long, int>>& res)
{
if(i == d.size()) res.push_back(make_pair(cur, m));
else
{
gen(d, i + 1, cur, m, res);
gen(d, i + 1, cur * 1ll * d[i], -m, res);
}
}
void solve()
{
cin >> n;
vector<pair<pair<long long, int>, int>> edges;
for(int i = 1; i < n; i++)
{
auto f1 = factorize(i);
auto f2 = factorize(i + 1);
for(auto x : f2) f1.push_back(x);
sort(f1.begin(), f1.end());
f1.erase(unique(f1.begin(), f1.end()), f1.end());
vector<pair<long long, int>> pos;
gen(f1, 0, 1, 1, pos);
for(auto [x, s] : pos)
{
if(x == 1) continue;
edges.push_back(make_pair(make_pair(x, s), i));
}
}
int ans = 1;
int l = 0;
sort(edges.begin(), edges.end());
while(l < edges.size())
{
int r = l;
while(r < edges.size() && edges[r].first.first == edges[l].first.first)
r++;
vector<int> e;
for(int i = l; i < r; i++)
e.push_back(edges[i].second);
long long i = edges[l].first.first;
int m = edges[l].first.second;
if(m != 0)
{
int absent = n / i;
int cur = mul(fact[n], ifact[n - absent]);
int lf = 0;
while(lf < e.size())
{
int rg = lf;
int nxt = e[lf];
while(rg < e.size() && e[rg] == nxt)
{
rg++;
nxt++;
}
vector<int> cnt(2);
for(int k = e[lf]; k <= e[rg - 1] + 1; k++)
{
if(k % i != 0)
cnt[k % 2]++;
}
//cout << cnt[0] << " " << cnt[1] << endl;
cur = mul(cur, choose(cnt[0] + cnt[1], cnt[0]));
lf = rg;
}
//cout << i << " " << cur << endl;
ans = add(ans, -1 * m * add(cur, -1));
}
l = r;
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
precalc();
int t = 1;
//cin >> t;
for(int i = 0; i < t; i++)
solve();
}









The tutorials for problems will be available in a few minutes.
B felt like a bullet stuck in the ribs, nor does in come out nor was it blocked, felt suffocated!
Tomorrow is my exam—worst decision ever to register!
same here bro...it sucks...
A — easy, B, C literally 1500
My implementation is so poor...
WAed 5 times during contest and got AC simply by changing a few chars.
Submission: 389954784.
I guess I shall improve my implementation skills now, as I got WA on #2 5 times during contest while getting AC only require changing a few chars (my specific implementation was inferior to the official solution that made debugging harder, though). Submission: 389954784.
I wrongly enumerated bitmasks starting from 0, yet another trivial bug in determining connectivity prevented me from detecting it from the samples :(
In my opinion you needed to spend more time with pencil and paper. The solution only requires 50 or so lines of code.(Assuming this was for problem C)
Um, but this is F.
Oh nvm. You did great by the way. The tutorial for F looks like a trip to hell.
disgusting b problem
Solution to E without using binary search 389952685
I was close to solving D , just kept trying to find a arrangement of the prefix that is optimal by considering block interval sizes (if its odd , then it can have 1 , 1 on the ends optimal).
figured prefix would only take value between [-2 , 2] .
Good contest, tho I couldn't solve C. I found the obervation where we need $$$x$$$ $$$AND$$$ $$$y$$$ $$$=$$$ $$$0$$$ but I couldn't do anything further
This is not a competition, but I got to the coding part(though contest ended before I can fix sillies).
It seems that I was able to somehow find all the possible cases and ended up just if-else my solution for D, submission — https://codeforces.me/contest/2260/submission/389959167