Спасибо за участие в раунде! Мы надеемся, что задачи вам понравились. Мы очень старались, готовя этот контест :)
Подсказки по задачам будут опубликованы чуть позже.
2238A - Очередная головоломка от Папируса
Идея: Friendiks
Решение
Tutorial is loading...
Код
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, C;
cin >> n >> C;
int ans = 0;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) cin >> b[i];
bool needReorder = false;
for (int i = 0; i < n; ++i) {
if (a[i] < b[i]) needReorder = true;
ans += a[i];
ans -= b[i];
}
if (needReorder) {
needReorder = false;
ans += C;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for (int i = 0; i < n; ++i) {
if (a[i] < b[i]) {
needReorder = true;
}
}
}
if (needReorder) cout << "-1\n";
else cout << ans << "\n";
}
}
Идея: KotlechkovEgor
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
long long an = 0;
for (int b = 1; b <= n; b++) {
an += 1ll * (n / b) * (n / b);
}
cout << an << '\n';
}
signed main() {
signed t_ = 1;
cin >> t_;
while (t_--) {
solve();
}
}
Идея: lewc
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
vector<int> g[maxn];
int depth[maxn], dp[maxn];
long long ans[maxn];
void dfs(int v, int p = -1) {
ans[v] = 0;
dp[v] = depth[v];
int m1 = depth[v], m2 = depth[v];
for (auto u : g[v]) {
if (u != p) {
depth[u] = depth[v] + 1;
dfs(u, v);
dp[v] = max(dp[v], dp[u]);
ans[v] += ans[u];
if (dp[u] >= m1) {
m2 = m1;
m1 = dp[u];
} else if (dp[u] >= m2) {
m2 = dp[u];
}
}
}
ans[v] += m2 - depth[v] + 1;
}
void solve() {
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int p;
cin >> p;
g[p - 1].push_back(i);
}
dfs(0);
cout << ans[0] << '\n';
for (int i = 0; i < n; ++i) {
g[i].clear();
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Идея: Friendiks
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
int m[maxn], A[maxn], prime[maxn], nxt[maxn];
int main() {
for (int p = 2; p < maxn; ++p) {
if (prime[p] == 0) {
for (int x = p; x < maxn; x += p) {
if (prime[x] == 0) prime[x] = p;
}
}
if (prime[p / prime[p]] == prime[p]) nxt[p] = nxt[p / prime[p]];
else nxt[p] = p / prime[p];
m[p] = m[nxt[p]] + 1;
A[p] = A[p / prime[p]] + 1;
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << m[n] + A[n] - 1 << "\n";
}
return 0;
}
Идея: lewc
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
vector<vector<int>> dp(n + 1, vector<int> (n + 1, 1e9));
dp[0][0] = 0;
for (int i = 0; i < n; ++i) {
vector<vector<int>> ndp(n + 1, vector<int> (n + 1, 1e9));
for (int cnt_f = 0; cnt_f <= i; ++cnt_f) {
for (int cur_s = 0; cur_s <= i; ++cur_s) {
if (s[i] != 'T') {
ndp[cnt_f + 1][cur_s + 1] = min(ndp[cnt_f + 1][cur_s + 1], max(dp[cnt_f][cur_s], cur_s + 1));
}
if (s[i] != 'F') {
ndp[cnt_f][max(0, cur_s - 1)] = min(ndp[cnt_f][max(0, cur_s - 1)], dp[cnt_f][cur_s]);
}
}
}
swap(dp, ndp);
}
int rs = 0;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
rs = max(rs, i - dp[i][j]);
}
}
cout << rs << '\n';
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Идея: KotlechkovEgor
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 2e6 + 1;
const ll mod = 1e9 + 7;
struct md {
ll x;
md(ll p) {
x = p % mod;
}
md operator+(md other) {
int p = x + other.x;
if (p >= mod) {
p -= mod;
}
if (p >= mod) {
p %= mod;
}
return {p};
}
md operator-(md other) {
int p = x - other.x;
if (p < 0) {
p += mod;
}
if (p < 0) {
p = (p % mod + mod) % mod;
}
return {p};
}
md operator*(md other) {
return {1ll * x * other.x % mod};
}
md operator^(int x) {
if (x == 0) {
return {1};
}
if (x % 2 == 0) {
md p = *this ^ (x / 2);
return {p * p};
}
return *this * (*this ^ (x - 1));
}
md operator/(md p) {
return *this * (p ^ (mod - 2));
}
};
vector<md> fact(1, md(1));
vector<md> rfact = fact;
void prec() {
fact.resize(maxn, md(1));
rfact = fact;
for (int i = 1; i < fact.size(); ++i) {
fact[i] = fact[i - 1] * md(i);
}
rfact[maxn - 1] = md(1) / fact[maxn - 1];
for (int i = maxn - 2; i >= 0; --i) {
rfact[i] = rfact[i + 1] * (i + 1);
}
}
md c(int n, int k) {
if (n < 0 || k < 0 || k > n)return md(1);
return fact[n] * rfact[k] * rfact[n - k];
}
md catalan(int n) {
if(n == 0){
return 1;
}
return c(2 * n, n) - c(2 * n, n - 1);
}
void solve() {
ll n, k;
cin >> n >> k;
k++;
vector<ll> bits(61);
for (int i = 0; i < 61; ++i) {
bits[i] = k >> i & 1;
}
for (ll x = 60; x >= n; x--) {
bits[x - 1] += 2 * bits[x];
bits[x] = 0;
}
int sm = 0;
for (int i = 0; i < 61; ++i) {
sm += bits[i];
}
md an = catalan(sm - 1) * fact[sm];
for (int i = 0; i < 61; ++i) {
if (bits[i] >= 2) {
an = an * rfact[bits[i]];
}
}
cout << an.x << '\n';
}
signed main() {
prec();
signed t_ = 1;
cin >> t_;
while (t_--) {
solve();
}
}









