We will hold AtCoder Beginner Contest 210.

- Contest URL: https://atcoder.jp/contests/abc210
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20210717T2100&p1=248
- Duration: 100 minutes
- Number of Tasks: 6
- Writer: leaf1415
- Tester: Nyaan
- Rated range: ~ 1999

The point values will be 100-200-300-400-500-600.

We are looking forward to your participation!

Thanks for the wonderful contest! I spent a lot of time on D. Wish I was faster with implementation skills got it in like last 2 minutes phew!

D-ideaExpand the manhattan distance and make 4 cases, 2 of which will be redundant. So we need 2 dp's to calculate answer.

CodeInstead of making 4 cases, just make 2 cases. Lets just fix the point for which 'row' value would be bigger. The code gets a lot more simple with that.

CodeYeah I have made 2 cases not 4. But your's is much neat :)

Can you explain the idea slightly more?

mand

The 1st and 4th quadrants are what we need to calculate answer(the other 2 are symmetric to 1 and 4 so we can discard them.). So consider them only and write expression of cost. It will of form

We treat each half individually and do a dp on the later half and store its minimum value over some 2d prefix matrix. Then we check and try to improve our answer by adding the former part of the expression and print the minimum.

A short implementation - Submission

How to think of short implementations faster ?

Think of some tricks to simulate many cases with some variables which you can pass in some function on basis of which it will change. Don't know to explain it better.

Can you plz. explain

problem c?How to do F?

++

We can reduce the problem to one big 2-SAT instance, containing one variable per card denoting which side of it we use (plus some supplementary ones).

First, we go through every possible divisor $$$d \in [2, MAX]$$$, where $$$MAX = 2 \cdot 10^6$$$, that could divide two numbers on the front side of two cards. For every $$$d$$$, we enumerate all cards $$$C_d$$$ that contain a number divisible by $$$d$$$ on either side. We can to this in $$$\mathcal{O}(MAX \log MAX)$$$ if we save every card containing a number for every number. The total size of all $$$C_d$$$ should be roughly $$$4 \cdot 10^6$$$ if we use the $$$\sqrt[3]{n}$$$ approximation for the number of divisors of some $$$n$$$.

Now for every $$$d$$$, we check whether there exists a card containing numbers divisible by $$$d$$$ on both sides. If thats the case, we go through we all other cards in $$$C_d$$$ and force them be flipped to the side not divisible by $$$d$$$ using a clause like $$$(x \lor x)$$$ or $$$(\overline{x} \lor \overline{x})$$$ (if there is another card with both sides divisible by $$$d$$$, we can immediately output no).

If there is no card with both sides divisible by $$$d$$$, we want to allow at most one card in $$$C_d$$$ to be flipped to its divisible-by-$$$d$$$ side. For that there is a construction using about $$$3 \lvert C_d \rvert$$$ clauses and $$$\lvert C_d \rvert$$$ supplementary variables. KACTL's 2-SAT code contains an implementation for it. I've always used it without figuring out why it works, but it probably becomes obvious if you spend some time playing around with it on paper. Solving the 2-SAT instance then just requires the standard algorithm using strongly-connected components.

For the last paragraph, you mentioned I used edges in a kind of prefix and suffix order a little bit similar to this Problem.

Is there any other nice trick to handle these types of implications? mine implementation was messy.

How to solve E?

Make pair of $$$Ai$$$ and $$$Cost$$$. Sort on Cost in ascending order, if cost is same compare gcd($$$Ai$$$,$$$N$$$). Calculate final answer by iterating on the vector pair and adding maximum possible edges with the current cost (edges = $$$N$$$ — $$$gcd(N-Ai)$$$). Update $$$N$$$ = $$$gcd$$$ after every iteration. If we reach $$$n$$$ = 1, i.e we made N-1 edges, break.

Actually this explanation is very helpful. I should just draw out some simple examples I guess to understand it. I've never seen any problems like it. What kind of prerequisite is helpful to get this one? The editorial just left me in the dust. I know modular arithmetic for the most part, but don't know how gcd came into it.

My code for D was failing for only 3 testcases for some reason. Can someone tell what is wrong with my code? Submission Link

Code.

Try this case

The answer should be $$$4$$$. $$$(1, 2)$$$ and $$$(2, 1)$$$

I also made the same mistake, you can correct it by rotating the grid clockwise once and repeating the dp.

thanks

I made the same mistake 5 times :(

We are only checking for (i, j) and (i', j'), such that (i, j) is up and left when compared to (i', j'). There need to be two DP checks for both directions.

Deleted

difficulty gap between C & D is soo high!

So true..

Wrote some hell on D, E was easier

A different idea to solve D: Let's solve it as a shortest paths problem. The graph will have a start vertex, which is connected to some randomly chosen tiles in the grid. The rest of the tiles is connected to the end vertex. Then search for the shortest path from start vertex to end vertex.

More concretely, from the start vertex make an edge of length $$$A_{i,j}$$$ to some pairs $$$(i, j)$$$ and from some pairs $$$(i', j')$$$ have an edge of length $$$A_{i',j'}$$$ to the end vertex. Also connect each $$$(i, j)$$$ to neighboring tiles ($$$(i + 1, j), (i - 1, j), (i, j+1), (i, j-1)$$$) with length $$$C$$$.

We set the probability of being connected to the start vertex as 0.5. The probability that the optimal pair has one vertex connected to the start and the other to the end is also 0.5. So if we repeat this whole algorithm $$$k$$$ times, the probability that we missed the optimal pair is just $$$2^{-k}$$$. Admitedly, this has a worse complexity than the solution in the editorial, $$$O(WH * log(WH) * k)$$$

Can someone explain problem E solution. I am not able to understand how they are calculating the number of connected components after each operation in the editorial.

++

Can anyone give me the solution of problem D according to tutorial logic?

Can anybody take a look at my code!? This is a

`O(H * W * log(H + W))`

solution, but getting TLE on few cases. Is there a way to make it more efficient?Here's my Code.