Блог пользователя Bazoka13

Автор Bazoka13, история, 3 года назад, По-английски
Tutorial is loading...
solution
Tutorial is loading...
solution
Tutorial is loading...
solution
Tutorial is loading...
solution
Tutorial is loading...
solution
Tutorial is loading...
solution
Разбор задач Codeforces Round 738 (Div. 2)
  • Проголосовать: нравится
  • +215
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +52 Проголосовать: не нравится

Am i only here who was too lazy to think about better solution at D1 than just writing full DSU?

»
3 года назад, # |
  Проголосовать: нравится +38 Проголосовать: не нравится

This was a lovely round. Thanks a lot to the problem setters (◍•ᴗ•◍)❤.

»
3 года назад, # |
  Проголосовать: нравится +74 Проголосовать: не нравится

"Mocha and Math" problem editorial:

So we can set x = 0 initially. Then we iterate over the sequence and make x = x & a_i, the x is the anwser finally.

Well, if we set x = 0 initially, the final result will always be 0.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +44 Проголосовать: не нравится
»
3 года назад, # |
  Проголосовать: нравится +352 Проголосовать: не нравится

I have a different approach to problem D2, which is simpler in my opinion.

First, try to add all edges $$$(1, x)$$$ for each $$$x$$$, after that, all nodes are in the component of node $$$1$$$ in at least one of the two trees.

If they are in the same component of node $$$1$$$ in both trees, we won't add edges from that node, since all nodes are in the same component than it in at least one tree.

Now we will consider nodes of two types, the ones that are in the same component than $$$1$$$ in the first tree, and the ones that are in the same component than $$$1$$$ in the second tree. We will store all nodes of type 1 in a stack $$$p1$$$, and all nodes of type $$$2$$$ in a stack $$$p2$$$, and we will try to match them with the following algorithm.

  • If the top of $$$p1$$$ is in the same component than $$$1$$$ in both trees, delete it

  • If the top of $$$p2$$$ is in the same component than $$$1$$$ in both trees, delete it

  • Otherwise, add an edge between the top of $$$p1$$$ and the top of $$$p2$$$.

Is possible to show that this algorithm will add the same number of edges that the one explained in D1's editorial.

The complexity is almost $$$O(n+m)$$$, since the solution only uses two DSU's, and stacks.

My implementation

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +21 Проголосовать: не нравится

    My Solution is almost the same as yours, except I just maintain two pointers $$$p1,p2$$$: $$$p1$$$ is the smallest index in the same component as $$$1$$$ in first forest but not in second forest; $$$p2$$$ is the smallest index in the same component as $$$1$$$ in second forest but not in first forest. Keep adding edges $$$(p1,p2)$$$ until no edges can be added.

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +18 Проголосовать: не нравится

      Thanks for this approach, but one more insight will clear it further,

      When we have greedily added all possible edges with 1, now it is sure that if 2 nodes aren't connected in the final forest till now, then either of them must already connected to 1 in the separate graphs (126097226 line 162).

      This translates to fewer calculations (and clean code) in the last while loop.
  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +6 Проголосовать: не нравится

    May I put and translate this lovely solution in my blog?

    I'll certainly leave the link.

  • »
    »
    3 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Hey humbertoyusta, I din quite get how the stack part works... Could you pls explain it in detail ? i.e. how does taking all the elements in mocha's tree which does not belong to the same component as 1 and then randomly selecting a node from diana's forest which also dont belong to the same component as 1, works ? i.e. basically i believe even if we randomly permute the elements within each stack, it will work. Could you elaborate this part ?

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +7 Проголосовать: не нравится

      You just need to try to match all the nodes who are in the same component than node 1 in the first tree, with the nodes who are in the same component than node 1 in the second tree.

      You can do it in any order, due to the proof in D1's editorial.

      So you just delete nodes who are in the same component than 1 in both trees(since is impossible to add an edge from them), and try to match the rest.

      The way I implemented it, was maintaining two stacks, and trying to add an edge between the tops of the stacks is possible, otherwise delete the tops who are in the same component than 1 in both trees, for more detail you can check my implementation in the comment above.

  • »
    »
    3 года назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

    "First, try to add all edges (1,x) for each x, after that, all nodes are in the component of node 1 in at least one of the two trees."

    In the first graph: 1 -> 3 , 3 -> 2 and 4 is alone

    In the second graph: 1 -> 4 , 4 -> 2 and 3 is alone

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Node 2 is in the same component than 1 in both trees, node 3 is in the same component than 1 in first tree, and node 4 is in the same component than 1 in second tree.

      In this case you just don't add any edge in this step since condition of every node is in the same component than 1 in at least one tree is already true.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can we solve C using a Topological sort? Tried implementing it but didn't succeed

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    How would you deal with cycles?

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I think we can keep track of the visited nodes and skip them if they've already been visited

      • »
        »
        »
        »
        3 года назад, # ^ |
        Rev. 3   Проголосовать: нравится +1 Проголосовать: не нравится

        What if a node can be visited using 2 different paths?

        If you want to use topo sort, I think, we need to apply DFS for each and every node one by one...

        Like for(node 1->node n)DFS(node)

        Correct me If I am wrong.

        Edit: It is possible to do it using topo sort... my bad

        Tarun_19 solved it.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    Yes, we can