Couldn't solve C,D on time, but the problems were great
Thanks for the problems. It was one of the most fun Div. 2 rounds in quite some time.
Somehow D(by greedy logic) got accepted. Here for the expected solution.
Nice contest : )
Problem E was easier than usual, but I think a lot of people would fail the testcase where every character is
T, which results the answer being 1 rather than 0. The given solution also produced the same wrong answer.oops, I forgot to read the part that mentioned choosing an empty range, my bad.
Easier than usual, maybe, but for a slightly more complicated dp problem there are way too many solves in contest.
I guess there's only one explanation for this :/
Somehow D(by greedy logic) got accepted. Here for the expected solution.
Nice contest : )
same solution, I genuinely thought this was the intended soln.
Friendiks
Solution for A,B,C isn't visible, it says loading
UPD: It's finally visible, Thank you!
i think it's a bug on your side (at least for me, everything is ok)
It's visible now, Thanks!
c can be solved with bfs and lca too
first of all for eg there lies a level which is 2 5 6 7
let the parents of 2 and 5 be 3 and parents of 6 and 7 be 4 then
the total number of different lcas you get is how your answer increases from n
for example
lca of 2 and 5 is 3 then lca of 5 and 6 is basically lca of 3 and 4 so let that be 1, then lca of 6 and 7 is 4 so 3 different lcas you got for this level so increase your answer by 3
do this for all levels
this works because naively you can run bfs for every node and count distinct levels, but if you observe then you'll see each level gets merged into one level which is a guild for an ancestor
lewc
I hope I am correct sir
Will not finding lca of every pair of nodes at each level give TLE?
No, I use the same approach and got AC. Submission.
Why only taking lca of adjacent nodes, after sorting by intime?
because let's say you have
3 4 10
2 5 6 7 8 9
now take lca
2 and 5 have lca 3, then 5 and 6 have an lca of 1 which is actually the parent of 3 and 4
so it's like literally merging 2,5 into 1 and 6,7 into 1 level and then finding their lca
then similar for 8 9
in total you get
4 different lcas which are 3 4 10 and 1
hence the answer gets increased by 4
Thanks
https://codeforces.me/contest/2238/submission/380671246
Is there any reason for using set instead of a counter. Can similar lca occur non-consecutively?
I did something similar, but I used DFS, computed depths, and computed LCA for every adjacent node (sorted by DFS entry time) and added it to the answer (the unique ones). But yeah, BFS makes sense.
Yes you can use dfs too and then make levels according to depths? Right?
Yess! Submission Link
I solved (not in time unfortunately) E in $$$O(n^2)$$$. Consider the following subproblem for fixed $$$D$$$: minimize the number of T under the constraint that $$$\sum_{i=l}^r x_i \le D$$$ for all intervals $$$[l, r]$$$. The subproblem can be solved greedily in $$$O(n)$$$ by starting setting all N->F and going left-to-right and maintaining the current prefix sum and maximum prior prefix sum of $$$x_i$$$; each time we violate the constraint, flip the rightmost (before our current position) possible N->F to be N->T. Solve the subproblem for each possible $$$D\in[0,n]$$$.
https://codeforces.me/contest/2238/submission/380524134
Elegant solution! But I wonder how to proof its correctness?
I solved it in $$$O(n^2 \cdot \log^2{n})$$$ XD
380533517
Yet Again D<C
Why contradiction proof for B? Direct proof is more educational and straightforward.
1) By definition of LCM:
and
2) Then:
3) Therefore b divides the left hand side, and so
meaning b divides both a and c, as desired.
a bit more on explanation if someone is looking for it..
let gcd(a,c) = x then by definition x divides a and c and since b also divides x it is indirectly stated that b also divides a and c. :3
better explanation than that of editorial, at least from my point of view.
Great contest! But I feel like cheating seemed much more prominent in today's contest because I didn't expect C to get that many solves to be honest.
Great contest! But I feel like cheating seemed much more prominent in today's contest because I didn't expect C to get that many solves to be honest.
honestly C isnt a hard question by any means, im sure simulating the testcase is enough to get the intuition for it, but still implementation wise I dont expect much people to solve graphs, and seeing my rating dropped much lower than it normal does around the same rank im guessing a lot of lower rated people were able to solve it, this prob means there might have been more cheating involved here
well just after the contest ended, I tried asking gemini some questions in Pro mode and it showed that the model was in high demand lmao, probably not coincidence
B is pretty OEISable, but great set overall!
This was one of the best Div. 2 rounds I've ever done!
Unfortunately didn't have time to solve E or F
Has anyone more clean explanation of task D? I just don't understand
All primes have to be in different set else gcd condition is violated. Now the other sets will be like product of 2 primes, 3 primes goes on till (sum of exponents) primes. These all should form separate groups, u cant put them together as the former will be the divisor of later. So minimum possible no of sets will be no of primes + (sum of exponents) — 1 (cause we already considered the primes(product of 1 prime))
good explanation, you can check my solution (sort of dp) also and please give upvote if u liked
For every prime factor all of it's factors would have to go in different layers Consider 120,
So 2 2×3×5 2^2×3×5 2^3×3×5 All in different buckets Similarly 3 3×5 And 5 Now any remaining factor can be put in one of these layers
You can simulate this it is similar to sieve
oh during contest i forgot that i can just simulate. But now that's not enough and i wanna understand proof
for 120 , answer is 7 layer , so please give me 7 layer , which layer contain which divisor, i try lots of time but failed to manage it in 7 layer.
`````````
include <bits/stdc++.h>
include
using namespace std;
using ll = long long int; const ll mod = 998244353;
ll dp[1000000+1]{0}; ll prime [1000000 + 1]; vector<vector> factors(1e6+1); void solv(){ ll n ; cin>>n; // for(const auto & x : factors[n]) cout << x <<" ";cout<<endl; cout << dp[n] <<endl; // cout<<endl; } int main (){ cin.tie(0); ios_base::sync_with_stdio(0); dp[1] = 0; for(int i = 0 ; i <= 1e6;i++) prime[i] = 1; for(int i = 2; i<=1e6;i++){ if( prime[i] == 0){ continue; } for(int j = 2*i; j <=1e6;j+=i){ prime[j] = 0; factors[j].push_back(i); } } // cout<<prime[2] <<endl; for(int i = 2; i<= 1e6;i++){ if( prime[i]){ // prime no dp[i] = 1; continue; } ll ans = 0; for(const auto & x : factors [i ] ){ ll otherno = i/x; if(otherno%x)ans = max(ans , 1 + 1 + dp[i/x]); else ans = max(ans , 1+dp[i/x]); } dp[i] = ans; } // for(int i = 1;i<=10;i++) cout << // cout <<dp[2] <<endl; ll t ; cin>>t; while(t--){ solv(); } }
````````
DP Solution for D, if you are comfortable with DP you can ask for proof from GPT
edit i got to now dp[i] = 2 + dp[i/x] if x is only one in primefactor of n else dp[i] = 1 + dp[i/x] and we can take any prime x that is factor of i; so max is just useless as dp is just cool think
I had the wrong idea implementing this, and somehow ran into a accepted solution.
Math-heavy questions always feel charming. Although I overcomplicated B
I know B is possible in $$$O(\sqrt{n})$$$ with square root algorithm combining or using blocks of equal values, but is there a better time complexity obtainable?
Hello Codeforces, I know I have low rating and I was unable to solve B in the contest. But here is my approach to proving the idea behind B, I feel this approach is less arbitrary and easier to come up with, please let me know if there are any flaws in my reasoning: gcd(lcm(a, b), lcm(b, c)) = gcd(a, c)
.
Funnily enough, $$$O(t \sqrt{n})$$$ is allowed for D
Nice contest! Problem F is really cool.
This assertion is not trivial. I cannot prove it strictly in contest. It is intuitively correct, though.
I agree. Here's how I proved it in round:
Suppose we're doing the strategy where we place all numbers with $$$2^x 3^y 5^z$$$ where $$$x + y + z = L$$$ for level $$$L$$$ and $$$L \gt = 2$$$. (I'm using 2, 3, 5, but this works WLOG for any primes)
The abstract strategy is to put (pure powers of 2), (at least one number containing both 2 and 3), (pure powers of 3), (at least one number containing both 3 and 5), (pure powers of 5). If a number fits into multiple categories then we can file it under any of them, e.g. it doesn't hurt to put $$$3^2 5^1$$$ under "pure powers of 3".Core idea: if $$$2^a$$$ for any value $$$a$$$ exists at this level, then $$$a = L$$$ trivially, and so must $$$2^{a-1} 3^1$$$. This is true as long as $$$a \ge 2$$$, which is true as long as $$$L \ge 2$$$.If there is no $$$2^a$$$, then there does not need to be a $$$2^b 3^c$$$ and we can go on to $$$3$$$. If there is a pure power of 3, then $$$3 \cdot 5$$$ is guaranteed to exist, and so on.Unrelated but I feel like D is a bit easy to cheese if you guess and don't prove, I wonder if it would be harder if we had to give a valid constructionEdit: Actually I don't think this is fully rigorous. Maybe a better way is
Suppose again $$$2^x 3^y 5^z$$$ where $$$x + y + z = L$$$. Then:
I think this one is much more rigorous and easy to implement.
I had a similar idea, but instead worked from the other direction, by grouping divisors of $$$n$$$ by two properties:
Then we output the groups in the following order:
Clearly these sets form a partition of all the divisors of $$$n$$$, and within each group all values share the highest factor. When the number of factors is at least 2, then while some of the early sets might be empty, when one set is nonempty, all subsequent sets are nonempty too. This provides the necessary connection between sets.
I came up with this inductive construction
Base case: With only one distinct prime, every Ω-layer has at most one element, so the claim is trivial.
Induction hypothesis: Assume for
m-1distinct primes, every Ω-layer can be arranged such that adjacent elements havegcd > 1, and the last element of every Ω-layer contains the newest prime.Now add a new prime
p.p.pis uniquely obtained by multiplying every element from Ω = k-1 layer from previous instance byp. This gives exactly all new elements of the Ω = k layer.gcd > 1.Hence every Ω-layer can be constructed for
mprimes, completing the induction.My post contest discussion stream here and hints here
guys im getting really demotivated my rating isnt improving. How should I grind? I used to solve 2 now idk if i have lost skill or smthing
great contest!
For C, there is a better way to solve the question, which looks cleaner on the implementation side.
Instead of tracking absolute depths from the root and subtracting them later, the bottom-up DFS can directly return the maximum relative height of the subtree. Since every node contributes 1 to the answer we can initialize our answer from n. During the single DFS traversal, we can directly accumulate the unique guilds formed by the branching channels by adding the 2nd largest child height to the answer. Also to add on the official answer used 4 arrays, but it can be solved only using one.
you can check my submission here Submission
Yeah I did this way and I think it is more intuitive than any crazy data structure solution
380484282
Nicee.
Very intuitive solution, thanks!
You're welcome
I had done it without using dfs. Had to use 3 arrays one for finding the largest height of the child for each node, one for finding the second largest height and another one just to find the height of each node. Now we can see that for each node i; to find the second largest height of subtree the parents of the nodes<i are irrelevent as for the parent of a node i has to be less than i. So considering this it is just required to run a for loop from n-1 to 0, initialising the required value to 0 and every time adding the second largest child height + 2(one for the child and one for the node itself). Now it is just required to put a condition to check if the height of the present node is greater than the largest or second largest child's height w.r.t the parent of the present node and based on that make changes to the first and second arrays.
Great, I've seen your code. Your logic is also marvelous
All problems are interesting!
Here’s an explanation for D which may be easier to understand:
First, find the prime factorisation of $$$n=p_1^{\alpha_1}\dots p_m^{\alpha_m}$$$
For example, let’s say $$$n=2^5\cdot 3^5 \cdot 5^5$$$.
Consider the divisor chain $$$[2], [2^2],\dots, [2^5],[2^5\cdot 3],[2^5\cdot 3^2]\dots [2^5\cdot 3^5\cdot 5^5]$$$
Since each divisor $$$d_i$$$ is a proper divisor of all other divisors $$$d_j$$$ for $$$j \gt i$$$, they each have to be in their separate layers.
Furthermore, all primes must be in their own layer, for which there are $$$m$$$ of them.
So $$$n$$$ must at least be in layer $$$A+m-1$$$, where $$$A=\sum_{i=1}^m \alpha_i$$$. We subtract 1 since we already considered $$$2$$$ as a layer so we don’t double count it when counting the number of primes.
Then to prove that these layers created are sufficient to fill all other divisors, consider the condition that no two numbers in a layer can be proper divisors of each other.
An easy way to avoid adding a number which divides some other number already in the layer, is to remove a prime factor and add another.
For example, in the layer with $$$[2^5\cdot 3^2]$$$, an easy way to add a divisor is to remove one multiple of 2, and add a multiple of 3 or 5. We will never add a proper divisor since a proper divisor must have a multiplicity (i.e. its value of $$$\Omega(x)$$$) strictly less than the current layer.
Note that we can’t only add or only remove prime factors, since that might end up adding a multiple or divisor some number in the layer. But for numbers with the same value of $$$\Omega(n)$$$, they will never be proper divisors of one another.
for 120 , answer is 7 layer , so please give me 7 layer , which layer contain which divisor, i try lots of time but failed to manage it in 7 layer.
My Solution for Problem B. And I think its unique. no one has commented it yet.
Firstly lets change the equation by a bit.
$$$lcm(a,b)$$$ and $$$lcm(b,c)$$$ must contain all factors of $$$b$$$, so their gcd certainly contains $$$b$$$.
Beyond $$$b$$$, the only extra prime powers that can appear in both numbers are those common to both $$$a$$$ and $$$c$$$. So we can say that $$$gcd(lcm(a,b),lcm(b,c)) = lcm(b,gcd(a,c))$$$. So now the equation becomes $$$lcm(b,gcd(a,c)) = gcd(a,c)$$$.
The equation will only hold when $$$b$$$ is a divisor of $$$gcd(a,c)$$$.
Now, to solve this problem, we can either fix $$$b$$$ or $$$gcd(a,c)$$$. I fixed $$$gcd(a,c)$$$. As $$$a,c \leq n$$$, we have $$$1 \leq gcd(a,c) \leq n$$$. Let $$$gcd(a,c)=x$$$. (We will iterate over all $$$x$$$, where $$$1 \leq x \leq n$$$.) Now we need to count the number of pairs $$$(a,c)$$$ such that $$$gcd(a,c)=x$$$.
Since both $$$a$$$ and $$$c$$$ are divisible by $$$x$$$, write $$$a=xi$$$ and $$$c=xj$$$.
Then $$$gcd(a,c)=x$$$ if and only if $$$gcd(i,j)=1$$$.
So, for every $$$x$$$, our task reduces to counting the number of coprime pairs $$$(i,j)$$$ with $$$i,j \leq \lfloor n/x \rfloor$$$.
This can be computed efficiently using the Euler Totient Function. We precompute all totient values beforehand to answer each $$$x$$$ quickly. Now let the number of such pairs be $$$y$$$.Also, let $$$d$$$ be the number of divisors of $$$x$$$.
Since $$$b$$$ must be a divisor of $$$gcd(a,c)=x$$$, there are exactly $$$d$$$ possible choices for $$$b$$$. Therefore, for a fixed value of $$$gcd(a,c)=x$$$, the contribution to the answer is $$$y \times d$$$.
good round, i solve D faster then B
I solved A by myself like solution,but could't know why:(