We will hold AtCoder Beginner Contest 350.

- Contest URL: https://atcoder.jp/contests/abc350
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20240420T2100&p1=248
- Duration: 100 minutes
- Writer: physics0523, kyopro_friends, ynymxiaolongbao
- Tester: MMNMM, nok0
- Rated range: ~ 1999
- The point values: 100-200-300-350-450-550-600

We are looking forward to your participation!

GLHF！

Best point values i have ever seen!!!

Why?

I'm late for it! So I had to registered by Unrated!

So sad:(

sad(:

How to do D?

Although I didn't solve D, I'm guessing it could be solved using a graph/tree data structure.

by union-find

like this

Guessed it correctly then. Although I learnt DSU,couldn’t implement it.

a very easy approach other than DSU. first thing to observe is that in every connected component, every node will be friend to every other node. this leads us to the conclusion that every connected component should be a complete graph. in a complete graph, the number of edges is given as:

Now the problem is reduced to something very easy. For each connected component, find the number of nodes it has. this will gives us the required number of edges per connected component. After that you can either subtract m at the end or separately count the number of edges in every connected component and subtract it from the number of edges in a complete graph.

My Submission: Link

I am subtracting m at the end as you said, but is giving Wrong Answer for some cases, if possible can you help

Here is my submission Link: https://atcoder.jp/contests/abc350/submissions/52650972

you are reading

Nedges instead ofMthe faulty part:

it should be

hope it works.

Thanks a lot!!

I checked evima editorial on yt, it was DFS

i do it with dsu as mentioned by the above user

can you share your code link? I tried with Dsu but 15 testcase didnt passed

wtf this tasks

Exactly my thoughts. A-F is criminally bad. Idk about G.

What do you find bad about them ?

They are trivial and way too standard. I only read G 5 mins ago and it's terrible as well, I can't spoil the solution rn but it's a standard optimization, I regret not reading it earlier.

Today, I gave my first atcoder contest and was able to solve 4. How many problems are you able to solve in those contests, generally ? And, how many did you solve today ?

I know ideas to 6 on average, today all 7 (but didn't code G). I usually don't solve <5.

Thanks

+1

Loved E

E — Toward 0 Problem Statement: You are given an integer N. You can perform the following two types of operations:Pay X yen to replace N with ⌊ A N ⌋.Pay Y yen to roll a die (dice) that shows an integer between 1 and 6, inclusive, with equal probability. Let b be the outcome of the die, and replace N with ⌊ b N ⌋.

i didnt understand this question much but the testcases made it worst :(

Is G just small to large merging?

Yes. My solution is: For each node I calculated its parent and depth in its component(a tree). Also I used union find to check if two nodes are in the same component. Notice that you can answer each query using calculated values. To merge two nodes, I recalculated the depth and parent of every node in the smaller component out of the two.

Could you help me find out why this solution gives RTE, TLE and WA? It seems to be what you described.

Submission Link

By a close look I think you should update ancestors inside of dfs on all nodes.

I don't think so, I'm updating only the smaller child when merging.

What I did in my solution is I updade all parents on smaller tree + all depths and when considering a query 2 u v I take the deeper node by depth let it be v and I check if parent[v] is adjacent to u. I use sets in adjacency list to make that quick.

I remember who the parent is for every node + keep a DSU to tell which component is smaller for merging. When merging I update the parents in the smaller tree so that the node from current query becomes its root. The runtime is

`24 ms`

though, so I'm not sure if it's even necessary to merge small to large here?up: Resubmitted with random swaps and got TLE, so small to large seems necessary.

sqrt also passed: split vertices to small an big by current deg. for small store all pairs of neighbours (will be $$$O(n \sqrt n)$$$ total) in set.

Thanks for the solution! Do you think the small memory limit was designed to kill

`sqrt`

s, or for something else?I guess to kill bitsets

For question D, I tried to do it with DSU but failed 16 cases

don't know why.My implementation was like:

long long find_parent(ll u,vector&parent) {

}

// For union

void union_set(ll u,ll v,ll parentU,ll parentV,vector&parent,vector&ranks,ll &ans) {

}

Any idea what wrong I did?

i can't read your code, but there can be more than one connected component, which you may have missed.

I considered that case also, and I have updated my code now.

That

ans = ans + (ru-1)*rv + rv-1;is handling those cases in which when there are multiple components.like

1 is connected with 2,3,4. So, 1's rank will be 4.

6 is connected with 7,8. So, 6's rank will be 3.

Now I want to connect 6 with 1. Now, this formula will do its job.

I was not able to understand D. In the last sample test case it is showing 12 as answer. can someone please explain how 12? I am getting more than 12. If i make 1 and 3 as friend and 3 and 4 are already are friend then i can make 1 and 4 friend as well.

To understand this question, you need to know DSU's working when implemented with path compression and rank optimization.

ok thanks. Let me check dsu.

Actually a simple dfs works:- For any connected component find the no of vertices(v) and edges(e) it has. Then the answer is simply incremented by (vC2-e) where vC2=(v*(v-1))/2 for each component.

My Submission https://atcoder.jp/contests/abc350/submissions/52589120

Your ideal is similar to mine, When there is a pair in so you have to subtract the answer which is 1. My code https://atcoder.jp/contests/abc350/submissions/52618619.

How to solve A and B for just 47 seconds? I could not even open the tasks for that time :)

+1

how to solve $$$E$$$?

Just DP/memoization, the only problem is with dice roll possibly giving you a 1, but that just multiplies the expected number of moves when choosing to roll a die by $$$\frac{6}{5}$$$.

I thought that this solution would get TLE so I didn't implement it.

How can we prove that the states number is small enough?

As written in Japanese editorial, any state to consider can be expressed in a form $$$\lfloor N / (2^p 3^q 5^r) \rfloor$$$, where $$$p, q, r$$$ are non-negative integers, since $$$\lfloor \lfloor N / x \rfloor / y \rfloor = \lfloor N / xy \rfloor$$$. Each of $$$p, q, r$$$ have only $$$O(\log N)$$$ possibilities, so in total the number of states is $$$O(\log^3 N)$$$.

DP calculating cost to reach 0 from current number. Min of 2 ways:

paying X yen for first way

X + f(num // A)

paying Y yet to throw the dice

(6 * Y + f(x // 2) + f(x // 3) + f(x // 4) + f(x // 5) + f(x // 6)) / 5

You get this from

f(x) = Y + (f(x // 1) + f(x // 2) + f(x // 3) + f(x // 4) + f(x // 5) + f(x // 6)) / 6

To get rid of the infinite recursion, move all f(x)'s to the left

The last equation should be —

f(x) = Y + (f(x // 1) + f(x // 2) + f(x // 3) + f(x // 4) + f(x // 5) + f(x // 6)) / 6

Instead of,

f(x) = (Y + f(x // 1) + f(x // 2) + f(x // 3) + f(x // 4) + f(x // 5) + f(x // 6)) / 6

To arrive at the equation —

f(x) = (6 * Y + f(x // 2) + f(x // 3) + f(x // 4) + f(x // 5) + f(x // 6)) / 5

P.S. — You have put Y in bracket and divided by 6 in the last equation.

can you please elaborate more ?

Let F(x) be the cost of reducing x to 0. When rolling a dice, we will pay Y cost. The dice on rolling will give either — 1,2,3,4,5 or 6

If it is 1, the cost will be F(x) = Y+F(x/1)

If it is 2, the cost will be F(x) = Y+F(x/2)

If it is 3, the cost will be F(x) = Y+F(x/3)

If it is 4, the cost will be F(x) = Y+F(x/5)

If it is 5, the cost will be F(5) = Y+F(x/5)

If it is 6, the cost will be F(x) = Y+F(x/6)

But, we need to find expectation value of F(x), which is essentially the mean. Hence,

F(x) = (6*Y+F(x/1)+F(x/2)+F(x/3)+F(x/4)+F(x/5)+F(x/6))/6

F(x) = Y+(F(x/1)+F(x/2)+F(x/3)+F(x/4)+F(x/5)+F(x/6))/6

Note, that there is a F(x) term on both LHS and RHS (This is because F(x/1)=F(x),as dividing x by 1, will give x)

Move the F(x) in the RHS to the LHS side, we get —

F(x)-F(x)/6 = Y+(F(x/2)+F(x/3)+F(x/4)+F(x/5)+F(x/6))/6

Thus, 5*F(x)/6 = Y+(F(x/2)+F(x/3)+F(x/4)+F(x/5)+F(x/6))/6

Hence, F(x) = (6*Y+F(x/2)+F(x/3)+F(x/4)+F(x/5)+F(x/6))/5

Now, this can be solved using Recursion+Memoization. My Submission

thanks a lot man. I was really having hard time solving.

Thank you, Even I'm doubtful initially about cancelling out $$$f(\left\lfloor \frac{N}{1}\right\rfloor)$$$ but later saw your comment and felt good that I'm on track.

In the editorial, it was

$$$f(N) = Y + \frac{1}{5} f(\left\lfloor \frac{N}{2} \right\rfloor) + \frac{1}{5} f(\left\lfloor \frac{N}{3} \right\rfloor) + \frac{1}{5} f(\left\lfloor \frac{N}{4}\right\rfloor ) + \frac{1}{5} f(\left\lfloor \frac{N}{5}\right\rfloor) + \frac{1}{5} f(\left\lfloor\frac{N}{6}\right\rfloor)$$$

$$$ f(N) = Y + \frac{1}{6} f(\left\lfloor \frac{N}{1} \right\rfloor) + \frac{1}{6} f(\left\lfloor \frac{N}{2} \right\rfloor) + \frac{1}{6} f(\left\lfloor \frac{N}{3}\right\rfloor ) + \frac{1}{6} f(\left\lfloor \frac{N}{4}\right\rfloor) + \frac{1}{6} f(\left\lfloor\frac{N}{5}\right\rfloor) + \frac{1}{6} f(\left\lfloor \frac{N}{6} \right \rfloor)$$$

Although it appears recursive due to $$$f(N)$$$ appearing on the right side, by shifting to the left and multiplying the entire equation by $$$\frac{6}{5}$$$, we get:

$$$ f(N) = \frac{6}{5}Y + \frac{1}{5} f(\left\lfloor \frac{N}{2} \right\rfloor) + \frac{1}{5} f(\left\lfloor \frac{N}{3}\right\rfloor ) + \frac{1}{5} f(\left\lfloor \frac{N}{4}\right\rfloor) + \frac{1}{5} f(\left\lfloor\frac{N}{5}\right\rfloor) + \frac{1}{5} f(\left\lfloor \frac{N}{6} \right \rfloor)$$$

Final Problem

$$$f(N) = \min\left(X + f\left(\left\lfloor\frac{N}{A}\right\rfloor\right), \frac{6}{5} Y + \frac{1}{5} f\left(\left\lfloor\frac{N}{2}\right\rfloor\right) + \frac{1}{5} f\left(\left\lfloor\frac{N}{3}\right\rfloor\right) + \frac{1}{5} f\left(\left\lfloor\frac{N}{4}\right\rfloor\right) + \frac{1}{5} f\left(\left\lfloor\frac{N}{5}\right\rfloor\right) +\frac{1}{5} f\left(\left\lfloor\frac{N}{6}\right\rfloor\right)\right)$$$

but what if die is always roll to 1? Problem seems kinda incorrect to me

The probability of such an event is 0.

what is problem with my solution in D? Three test case give me RE

Thanks a lot

It seems that someone's brute force passed G.

marvellous

wow many people think such shit tests preparation is ok?

Since some people are not impressed with the problems, I'd like to counterbalance and say that I quite enjoyed this round.

In particular I don't think the tasks being doable and their difficulty curve being smooth is a bad thing for a contest like this, what with it having 'Beginner' in the name and all. :)

Solved Problem F by using

`__gnu_cxx::rope<char>`

for performing the fast reverse operations (solution) in`1041 ms`

and Problem G in $$$O(N \times Q \times \log{(N)})$$$ with sorted vectors and binary search in them (solution) in`2764 ms`

.Any help with why my code in problem G is wrong?

Code

Is it possible to get banned in Atcoder?

Can anyone tell me why this solution for problem F is giving rte?

Edit: Got AC had some issue in my treap implementation.

.

Actually, the brute force solution to problem G is correct. For instance, in order to make the brute force solution run most slowly, we can use the following graph and query $$$2\ 1\ n$$$ every time:

However, the maximum number of queries is $$$10^5$$$, so the best solution (to make the brute force solution run most slowly) is to use $$$50000$$$ queries to build the graph I mentioned, and use the remaining $$$50000$$$ to query $$$2\ 1\ n$$$. We can calculate that the brute force solution will run $$$50000\times 25000=1.25\times 10^9$$$ times at most $$$^{[1]}$$$. With a 3s time limit, brute force can pass easily.

$$$^{[1]}$$$: If we use $$$n$$$ queries to build the graph, and use the remaining $$$100000-n$$$ to query $$$2\ 1\ n$$$, the brute force solution will run for $$$\dfrac{n}{2}\times (100000-n)$$$ times. Obviously, this is a quadratic function with a maximum value of $$$1.25\times 10^9$$$.

Why do my answer get TLE if I use bfs, but get AC if I use dfs on Problem G？ DFS dfs: https://atcoder.jp/contests/abc350/submissions/52624937 bfs: https://atcoder.jp/contests/abc350/submissions/52624751

Hello, I don't understand the solution for Atcoder : is it a small in large merge ?

How to solve F using a stack?

solve it as balanced parenthesis. Reverse the segments with treap or splay tree and for lower-upper cases use scaline to count no of times an index is affected if it's odd change the case.

I'll just say that your solution is a complete overkill. No need for any of that. Just store all the opening/closing intervals and try to come up with a recursive function that prints the interval [l,r], provided s[l]=='(' and s[r]==')'. It works in O(N).

I didn't get you, how will you reverse the respective segments without them??

It's obvious that we takes intervals [l, r] with given conditions same as we do in balanced parenthesis with the help of stack.

Here's a link to my submission. Url

The trick is that you don't actually have to perform these operations. You just need to know the "depth" of a parentheses meaning the number of open parentheses that come before it that are not yet closed.

The idea is very similar to the problem of reverals of word order in a string. For example "this is good" reversed os "good is this". This can be achieved by first reversing each word and then the whole string:

"this is good" -> "siht si doog" -> "good is this"

If you can find an efficient way to solve this problem in one pass each depth of parentheses define words of the new "sentence".

Hope this helps. It's a bit hard to explain, but essentially you only care about whether you print the interval from left to right or right to left.

My Recursive solution gives TLEIterative passes. Though it is harder to come up with this iterative approach compared to the recursive one.Will the testcases of A update after ABC350?

no

I think it should be that, but the fact is not......

can someone please explain the solution of F ?

Can someone please explain the solution for problem E ?

In problem G, the inputs seem to be generated randomly, how come they guarantee that for type 1 queries u, v belong to different connected components

For Sample 3 in C,I think it's OK to print

2

2 3

1 2

But why was it false in judge?(AC*2 & WA*1)

Are you sure that you have no problem with the other two samples?

is there something wrong in C sample output3? after exchange 1&2 and then exchange 2&3 , 312 will be 231,so i think it may be wrong

I solved ABCD,I am very weak...

Problem E Why can't use this?

But this can pass.

And why can this pass?

Integer overflow when calculating

`y * 6`

?Thanks, it works with

`y * 6LL`

.