»
3 года назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится

C was essentially asking for you to print out a Hamiltonian path in a special directed graph. In contest I remembered about a blog which has a heuristic algorithm for detecting Hamiltonian paths. Not sure if its even supposed to pass....

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

Slight mistake in A : we need to set x = a[0] rather than 0 .

»
3 года назад, # |
Rev. 3   Проголосовать: нравится +10 Проголосовать: не нравится

Accidentally got AC on D2 and couldn't find any similar solution 126013984

Q1: Is there any test that can turn AC to TL?
Q2: If not, can you give me some explanation (proof) why a and b is small enough?

Explanation:
1) I'm trying to connect all components with the component which contains vertex 0 2) For some vertices we obviously cannot do it. For example in test case 3 we get only 4 edges instead of 5
3) If we look closely on the vertices that we couldn't connect with vertex 0 there is a similarity between them. Both of them are separate components (Perhaps I unclearly express what I want to say. You can uncomment debug in my submission and look at DSUnions in the end of step 1 of explanation)
4) If a = count of such vertices in graph 1 and b = same for the 2nd graph. We can try to connect them for O(a * b)

I supposed that a * b is small that's why I submitted it.

This solution got 1.5s which is close to time limit which means that a * b is not as small as I expected.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +65 Проголосовать: не нравится

I solve D with bitset .

you can save a bitset for each component , which is the set of elements which is not in this component .

when merge two components , you can simply "AND" two bitsets .

But the memory is $$$O(n^2/w)$$$ . You can do a simple optimization :

  • if the number of elements in the component is $$$\leq B$$$ , just use a vector to save all the elements .

  • otherwise , use a bitset .

Then you can consider 1,2,3...n , and check whether you can add an edge connecting i and a vertex not in the component .

So the memory is $$$O(n*n/(w*B)+n)$$$.

And the time complexity is $$$O(n*n/w+n*n/B+n*B)$$$,you can let B = $$$\sqrt n$$$ ,then it can pass.

solution : 125997167

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    If there is a valid vertex to join, then after AND operation there must be several bits left. How did you know at what positions those bits are?

»
3 года назад, # |
  Проголосовать: нравится -38 Проголосовать: не нравится

