👋 Salam, Codeforces!
We are thrilled to invite you to the Rayan Programming Contest 2024 - Selection (Codeforces Round 989, Div. 1 + Div. 2).
The Selection Round is a rated contest for Div. 1 + Div. 2, taking place on 30.11.2024 17:35 (Московское время). The problems for this round were crafted and prepared by ArshiaDadras, MohammadParsaElahimanesh, AmShZ, AmirrzwM, and Keshi.
The top 60 trusted participants (with a maximum of three from each country) will qualify for the onsite Final Round in Tehran, in Spring 2025. An additional quota is reserved for the host country. Hotel accommodation, local transportation, and meals will be provided to all finalists. More details about the final contest will be available on the Rayan website.
🏆 Selection Round Prizes:
- The top 50 participants will receive T-shirts. 👕
- Additionally, 50 random participants from ranks 51-1000 will also receive T-shirts.
🏅 Final Round Prizes:
- 1st place: $15,000 💵
- 2nd place: $10,000 💵
- 3rd place: $5,000 💵
- 4th–6th places: $2,000 each
- 7th–10th places: $1,000 each
🙏 Special Thanks:
We would like to extend our deepest gratitude to:
- MikeMirzayanov for enabling this event through the amazing Codeforces platform.
- The one and only KAN for his invaluable support in hosting the round.
- Dear TheScrasse for his dedicated coordination efforts and lots of discussions about the problems
and rejecting a big chunk of our problems for Problem C. He also contributed some problems to the contest, one of which was selected by the team! - Our testers who tested and provided invaluable feedback to improve the problems: peti1234 for being our LGM (VIP) tester, _istil for his great ideas
and ignoring his school midterms to test the problems, Error_Yuan for helping usand picking on tough problems for no reason, Hyperbolic, DylanSmith, cadmiumky, Clan328, Weikatuo, CepryH9, WansurMyKing667, PineapplesOnPizza, vyshniak.n, ErrorAssassin, nicolasalba, naniim, estoy-re-sebado, mika_uwu, rulerofcakes, AkiLotus, Too_Weak_To_Anything, sleepwalking643, Leonardo_Paes, RockSnow, waidenf, and newb_programmer. - Prof. Hamid Zarrabi Zadeh and Prof. Mohammad Ali Abam, whose leadership and organizational efforts make this event a reality. Thanks to their vision, Rayan is becoming one of the largest competitive programming contests in terms of prizes (and soon participants!).
- You for participating and getting positive delta (and maybe the final prize in the next year 💸😉).
📊 Score distribution: $$$500 - 750 - 1500 - 1500 - 2000 - (2000 + 2000) - (3000 + 3000) - 5000$$$
We look forward to seeing you both online and onsite!
Good Luck & Have Fun! 🎉
📝 Note: Editorial just published here! 🚀🍿
🔔 Update: The official announcement for the Rayan 2025 World Finals has just been published! 🎉 You can read all the details about the onsite event in Tehran, November 25–29, 2025, in Arpa’s blog. For registration, schedule, and more information, please visit the Rayan website. Make sure to check it out if you’re among the top 1000 in the Selection Round! 🚀







