We will hold AtCoder Beginner Contest 416.
- Contest URL: https://atcoder.jp/contests/abc416
- Start Time: http://www.timeanddate.com/worldclock/fixedtime.html?iso=20250726T2100&p1=248
- Duration: 100 minutes
- Writer: kyopro_friends, sounansya
- Tester: sheyasutaka, cn449
- Rated range: ~ 1999
- The point values: 100-250-300-400-450-525-625
We are looking forward to your participation!








Hi! Good luck guys
Good luck and have fun guys!
Good luck,everyone!Have a fantastic grade,bro!
Good luck!
Good luck guys
How did you approach C and D, Can you explain your thinking process?
I solved C by using backtrack, the constraint of the X in Cth questions works as a hint. can't be able to solve D
C — recursion + backtracking
D — sort both the arrays and use two pointers
Can you have a look at this code and tell what's wrong for problem C?
https://atcoder.jp/contests/abc416/submissions/67971149
I felt like you complicated it too much with the sorting and next permutation and all. The constraints allow us to generate all the permutations, sort them and print the xth smallest one. I don't know why your code isn't working as there are too many variables at play. Try taking a custom input of bigger size and debug your code after each step
C is brute force as k is upto 5 and n is upto 10.
we can generate all possible subsets and select subset size of given k . sort the strings formed in these k sized subsets. print xth string from sorted array.
https://leetcode.com/problems/subsets/ -- can be helpful.
Can you tell me why this fails? I think C was easy too, but I guess I am missing something and also we can't see test cases for AtCoder. (;-;)
C is just generate all possible strings, sort them and output the x-th element.
D was a bit tough for me (maybe because I was trying to find the pairs with sum like 0 or m). But then after trying some values, I realized that I need to find more pairs where sum>=m. Then it was just binary search and sorting.
E was a good one.
I have question what would be the solution for D, if we were asked to find tot % m as well ?
Wdym by 'tot'?
sorry, tot I mean total sum of a[i] + b[i] for (i, 1..n) and minimised that total % m value
Then it doesn't matter if you rearrange or not. Because sum(a[i]+b[i]) for 1<=i<=n is same as sum(a[i])+sum(b[i]) for 1<=i<=n .
Idea behind F ?
did you solve D ? please share the intuition behind it
you can read all elements mode M, then use lower_bound() to find the optimal choice! sort on of them in non-increasing order, because it's more easy to fill the gaps when it still just small sizes at the end, it's the problem of make to tower such that the diff between them is minimal
Is E Floyd warshall?
yes.
Yes. After a long time I saw a good problem using Floyd-Warshall.
Yes, the concept of sky node was really amazing
Could you explain in more detail? I didn’t quite understand the concept
Have you checked the editorial?
I think E is classical.
https://mirror.codeforces.com/contest/1157/problem/E the same problem D in the contest just replace n with m ^_^
got stuck on both C and D... there a ~3000 rank and ~600 performance gap between those who finished ABCD fastest and slowest...
How to see the rating change like shown in the picture? I always need to wait for couple hours before the website update
search "AC-predictor" in greasyfork.org and download the userscript (you need to install tampermonkey in your google first)
Nice solution for E. The idea of using a sky node is very elegant.
where can i get solution for E ?
https://atcoder.jp/contests/abc416/submissions?f.LanguageName=C%2B%2B&f.Status=AC&f.Task=abc416_e&f.User=&orderBy=source_length
Wow that is quite amazing... maybe you should add a spoiler for this
how to approach on problem D? I got stuck on D ...
start by read all elements Mod M, then try to link each element from array A to it's complement on array B if exists, if not, try the closest one
No need of mod m while reading elements. elements values < m.
just code: ~~~~~~~
`
~~~~~~~
Thanks for comments!
Problem G can be solved in $$$O(N|S_\max| + |S_\max|^4)$$$ (where $$$|S_\max| = 10$$$), which is more efficient than the $$$O(K|S_\max|^2)$$$ solution in the editorial.
First, we define a string set $$$T$$$, called "the set of strings that might be the answer."
Next, we consider how to find these strings in the set. We first sort the strings in $$$S$$$ lexicographically in ascending order.
String $$$S_1$$$ is definitely a candidate, so we add it to $$$T$$$.
At this point, any string that might be the answer must start with $$$T_1$$$ as a prefix.
Thus, we directly brute-force check whether $$$S_2$$$ can be a valid string, with a time complexity of $$$O(|S|^2)$$$. The method works by maintaining two pointers, $$$i = 0$$$ and $$$j = 0$$$, and incrementing them as $$$i \gets (i+1) \mod |S_1|, j \gets (j+1) \mod |S_2|$$$. If $$$S_{1,i} \gt S_{2,j}$$$, then $$$S_2$$$ is not a valid answer. If $$$S_{1,i} \lt S_{2,j}$$$, then $$$S_2$$$ might be an answer, and we add it to $$$T$$$.
By following this approach, we find that only the first $$$|S_\max|$$$ elements might be valid answers. Therefore, the time complexity for this part is $$$O(|S_\max|^3)$$$.
When $$$k = \infty$$$, using $$$T_{m=|T|}$$$ repeatedly would be optimal, but since $$$k$$$ is finite, we need to consider how to maintain this.
We observe that the answer must have the following structure:
A certain number of $$$T_m$$$, followed by a certain number of $$$T_{m-1}$$$ (which could be zero), followed by a certain number of $$$T_{m-2}$$$ (which could be zero), and so on, ending with a certain number of $$$T_1$$$ (which could also be zero).
Therefore, we can use dynamic programming (DP), where $$$f_{i,j}$$$ represents the lexicographically smallest string that can be formed using elements from $$$T_{\leq i}$$$, totaling $$$j$$$ elements.
The transition for this DP can be done in $$$O(|T|K^3)$$$, which includes the complexity for string concatenation.
We find that $$$K$$$ is a bit troublesome, but we make a bold assumption: for the first $$$k - |S_\max|^2$$$ strings, we only pick $$$T_m$$$, and only the last $$$|S_\max|^2$$$ strings need further processing.
Here, we discard the DP and directly apply the approach from the official editorial, achieving $$$O(|S_\max|^4)$$$.
Finally, we realize that the previous sorting step is unnecessary. We can directly build a Trie and traverse it to find the first $$$|S_\max|$$$ strings.
o, maybe it can be $$$O(N|S|+|S|^4)$$$ by use editorial's solution in the last step.
Using the editorial, you can solve for arbitrary $$$K$$$ (given that you can output the solution, you can have a bunch of queries at positions where you're asked to print what is the character at said position) in $$$\mathcal{O}(|S_{max}|^3 \log{K})$$$. You compute $$$d_{i, j, t}$$$ = using $$$2^t$$$ strings, what is the shortest string that matches $$$T_{\infty}$$$ starting at position $$$i \mod |S_{max}|$$$ and ending at position $$$j \mod S_{max}$$$ and do matrix exponentiation on that and then you do exponentiation.
To build the base matrix, for each pair $$$(i, j)$$$, you check whether there is a string that matches that part and if there is, you put the length of that string, or if it not, then you set $$$\infty$$$. Then you do exponentiation with $$$* = +$$$ and $$$+ = \max$$$.
E question is so disgusting! The input a, b, and c actually have the same side, so I adjusted it for 40 minutes!
What do you mean by 'same side'?
He meant for size, I think so.
such as have two Edges (a1 to b1) (a2 to b2) a1=a2,b1=b2 but c1≠c2
that is called multi-edges.
Multiple roads may exist between some pair of cities. Also, a city may have multiple airports.https://atcoder.jp/contests/abc416/submissions/67945292 Can someone help me find the counter test case for my solution ?!
3 2 1 cba cb c answer must be cbac
when will rating get updated?
I think today's ABC is good as well. From problem C to F, the topics are [dfs or backtrack], [greedy + binary search or two pointers], [floyd], [dp on trees].
By the way, my solution to problem F is a little bit different from that in editorials.
I use dp[u=1/2/.../n][j=0/1/2][p=0/1/2.../k] to denote the maximum values, where
u denotes the subtree of node-u
j = 0 denotes that node-u is white
j = 1 denotes that node-u is black, and there is still a chance to extend the path to its parent
j = 2 denotes that node-u is black, but we can not extend the path to its parent anymore
p denotes the number of black paths we already have.
Had a bruh moment on E.
I did:
instead of
Kept getting WA :(
fr. I just realized that I was stressing for an hour in the night cuz of this sht 💀💀
Can someone please help me with D, I have everything AC except 2 test cases which are TLE. Here's my code:
~~~~~
calling the b.erase(it) method works for the line because it shifts all the elements after the it. element. It's better to use multiset for this problem
leme try
Yeah, you actually right. Thanks alot!
D and E are good problems (though I didn't solve them during the contest)
I don't understand D, can someone please explain its approach to me and it will be very helpful if you could give me the proof of the solution that you came up with..
Can anyone explain there approach for F ....cant understand the editorial
https://atcoder.jp/contests/abc416/submissions/68006865
i've read the editoral for E, i already build a sky node, multi edges, and etc but kept getting WA, can anyone help pls
jpr[i] = minimum distance to go to the sky for node i
The floyd warshall looks a bit different from what I'm used to.
https://atcoder.jp/contests/abc416/submissions/68027537 (AC)
You're right man, i just learned floyd warshall after the contest, didn't know the right iteration, thanks a lot for your help ^-^
hey kang, I think you need to learn more about Floyd warshall especially the proofing behind the iteration, since u got the iteration wrong. good luck!
Please, someone help, keep getting wa on E, here's my code:
I know brutal force can solve C, but I just wonder how is the idea below failed? If anyone can share a test case that would be great. thanks in advance!