What's the point in making 256MB memory limit in a space complexity $$$O(n\log n)$$$ problem? It's really confusing. I was afraid of MLE on system tests so I resubmitted, losing rank 1 :(

Also I don't understand why E is even not rejected. It's "yet another trivial and straightforward gcd counting problem".

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +82 Проголосовать: не нравится

    Sorry for trouble caused.

    In problem D2, we think that the space complexity $$$O(n\log n)$$$ isn't that large when $$$n\le 10^5$$$. My submission only uses 26700KB and among all the testers the largest memory usage is 89200KB. So we just set a standard 256MB memory limit.

    As for problem E, we didn't expect that it would be a trivial problem. That's why we say 'Sorry for my mistake in estimating the difficulty of problem D2 and E.' in the previous blog.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +100 Проголосовать: не нравится

As the queue is too long, I didn't know I got a WA on E until the contest finished.

:(
»
3 года назад, # |
Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

It's so sad that I got everything in E right except the last part. I didn't compute i from 1 to M but only the factors of M (cuz of some wrong thoughts when I first read the problem I saw the sum must be M instead of sum not greater than M) and I got wrong answer on sample test 3. I thought the complexity is gonna be $$$O(nM^2)$$$ if I loop i from 1 to M and forgot that for high values of i the complexity of the dp will also decrease. I can't think of solution for D2 so I skipped D1 as well to do E since I have rough idea of E but in the end I failed to complete both D and E...

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

A slightly alternative solution for E.

We can already count the dynamics from the editorial , but we will slightly change the calculation of the answer. Let's calculate for each $$$i$$$ $$$(1 \leq i \leq m)$$$ how many different primes there are in its decomposition, and is it true that each prime number is included in the power of $$$1$$$, if not, then we will not take it into account. Then if the number includes an even number of primes, then we will take it in the answer with a plus sign, otherwise with a minus sign.

Well, it works, since we just wrote the formula for inclusions of exceptions

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

excellent Round,the statements are clear and the problems are interesting.

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

my first contest in cf. Really enjoyed it even only worked out A — D1. Thanks very much for the excellent problems and answers!

»
3 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

Thank You so much, from the last two questions, I will learn something new.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

IN Mocha and math how could i have observed that i had to iterate over all the array to minimize the maximum value??? can someone explain me.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone please explain me why can't we do min_element & max_element to get the ans in problem A ?

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Problem E:

We can compute it in O(nm) by Knapsack DP optimized by prefix-sums.

Bazoka13 can you please explain how prefix-sum is used here?

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    suppose you have calculated dp[i][j] for 0<=j<=m

    dp[i+1][j] = dp[i][j] + dp[i][j-1] .... dp[i][j-cap[i+1]];

    here cap[i+1] is upper bound on variable i+1

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +21 Проголосовать: не нравится

    suppose you have following task: given array $$$a$$$, and you want to do fast multiple times following action: increase value by $$$x$$$ all $$$a_i$$$ within range $$$[l, r]$$$. And after you did all those actions, you need to output resulting array $$$a$$$. This is what you need to do for this knapsack problem. dp[i][j] will store how many arrays of length $$$i$$$ have sum $$$j$$$. And, from array of length $$$i$$$ with sum $$$j$$$ you can do arrays of length $$$i+1$$$ with sums $$$[j+l, j+r]$$$. This range is where you need to add variants. And you increase them by same amount $$$dp[i][j]$$$. So, if you can increase values within range fast, you can solve this. Idea is that instead of actual values we will store array of deltas. To be precise, we will store certain array such that if you make prefix sums array from it, you'll get actual values of dp. For example, instead of storing 1,2,3,2 — values, we will store 1,1,1,-1 — deltas. Because we can calculate prefix sums array, and get 1,1+1,1+1+1,1+1+1-1 = 1,2,3,2. Now, if you increase single element of this array, prefix sums array will change all values in suffix. For example 1 2 1 -1 will give 1 3 4 3. So you can increase all values in suffix. Now you can increase within $$$[l, r]$$$ range by increasing value at $$$l$$$ so you'll increase within range $$$[l, \infty)$$$ and decrease value at $$$r+1$$$ so you'll also decrease within range $$$[r, \infty)$$$ which results into increase only within range $$$[l, r]$$$. Once all operations done, you calculate actual values by simply calculation of prefix sums, and use it as next dp[i].

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Wow Wow Wow, what a great explanation, I know the prefix sum delta trick but I wasn't able to figure out how the dp works, you made it very very clear. Again Thanks A lot.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Since the constraints for problem B is quite small, I have another approach in $$$O(N^2)$$$ which seems more straightforward to me.

For every letter that is not a $$$\tt{?}$$$, I check whether the letter next to it is colored or not. If it isn’t colored yet, I simply paint it the opposite color of the current one. For example with the string $$$\tt{?R?}$$$, the left and the right letter is painted $$$\tt{B}$$$.

If something like $$$\tt{B?R}$$$ occurs, painting $$$\tt{?}$$$ either $$$\tt{R}$$$ or $$$\tt{B}$$$ results in the imperfectness value equal to 2, so this guarantees to be the optimal solution even if I start at $$$\tt{B}$$$ or at $$$\tt{R}$$$. All to do left is to repeat that process until there is no $$$\tt{?}$$$ left in the string.

»
3 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

♂That's amazing♂ contest! Enjoyed it!

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Nvm found my mistake.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

In fact, Mobius function is not necessary to be used in E, as it can be solved by inclusion-exclusion principle which is similar to the original solution and easier to understand.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

[Problem D1] I dont understand why if we connect random two edge will get the best result.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In the E's editorial It's mentioned that We can compute it(the number of integers satisfying only the two conditions as mentioned) in O(nM) by Knapsack DP optimized by prefix-sums. I am unable to figure this part. Can anyone help?

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    We want to compute the number of arrays with gcd dividing $$$g$$$. Therefore, we are only allowed to have numbers $$$i\cdot g$$$, for $$$1 \le i \le \lfloor \frac{m}{g} \rfloor$$$. The sum of numbers in the array is also a multiple of $$$g$$$. From each range $$$[L, R]$$$ we are only allowed to have numbers $$$i \cdot g$$$, where $$$\lceil \frac{L}{g} \rceil \le i \le \lfloor \frac{R}{g} \rfloor$$$. Therefore, each segment $$$[L, R]$$$ can be transformed into a segment $$$[\lceil \frac{L}{g} \rceil, \lfloor \frac{R}{g} \rfloor ]$$$. Consider $$$knapsack[k][j]$$$ — the number of ways to get sum $$$j \cdot g$$$ from the first $$$k$$$ elements. When we add a new element $$$a_{i+1}$$$ with the possible range $$$[\lceil \frac{L}{g} \rceil, \lfloor \frac{R}{g} \rfloor ]$$$, we can see that the state $$$knapsack[i][j]$$$ contributes to states starting from $$$knapsack[i+1][j + \lceil \frac{L}{g} \rceil]$$$ and ending with the $$$knapsack[i+1][j + \lfloor \frac{R}{g} \rfloor]$$$, so we need to add value $$$knapsack[i][j]$$$ to some segment of $$$knapsack[i+1]$$$. We can do this with the difference array technique (see https://codeforces.me/blog/entry/88474?locale=en ). So we compute $$$knapsack[i+1]$$$ as a difference array, and then at the next step we compute prefix sums of this array to get the actual values of $$$knapsack[i+1]$$$ and proceed to computing $$$knapsack[i+2]$$$. Note that, as always, we can only store two layers. See my code for more details: https://codeforces.me/contest/1559/submission/126027569

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Another approach for the knapsack DP part is to treat it as a problem of multiplying polynomials. For the case where we want sequences of gcd >= 1 the polynomial of the interval $$$[l_{i}, r_{i}]$$$ is $$$x^{l_{i}} + x^{l_{i}+1} + \ldots + x^{r_{i}}$$$. To get the number of sequences where the gcd>=1 and the sum <= m you multiply all these polynomials and sum the coefficients of the terms for powers <= $$$x^{m}$$$. You can try doing this with NTT but it's too slow. The key observation is $$$x^{l_{i}} + x^{l_{i}+1} + \ldots + x^{r_{i}} = \frac{x^{l_{i}}-x^{r_{i}+1}}{1-x}$$$ This means if you have the current polynomial $$$p(x)$$$ and you want multiply by the polynomial for $$$[l_{i}, r_{i}]$$$ you can do it in $$$O(m)$$$ by multiplying by $$$x^{l_{i}}-x^{r_{i}+1}$$$ then dividing by $$$1-x$$$. The case where you want gcd>=g is the same but you replace the interval $$$[l_{i}, r_{i}]$$$ with $$$[\lceil l_{i}/g \rceil, \lfloor r_{i}/g \rfloor]$$$ and $$$m$$$ with $$$\lfloor m/g \rfloor$$$. 126171468

»
3 года назад, # |
  Проголосовать: нравится +21 Проголосовать: не нравится

Any proof in D1 that this is sufficient ? merging each possible two trees when they're not already merged in the other graph (Diana's) how can we be sure that working like this greedily will not affect the next step badly ?

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    You always have the option to connect two nodes if none of the two forests is a tree. The proof is in the tutorial:

    Tutorial

    So if you can't find two nodes to merge, that means you achieved the optimal answer.

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      So for like two trees A,B... no matter how many ways there are to merge them their case is totally independent from everything will happen in later steps in other trees but why exactly ? the tutorial doesn't explain this point clearly

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится +3 Проголосовать: не нравится

        Let say $$$a$$$ and $$$b$$$ is number of components in corresponding forests. Each time you add edge, number of components in both forest decrease by one, no matter which edge you pick. So, after first edge, number of components is $$$a-1$$$ and $$$b-1$$$, after second edge $$$a-2$$$ and $$$b-2$$$. Then after you add $$$min(a, b) - 1$$$ edges you will get tree.

      • »
        »
        »
        »
        3 года назад, # ^ |
        Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

        Although which edge you pick may affect later options, it won't make your answer worse.

        The tutorial proved that if you can't find an edge to add, one of the forests is always a tree. Obviously, if one forest becomes a tree, it is the optimal answer.

        So you can greedily add any edge you can add until you can't add any more. And when you can't add an edge, you achieved the optimal answer.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +22 Проголосовать: не нравится

Alternative solution for 1559D2 - Mocha and Diana (Hard Version).

wall of text
»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Should this submission for 1559D2 - Mocha and Diana (Hard Version) be accepted? 126011074

What it does is similar to choosing one component in each graph randomly and connecting them if possible each time, and repeating the process until it reaches 1.8s.

After changing it to trying for only 2n times, it also passes the tests. 126093360

If the random part works well, is this a submission that can be hacked or a good solution that proves to be correct most of the time? (I think maybe it's a good solution because of m << 100000^2)

»
3 года назад, # |
  Проголосовать: нравится +27 Проголосовать: не нравится

Even simpler solution for D:

If vertices $$$u_1$$$ and $$$v_1$$$ belong to different trees in Mocha's forest and vertices $$$u_2$$$ and $$$v_2$$$ belong to different trees in Diana's forest then one of 6 edges between $$$u_1, v_1, u_2, v_2$$$ can be added. It's pretty easy to prove and we also don't even care if some of them are equal. Now just keep a vertex for every tree in both forests and do this until you only have one tree in one of the forests.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Could you explain your solution a bit more? Obviously adding edges in u1 to u2/v2 i.e. across Diana's and Mocha's forest doesn't make sense.

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +13 Проголосовать: не нравится

      It does!

      If $$$u_1$$$ and $$$v_1$$$ belong to the same tree in Diana's forest (otherwise pair $$$(u_1, v_1)$$$ works) then either $$$u_2$$$ or $$$v_2$$$ is from a different tree from these two. Let's say it's $$$v_2$$$. Then both pairs $$$(u_1, v_2)$$$ and $$$(v_1, v_2)$$$ are from different trees in Diana's forest and at least one of them is from different trees in Mocha's forest as well since $$$v_2$$$ is the same in both pairs but $$$u_1$$$ and $$$v_1$$$ are from different trees.

  • »
    »
    9 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится


    //////////////////////////////////////// #include<bits/stdc++.h> /// using namespace std; /// #define ll long long int /// #define ull unsigned long long int /// #define vell vector<ll> /// const ll MOD = 1e9 + 7; /// void init_io() { /// ios_base::sync_with_stdio(false); /// cin.tie(nullptr); /// } /// //////////////////////////////////////// const int N=2e5+10; vector<int>graph1[N]; vector<int>graph2[N]; bool vis1[N]; bool vis2[N]; void dfs1(int vertex){ vis1[vertex]=1; for(int child:graph1[vertex]){ if(!vis1[child]) dfs1(child); } } void dfs2(int vertex){ vis2[vertex]=1; for(int child:graph2[vertex]){ if(!vis2[child]) dfs2(child); } } void invictus(){ ll n,e1,e2; cin>>n>>e1>>e2; for(int i=0;i<e1;i++){ ll v,u; cin>>v>>u; graph1[v].push_back(u); graph1[u].push_back(v); } for(int i=0;i<e2;i++){ ll v,u; cin>>v>>u; graph2[v].push_back(u); graph2[u].push_back(v); } dfs1(1);dfs2(1); vector<pair<ll,ll>> ans; for(int i=1;i<=n;i++){ if(vis1[i]==0 && vis2[i]==0){ dfs1(i),dfs2(i); ans.push_back({1,i}); } } set<ll>un,co; for(int i=1;i<=n;i++){ if(vis1[i]==0 && vis2[i]==1) un.insert(i); if(vis1[i]==1 && vis2[i]==0) co.insert(i); } auto it1=un.begin(); auto it2=co.begin(); while(it1!=un.end() && it2!=co.end()){ ll x=*it1,y=*it2; ans.push_back({x,y}); it1++; it2++; } cout<<ans.size()<<endl; for(int i=0;i<ans.size();i++) cout<<ans[i].first<<" "<<ans[i].second<<endl; } //////////////////////////////////////// int main(){ /// init_io(); /// // ll t;cin>>t; /// // while(t--) invictus(); /// } /// ////////////////////////////////////////

    this is my solution for problem D

    1) i'm doing dfs starting with the vertex 1 and marking the visited nodes (doing this for both the forest).

    2) then i'm checking vertex which is not visited in both the forest then im doing dfs for that vertex in both forest and adding an edge between that vertex and 1.

    3) last step im cheking for vertex which is visited in one of the forest and not visited in another forest for ex:- 5 is having edge in forest 1 and not in forest 2, 7 is having edge in forest 2 and not in forest 1 so i will add a edge between them

    idk what's wrong in this logic please check the code