Auto comment: topic has been updated by ArshiaDadras (previous revision, new revision, compare).
Is there a separate category for Iranians, or will all participants be grouped together?
All participants are considered together. The only difference is that we have a separate quota (about 30–40) for Iranians, which will not be counted within the main quota.
As a tester, I didn't know I could have won $15,000 (just needed to beat a certain Belarusian...)
Y'all should give it a shot!!! Good luck.
Sorry man, but I have to admit that I'm kinda glad you didn’t know that 🫂
For the 1st time in my life, I found the tester's comment funny!
As an author, please give me T-shirt too.
I predict tourist will fall below 4000.
Edit after round: everyone who downvoted this should apologize to me in DMs.
he already did unfortunately last contest
I predict tourist will become tourist once again.
yes i think so
I think he will become tourist again.
yeah same i think it too
He lost to orzdevinwang again
Nice! This must be one of the rounds of all time, I'm so excited to see my teachers' round ;)
Let me tell u sth about ArshiaDadras:
I officially started competitive programming and programming classes with Arshia Dadras four years ago. He is a very capable and ethical teacher. Thank you, Arshia, for being so great <3
Thank you so much for your kind words and support, AmirParsa!
It’s truly a pleasure to be remembered in such a thoughtful way. I’m also really happy to see that you’re continuing to improve your coding skills.
Wishing you the best of luck in your journey, and I hope we get to meet in person soon!
Thank you, man. It's an honor for me to have grown under the guidance of professors like you. I'm looking forward to meet you in person too ;)
"The top 60 trusted participants (with a maximum of three from each country)"
How will you enforce this?
We will start inviting participants from the top of the list while considering the country limit (a maximum of three participants per country) until we fill all 60 spots. Each invited participant will have a specific time frame to confirm their attendance. If someone declines or cannot attend, their spot will be given to the next eligible participant from the scoreboard who meets the criteria.
As a tester, this is the $$$4^{th}$$$ time in a row I found at least one problem in the round I tested really satisfying, and I hope people would feel the same.
I love Persian contests!
Why do you like Persian contests?
Because most of them are about their history, and Persian history is very good.
As a tester I wish good luck to everyone!
As a tester, I can confirm that I tested this round.
As a tester, I liked to test my first round! :)
PS: If you win $15,000, please give me the T-Shirt, hahaha
gpi
Final Round Prizes: 1st place: $15,000 2nd place: $10,000 3rd place: $5,000 4th–6th places: $2,000 each 7th–10th places: $1,000 each
^^^
How much will you win? They're really rich...
(This is my first comment on CodeForces so if you think I'm wrong please politely scold me)
If you look at their website, the onsite part is sponsored by a university and from what I can tell an entire ministry, and these prizes are for the onsite part. This round is only a selection round before the onsite one.
as a persian, i can confirm i ain't winning shit
But I guess we'll have someone from Iran in the top 10... P.S. Let’s bet lunch on it!
Kaviani?
W salam
I'm quite interested that, is there any special about the sponsor? why does this contest appears on the top bar?
tourist will destroy this contest to earn money
With pleasure—he’s more than welcome!
As a participant, I will participate!
BTW, what watch are you wearing, sir ?! ArshiaDadras
Do you arrange this every year?
We’re planning to do that, but this is Rayan’s first year!
We need to evaluate the feedback and reception from participants before making a clearer decision.
Thanks.. It will be great. Can I ask you the meaning of "Rayan"
“Rayan” is derived from the first part of the Persian word “Rayaneh,” which means “computer.”
Fun fact: If someone asks us, “What is the event name?” we would reply, “It’s Rayan.” In Persian, we could say this as “Rayan eh,” which follows the same pattern as saying, “It’s cold” -> “Sard eh” or “It’s good” -> “Khoob eh.” Interestingly, this sounds exactly like the word “Rayaneh”!
this round will make tourist get back to gm i guess. lol
Nah he will become reverse nutella again
I'm just looking at the rewards knowing I won't be even close to top 10 :/
Why not? At the very least, you’d get to enjoy a wonderful trip to Iran, stay in one of the best hotels here for free, and experience some amazing hospitality! (if you can make it to the top 60 😁)
if there are $$$50$$$ russians above me and all of them refuse to go to the onsite, will i be invited?
Yes, as I said here! (if you'll be one of the next candidates to invite)
if someone sets their country to Gibraltar will you somehow check that they are indeed from Gibraltar?
I hope we don’t encounter these types of cheaters in the top 60. But don’t worry—we’ll verify their passports during the invitation process to catch any discrepancies.
Sad
As an Iranian, this really makes me proud!
And I would like to thank the medalists who designed the questions and all the people involved and hardworking in this event!
I think it is a good thing that the famous programmers of Codeforces come to our country.
I hope to see the tourist here!
The same day as chinese NOIP! Hoping we all have good performance in the both round!
Good luck to us.
Is this round rated?? as normal cf contest
participation is enough for registration or i have to register on website or something??
It's like a normal rated contest in CF. Also, participation is enough for registration and you don't need to register on the website or somewhere else.
Time Conflict with ICPC Regionals QAQ
Salaam too.
As a tester, I wish everyone good luck! :)
teams or no?
No, buddy!
Rayan Gosling Round
Wish tourist back 4000++ once more.. yo yo
rainboy orz (Problem H).
Seems orzdevinwang will become tourist soon!
...Why does the sentence sounds that strange.
I said D > C;
c>d for sure bro
b>>d
What is the case in E where a solution exists for both k and n being odd and k < n?
in all cases, a solution will exist if n and k are odd except for k=1 and k=(n-1)!
k=3 and n=5 what is the solution?
1 2 3 4 5 3 4 5 1 2 5 3 1 4 2
how do you construct the solution if k=3 and n>k?
First 2 permutation will be 1,2...n and (n+1)/2...n,1..(n-1)/2 . For odd n
ah ok i get the solution i just assumed that if k=3 and n>k it was impossible
C was so annoying to solve. Forgot one edge case and spent all my contest time on it, leaving no time for D :(
Actually, C seemed difficult to me not because of solving the problem, but because of the implementation. What edge case did you get stuck on? I can share my solution if you want.
How to solve E?
For even k, you can keep any permuation and it's opposite( p'[i]=n+1-p[i]). For odd k, you need to find any 3 permuation with equal sum on all index and then do as the case with even.
Sooo nice E! Though it contains some casework, there is a beautiful construction method.
Can you share it with us common folk?
We treat $$$n=1$$$, $$$k=1$$$, $$$k=n!$$$, $$$k \gt n!$$$ first.
Let's define $$$i$$$ th permutation as the permutation when we call next_permutation for $$$(1,2,\dots,n)$$$, $$$i-1$$$ times.
First we construct for even $$$k$$$. We can just take $$$i$$$ th permutation $$$p$$$ and $$$(n!)+1-i$$$ th permutation $$$q$$$ together. (actually, $$$p_i = n+1-q_i$$$)
For even $$$n$$$, if $$$k$$$ is odd, the answer is
No(because $$$k \times \frac{(n+1)n}{2}$$$ isn't divisible by $$$n$$$)Now, we construct, for odd $$$n$$$, $$$k=3$$$. This can be done by this method (as an example, let $$$n=7$$$).
At last, we construct, for general odd $$$k$$$. We ignore following pairs:
At the current state, we can take $$$\frac{n!}{2} - 3$$$ pairs. After that, just add $$$k=3$$$. Then, we can construct for $$$3 \le k \le n!-3$$$.
tysm
wow! That left shift (n-1)/2 is insane, Any inspiration to come up with that?
I saw this problem and immediately coded brute force for $$$n=5$$$ and $$$k=3$$$ and the first one that came out was that construction
It comes by try & error so I can't tell exact step, but I tried to match $$$(1,4,7)$$$ first, suddenly cames up my construction.
Maybe $$$(1,2,3,4,5,6,7)$$$ + (shift) leads the difference of the sum of two permutation $$$+2, +2, \dots, +2, -n+2, +2, \dots, +2$$$ (and under odd $$$n$$$, this works) is the decent way?
I didn't manage to solve E during contest (I was really close, but I missed a few simple cases, like n = 1 and forgot about the cap for evens). I did get something very close. But basically once you realise that you can do all evens up to n! you can come up easily with "maybe if I can figure out a solution for 3 then it will work". And then you realise that you are looking for something that once you add 1 2 3 ... n it makes the same thing, you get the idea that you make something where the sum increases by 1 every time. I still had some trouble, but then I thought it would be easier if I just do every 2.
So for every 2 I could do for 7:
4 5 6 7
1 2 3 4
that would add up to 5 7 9 11
and then the rest complete the sequence. So my own solution for 7 was:
4 1 5 2 6 3 7
1 5 2 6 3 7 4
7 6 5 4 3 2 1
Then it's somewhat obvious that 4 = 7 / 2 + 1. Then it was easily expandable as start for n / 2 + 1 for the first one.
why is this getting downvoted
Because the problem E is neither nice, nor beautiful.
how you rate a problem can be subjective, however, why would you take it out onto someone else who's not involved in setting it?
Nothing could be worse than losing rating on my birthday :(
Edit: problems are nice, I choked on H with some cases like
1 3 105 63 45and what I missed is just enumerating the GCD of all picked numbers...Happy birthday tho!
Well a gain of 0 is better than any loss I suppose. Happy birthday!
Yeeeees! I’m so happy that codeforces give me this non-negative delta as my birthday gift, and my solution for H works after fixing the bug. Thank you! OvO
AT 2:53 OF CONTEST TIME THE DIFFERENCE BETWEEN TOURIST AND ORZDEVINWANG WAS 32 POINTS. TOURIST NEEDED TO HACK SOMEONE AND HE MANAGED TO DO SO 8 SECONDS BEFORE THE END OF THE CONTEST!!!!!! BUT ORZDEVINWANG SOLVED G2 3 MINUTES BEFORE THE END OF THE CONTEST, SECURING THE FIRST PLACE!!!!! WHAT A DRAMA OMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOGMOG
The problems were nice, though I feel like I solved something very similar to F a long time ago on Codeforces (I didn't have time to solve it though, I spent too long on implementing D and E). Also, they have very nice pictures :D Do you think that they are AI generated? They are very intricate but I don't see any obvious AI artifacts. Otherwise, big respect to the artist who made them.
You're welcome!
orz orzdevinwang. Also IF orzdevinwang didn't solve G2, then tourist would have won by doing a successful hacking attempt at the last minute, that's crazy.
Is it true that in H the max size of the answer is 3? And does it have randomized solution?
The max size of the answer is 6. let $$$x=2\cdot3\cdot5\cdot7\cdot11\cdot13$$$, then $$${\frac{x}{2},\frac{x}{3},\frac{x}{5},\frac{x}{7},\frac{x}{11},\frac{x}{13}}$$$ is a valid set
Here is the rough sketch for H.
This is how we can characterize all possible linearly independent sets $$$a$$$.
First find a set of distinct primes $$$p_1,p_2,\ldots,p_k$$$ and let the gcd of all the $$$a$$$ be $$$g$$$. We will write $$$P = \prod p_i$$$ as the product of all the primes here.
Then we can construct a linearly independent set $$$a_1,a_2,\ldots,a_k$$$ where $$$a_i$$$ is a multiple of $$$\frac{gP}{p_i}$$$ but not $$$gP$$$.
For each linearly independent set, you can find a corresponding $$$(g,p_1,p_2,\ldots,p_k)$$$ that satisfy the above constraint.
Then after brute forcing everything, you realize the the number of possible $$$(g,p_1,p_2,\ldots,p_k)$$$ with $$$k \geq 3$$$, $$$g \mid P$$$ and $$$\frac{gP}{\min(p_i)} \leq 10^5$$$ is at most 500k or something. So you brute force everything. I had to use a bunch of constant opts to pass because my code is shit.
Checking answer is at least $$$2$$$ can be done in $$$O(n \log n)$$$.
What's with the constraint of A? I see a lot of people passed it with brute forcing, but they're quite close to the time limit, while I got TLE and I suspect it's because I used
long longinstead ofint.I don't see why it couldn't be much higher so that no brute force solution could pass, or be much lower so that every brute force solution could pass.
Why are there few pretests on problem D and E? I'm worried that my solution won't pass the system tests.
Nevermind, they all passed the system tests.
How to solve F? I cant find any clear approach to solve this problem
Firstly, for simplicity, redefine the special conditions to be in terms of the number of rubies/sapphires in your satchel (not chest). Then add dummy states $$$(0, 0)$$$ and $$$(n, m)$$$ for convenience (we won't double value at these though).
Define dp states to be $$$dp[i]$$$, which is the total sum of value of your satchel across all ways you can reach the state defined by the $$$i$$$-th condition.
To solve this dp you will firstly need to define some ordering on the conditions so that when evaluating some dp state, all the states that it depends on have already been evaluated. Just sort by $$$x + y$$$.
Then, the transition from state $$$i$$$ to state $$$j$$$ will correspond to the ways you can go from state $$$i$$$ to state $$$j$$$ without visiting any other special condition state in between.
Computing the number of ways you can go from state $$$i$$$ to state $$$j$$$ (where $$$i$$$ occurs before $$$j$$$ in the topological ordering) without visiting any intermediate special condition state is easy and can be done using inclusion exclusion in $$$O(n^3)$$$. Call this $$$w[i][j]$$$.
Also define the total number of ways for us to go from state $$$i$$$ to $$$j$$$ to be $$$g[i][j]$$$. This can be computed in $$$O(1)$$$ using simple combinatorics.
Define the addition in value for going from state $$$i$$$ to state $$$j$$$ without any intermediate doubling to be some $$$h[i][j]$$$.
Then, the dp formulation for state $$$i$$$ is:
(where $j$ occurs before $$$i$$$ in the topological sort)
All states will be evaluated in $$$O(n^2)$$$.
The final answer is the value of the dp state corresponding to the the state $$$(n, m)$$$ divided by the total number of ways to go from $$$(0, 0)$$$ to $$$(n, m)$$$, which is just $$$\binom{n + m}{n}$$$.
Can you explain more about how to calculate $$$w(i, j)$$$ using inclusion exclusion, I still didn't get this part.
$$$w[i][i] = 1 \; \forall \; i$$$.
$$$w[i][j] = g[i][j] - \sum_{k} {w[i][k] \cdot g[k][j]}$$$ (here $$$i$$$ occurs before $$$j$$$ and $$$k$$$ occurs between $$$i$$$ and $$$j$$$ in the ordering)
Seems I got it, thanks!
I liked F. However E is bad.
(1) I believe it made the contest unfair that one sample testcase was silently added. What you should have done is to make a clarification as an annoucement instead.
(2) The essential part had appeared multiple times — IMO 2009 Shortlist C2 and UTPC2023 and possibly many others. The "distinct" part does not add fun (perhaps personal opinion).
who can tell me why runtime error pleaseYour text to link here...
D
294071265 this gives runtime error on pretest 8 for problem C . Can anyone tell what is the reason . I have not seen similar verdicts in any other solutions , so I am curious what caused this error ?
have you considered what would happen if dfs(i, j) went out of bounds?
How can this even happen , for all border elements which are overflowing i have assigned visited =1 and verdict=-1 initially only. Can you explain or give example
How to solve D ?
What I did was iterate until the columns are sorted. On each iteration I:
Swap the 1 that is furthest to the left with the 0 that it furthest to the right until all 0s are to the left of every 1.
Swap the 2 that is furthest to the left with the 1 that is furthest to the right until all 1s are to the left of every 2.
I did have to use a set to store the indexes otherwise it would TLE.
I can't imagine how many people were blocked by $$$n=m=1$$$ in $$$C$$$ and $$$n=k=1$$$ in $$$E$$$...
why is n=m=1 in C an edge case? I didn't consider this and it seems fine
if you have all ?-s in the grid, then n=m=1 is the only case you will have 0 as answer. otherwise the answer is n*m always.
but still depends on the implementation obviously, i failed it though
Wrong answer on test 4 ...
I realized right after the end of the contest is that the wrong with my code for $$$E$$$ is that I didn't handle the case where $$$n = 1$$$
it really hurts 😢
A regular request to increase the memory limit to allow solutions in python. Going from https://codeforces.me/contest/2034/submission/294035798 to https://codeforces.me/contest/2034/submission/294041682
was neither insightful nor fun.
BRO I passed A with 999ms whats on about the tight TL
I thought A was O(log(n)) per tc lol
GreedyForces
idk how many CF rounds I have to do to stop wasting half an hour on finding N = K = 1 testcases on E and similar :v
If you have a hard time figuring that out, then what am I supposed to do? I missed two test cases in E....
& I used "int n" instead of "long long n" and couldn't debug it though i had 1 hours left. :) sadlyf.
I FST-ed on A :( Why half allow O(t*a*b) solution
loved the statements, especially the characters. look forward to seeing this competition continue in the coming years!
TLE on A system testing because of define int long long
because it's bad
Loved the images :)
I still do not understand why I got TLE on A and B in the system testing while the other similar answers were getting accepted...
A: https://codeforces.me/contest/2034/submission/294007119 B: https://codeforces.me/contest/2034/submission/294033976
In A, using int is faster than long-long
https://codeforces.me/contest/2034/submission/294091655
please someone help me in figuring out whats wrong in my code?
please!
Fixed in 297729300
The issue is that when this is executed:
j may be out of bounds.
yea i realised it later that night! just missed continue; in my code
Hoping to get my first t-shirt(among Randomly choosen 50 participants)
This is my code for question A but it has not been accepted and showing TLE . How is this possible. I have reviewed my code thoroughly and believe it is correct .The code should definitely work as the constraints are — (1≤a,b≤1000) and (1≤t≤100). Many with same logic have their solutions accepted. I kindly request re-evaluation of my submission.
Submission Id — 294008048
Your solution works in $$$O(tab)$$$, with the constraint the worst case is $$$10^3\times 10^3\times 10^2=10^8$$$.
However, the
%operator has a large constant factor, which might case TLE in this case.#define int long longalso slow down your code a little bit.PS: your code runs for about 2700ms on AtCoder (usually faster than codeforces) in about a $$$8\times 10^7$$$ total product, which is within the constraint. It would definitely TLE in the worst case here.
The fact that the glorious history of Iran and amazing myths were mentioned in the text of the questions made me proud that I was born in Iran. I hope this beautiful work will be repeated later.
unable to view the editorial
hello everyone do you know how to get job because i was recently graduated i don't know what to do
get good.
can anybody give me the id of this contest
2034
thanks a lot man, how did you find it out
You can find it from the URL: codeforces.com/contest/ 2034
How did you add a link to the text?
I've seen people do for a long time, never got how(I guess I never got curious enough).
CF blog texts are written in a format very similar to Markdown. You can read more about adding reference links and other features online. For example, if you’d like to learn how to insert a link into a page, you can check this guide. You can also read more about the Codeforces blog writing format here.
Thanks!
vulestamenkovic orz
vulestamenkovic orz for being super sigma specialist.
orz FinrodF
Nice problems! F2 and H are nice, even though I wasn't able to debug the latter in contest time :(
On a side note: Why insert the image in the middle? It randomly divides the statement into two parts, which can be really annoying when checking to see if you have missed something.
Today I received this message:
Your solution 294541298 for task 2046D significantly coincides with the solutions of other participants and is in the group of identical solutions Hectonit/294541298, studboy/294566190.
This is unfair decision. The only similar parts of this submissions: mine, studboy — is parts with sets, which is not the main logical part. Main ideas looks quite similar, but ways of solving are different. Hope this misunderstanding will be fixed.
Hello, Timophey!
Detecting similarities between codes and marking them as potential cheating cases is not something handled by the problem authors. In fact, we aren’t informed when these checks are performed or who ends up being flagged as a result.
This process is part of the system’s automated operations. I believe reaching out to KAN or contacting the appropriate individuals in charge might help resolve this situation for you.
I’m sorry that I’m unable to assist directly with this matter.
best problems
I received a form to promote the final round from system, but my rank is 160 (about 20 of Japanese). Is this message correct?
Yeah, that's correct!
You could be one of the qualified participants if those ahead of you in the queue decline to attend the in-person contest, making you the next candidate for an invitation.
This form is sent to potential invitees rather than the final list of confirmed qualifiers. After you complete it, an official invitation letter will be sent separately.
If I answer "yes", am I required to accept the formal invitation? How much will the trip cost?
The official invitation letter will provide more detailed information about the event, including trip costs, the exact date, and other relevant details. You will also have the opportunity to ask any questions you may have during this process.
As far as I remember (not entirely sure), the only cost you need to cover is your flight tickets; we will cover everything else!
Why does like have of the Codeforces population have an anime character as their PFP?
Sure, there's nothing wrong with that until you see it's all females/waifus.
Hey, I'm not judging(Just curious), but it can't just be that everyone just wants to have their PFP as a female/waifu.
And maybe it's just me but I have no idea who most of them are(including the male anime characters)