»
3 года назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

I have better E solution. It is pretty similar to editorial, but it doesn't use Möbius function and easier to understand.

Let $$$f(k)$$$ be number of sets of integers, which are bounded($$$l_i \leqslant a_i \leqslant r_i$$$ for all $$$i$$$) and sum of $$$a_i$$$ doesn't exceed $$$M$$$ and also for all $$$i$$$, $$$a_i$$$ must be divisible by $$$k$$$. It can be calculated with DP for $$$O\left(n\frac{M}{k}\right)$$$ time and $$$O\left(\frac{M}{k}\right)$$$ space as in editorial.

So calculating $$$f(i)$$$ for all $$$1 \leqslant i \leqslant m$$$ will take $$$O(nM \log M)$$$ time.

Let $$$d(k)$$$ be defined like $$$f(k)$$$, but instead of condition "for all $$$i$$$, $$$k \mid a_i$$$" we use condition "gcd of all numbers is $$$k$$$". Thus by definition, answer is $$$d(1)$$$.

Also it is clear, that $$$d(i) = f(i) - (d(2i) + d(3i) + \ldots)$$$, hence all $$$d(i)$$$ can be calculated by this formula for $$$O(M \log M$$$) because of harmonic series.

Submission using this solution: 126092551

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

In problem D2, can someone explain the this line about the time complexity," This is because an element moves to a new row/column O(logn) times and each move is O(logn) time (using STL set in cpp)" with respect to the operations that are happening. I understood the operations above but not able to figure out the time complexity. To me, it seems more like O(logn)+O(logn).

  • »
    »
    3 года назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

    I think , in a row there can be at most n elements to merge, and each element is merged logn times max, as rows are merged by size (DSU by rank logic) and logn to insert in set each time. so for 1 element (logn)^2 , for n elements n*(logn)^2.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Unable to understand the code given in D1 can anyone help ??

  • »
    »
    3 года назад, # ^ |
    Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

    To detect whether connecting two vertices is possible (there will be no cycle after connection) we can use DSU (disjoint set union, tutorial). Now we assume that for each vertex A and B we know whether we can connect them or not. Just try to brutforce all pairs of vectices. If connection of two vectices in both forests wouldn`t create a cycle add new edge. Example of code: 126156817. Hope this will help you. Good luck :)

»
3 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

Can someone please explain why $$$\sum_{d|gcd(a_1,a_2,...,a_n)}\mu(d) = \sum_{d|a_1,d|a_2,...,d|a_n}\mu(d)$$$ ? Couldn't wrap around my monke brain :(

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    d is a divisor of $$$gcd(a_1,a_2,...,a_n)$$$, so d is also a divisor of $$$a_1$$$ and $$$a_2$$$ ...

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      But the righthand also includes divisors that aren't divisors of $$$gcd(a_1,a_2,...,a_n)$$$, do they cancel each other out?

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone help me explain this from problem Bs[i]=s[i-1]^('B'^'R'); `` What does 'B'^'R' return?? ouput of 'B'^'R' is not showing when I run it in c++.

  • »
    »
    3 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    s[i]=s[i-1]^('B'^'R') basically turns s[i] depending upon the value of s[i-1]. If s[i-1] is 'B', s[i] changes to 'R' and if s[i-1] is 'R', s[i] changes to 'B'.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    'B', 'R' in C++ is number equal to ASCII code of symbol. It basically means that char is also integer in C++, it just has fewer bytes and narrow range. You should also know that (x^y)^z = x^(y^z). This is basic property of XOR. It's easy to prove by considering bits separately. Then, feed 'B' into formula you get 'B'^('B'^'R') = ('B'^'B')^'R' = 0^'R' = 'R'. Also, you should know that x^y = y^x, so if you feed 'R' into formula you get 'R'^('B'^'R') = ('B'^'R')^'R' = 'B'^('R'^'R')='B'^0='B'

    • »
      »
      »
      3 года назад, # ^ |
      Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

      Hello, can you tell me how ('B'^'B') is zero ? and why there is the need of two for loops(one from either side of string) to insert 'B' or 'R' ? Running the loop from one side will fill all elements except first char(when the first char is '?') ?

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        As I said, in C++ 'B' is number, to be precise it's 66. 'B'^'B' literally means 66^66, which is obviously 0. Two loops need in case ????B????, after first case we'll get ????BRBRB. We also need second loop backwards.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

i was trying to solve d1 using union find, that is, i pick 2 nodes i and j, if they share same parent in the first forest or the second forest, we can't make this connection, because then we would be forming a loop, else, make that connection, make node j have same root as node i

was my approach wrong?

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    DSU doesn't work like this. If you want to join two sets that have nodes $$$i$$$ and $$$j$$$, you have to find the leaders (roots) of each set and update their parents, not for $$$i$$$ and $$$j$$$.

»
3 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Problem E can be solved in $$$O(m \log^2 m \log n)$$$ if $$$l_i$$$ and $$$r_i$$$ are constant in each case.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) Please explain me brothers what does this mean?

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Just use it as it is, it makes input/output operations faster, which results in faster runtime. Just use it without knowing what is it(abstraction) and believing it does ur job!!

»
5 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

For, D2, I have a better solution of complexity O(n) + O(n), https://codeforces.me/contest/1559/submission/264315392 , Idea is to just do 1 loop from 1-n and connect all the nodes which are not in the same component as 1, and both in mocha’s and diana’s, and join them to 1, now we we take two pointers L, R and L would have the nodes which are not in 1, (ofc as in first step we added all those nodes), and R would have the same thing for graph2, now we would be adding the edge from L-R.

Spoiler