teraqqq's blog

By teraqqq, history, 4 days ago, translation, In English

Note. This English translation was produced with the help of ChatGPT.

This year, BSUIR Open XIV took place, and I was very happy to travel to Minsk and attend the contest itself. But what I liked even more was the problem that I proposed there. Overall, I had not seen this idea before, though it feels simple enough that it may well have appeared in some earlier contest. So if you know where this idea had shown up before, please let me know.

Problem. (*BSUIR OPEN XIV*) We are given a $$$100 \times 100$$$ grid. Some pairs of adjacent cells are separated by walls, and there are also walls along the boundary of the grid. A ray starts from the center of cell $$$(r,c)$$$ in direction $$$(x,y)$$$. We need to determine in which cell it will be after traveling a distance of $$$\sqrt{x^2+y^2}$$$, assuming the ray reflects off walls.

Solving the problem

Let us look at the vector $$$(x,y)$$$ and examine the order in which it crosses the vertical and horizontal grid lines. At the point $$$(0,0)$$$ we will ignore the intersection with grid lines, but we will count intersections at the point $$$(x,y)$$$; moreover, for formal reasons, we will assume that at $$$(x,y)$$$ the crossing with the vertical line happens first.

The sequence of crossings can be written as a string:

  • $$$R$$$ denotes crossing a vertical grid line (moving one cell to the right),
  • $$$U$$$ denotes crossing a horizontal grid line.

Thus, to each vector $$$(x,y)$$$ we can assign a string $$$s(x, y)$$$ containing the sequence of these crossings. For example,

$$$ s(4,3) = \texttt{RURURRU}. $$$

Assume that $$$x \ge y$$$. This means the vector is “shallow”, so before each crossing of a horizontal line there is a crossing of a vertical line. More precisely, before the $$$k$$$-th symbol U there are

$$$ \left\lfloor \frac{x}{y} \cdot k \right\rfloor - \left\lfloor \frac{x}{y} \cdot (k-1) \right\rfloor $$$

consecutive symbols R.

Therefore, before each symbol U there is a block containing at least $$$\left\lfloor x/y \right\rfloor$$$ symbols R; these blocks together with the symbols U can be compressed into a single symbol. For example,

$$$ s(4,3) = \texttt{RURURRU} = (\texttt{RU})(\texttt{RU})\texttt{R}(\texttt{RU}) \to s(1,3)=\texttt{UURU}. $$$

So, we may conjecture that if $$$s(x, y, R, U)$$$ denotes the string $$$s(x,y)$$$ where the symbols R and U are replaced by strings $$$R$$$ and $$$U$$$, then the following identity holds:

$$$ s(x,y,R,U)= \begin{cases} s(x-y\lfloor x/y\rfloor,\ y,\ R,\ R^{\lfloor x/y\rfloor}U), & x\ge y, \\\\ s(x,\ y-x\lfloor (y-1)/x\rfloor,\ U^{\lfloor (y-1)/x\rfloor}R,\ U), & x \lt y. \end{cases} $$$

This is indeed true. For example, in the case $$$x \ge y$$$ it is enough to check that after substitution, the number of consecutive R letters before the $$$k$$$-th symbol $$$U$$$ is preserved:

$$$ q = \left\lfloor \frac{x}{y} \right\rfloor: \qquad q + \left\lfloor \frac{x-qy}{y}\cdot k \right\rfloor - \left\lfloor \frac{x-qy}{y}\cdot (k-1) \right\rfloor $$$
$$$ = q + \left\lfloor \frac{x}{y}\cdot k - qk \right\rfloor - \left\lfloor \frac{x}{y}\cdot (k-1) - q(k-1) \right\rfloor $$$
$$$ = \left\lfloor \frac{x}{y}\cdot k \right\rfloor - \left\lfloor \frac{x}{y}\cdot (k-1) \right\rfloor. $$$

Thus, the string $$$s(x,y)$$$ can be computed using the Euclidean algorithm. Of course, in the general case we do not need the string itself, but rather some more useful information. The symbols R and U may belong to an arbitrary monoid for which we can efficiently compute the following operations:

  • product of elements;
  • exponentiation of an element.

For example, the monoid could consist of permutations, matrices, or any other objects that can be maintained in a segment tree. In particular, one can maintain the number of subsequences UR inside a substring of $$$s(x,y)$$$, which corresponds to computing floor_sum (though in that case one has to be careful about the priority/order of the symbols R and U).

In general form, the vector-tracking algorithm can be implemented as follows:

Download code with no registration and no SMS
Solution to the original problem

Other generalizations

During the algorithm, instead of building the composition of the monoids $$$R$$$ and $$$U$$$, we may build the sequence of partial quotients together with all intermediate computations. For example, for input $$$(x,y)$$$ the computation table looks as follows. Recall that

$$$ q = \max\left\{\left\lfloor \frac{x}{y}\right\rfloor,\ \left\lfloor \frac{y-1}{x}\right\rfloor\right\}. $$$
Step Computed value $$$q$$$ $$$x$$$ $$$y$$$
1 RU 1 4 3
2 RURUR 2 1 3
3 RURURRU 1 1 1

Working with all intermediate computations, we can additionally handle the following scenarios:

  • composition of the monoids $$$R$$$ and $$$U$$$ on any subsegment $$$(l,r)$$$ of the string $$$s(x,y)$$$.

For example, if we start the vector not from the point $$$(0,0)$$$ but from an arbitrary point $$$(\alpha, \beta)$$$, then the resulting string $$$s(x,y,\alpha,\beta)$$$ may differ from $$$s(x,y)$$$. However, it is also clear that the string $$$s(x,y,\alpha,\beta)$$$ is just a cyclic shift of the string $$$s(x,y)$$$.

To understand exactly which cyclic shift it is, recall that the string $$$s$$$ can be represented as a concatenation of blocks of the form R...RU, where the number of letters R before the $$$k$$$-th letter U is given by

$$$ \left\lfloor \frac{x}{y}\cdot k \right\rfloor - \left\lfloor \frac{x}{y}\cdot (k-1) \right\rfloor = \frac{x}{y}\cdot k - \frac{xk \bmod y}{y} + \frac{x(k-1) \bmod y}{y}. $$$

Therefore, if the vector starts from an arbitrary point $$$(\alpha/y, 0)$$$, then the length of the $$$k$$$-th block is given by a similar formula:

$$$ \left\lfloor \frac{x}{y}\cdot k + \frac{\alpha}{y} \right\rfloor - \left\lfloor \frac{x}{y}\cdot (k-1) + \frac{\alpha}{y} \right\rfloor = \frac{x}{y}\cdot k - \frac{(xk + \alpha) \bmod y}{y} + \frac{(x(k-1) + \alpha) \bmod y}{y}. $$$

If $$$x$$$ and $$$y$$$ are coprime, we may substitute

$$$ \alpha = k_0 x \pmod y. $$$

Then the number $$$k_0$$$ is exactly the desired cyclic shift.

Thus, the proposed algorithm is a universal (and probably not the best in terms of constant factors) way to solve many generalizations of floor_sum. In particular,

$$$ f(l, r, p, q, a, b, c) = \sum_{x=l}^r \left\lfloor \frac{a+bx}{c} \right\rfloor^p x^q $$$

can be computed in time

$$$ O(\log C \cdot pq(p+q)) $$$

in the general case, which is quite nice.

Full text and comments »

  • Vote: I like it
  • +142
  • Vote: I do not like it

By teraqqq, history, 12 months ago, translation, In English

Hello Codeforces! This Saturday, the student final of BSUIR Open XIII will take place, accompanied by rounds Codeforces Round 1021 (Div. 1) and Codeforces Round 1021 (Div. 2) at Apr/26/2025 11:35 (Moscow time), featuring problems from this competition. Note the unusual time of the round.

This year, the problems for the rounds and the olympiad were prepared by teraqqq, FairyWinx, Kirill22, and wilcot. Each round will contain 6 problems, and you will have 3 hours to solve them. Notably, wilcot has been contributing problems to BSUIR Open for multiple years and has helped involve new authors in this event. We believe this effort has resulted in a diverse and exciting student final, part of which has been adapted into this Codeforces round.

We extend our special thanks to MikeMirzayanov, geranazavr555, and KAN for the fantastic Polygon and Codeforces platforms! We also thank:

Special thanks to the people who made BSUIR Open XIII possible:

  • Velikovich Vladimir, Shved Elizaveta, Vnuk Olga, Yurchenko Olga, Gorokh Andrey, Makarevich Darya, and Sevryukov Stepan for organizing the championship;
  • Udovin wilcot Ivan, Markovets Roman, Baitasov Rinat, Romashevsky German, Butoma Vitaly, Lyubashenko Andrey, and Sitnikov Alexey for preparing the semifinal problems;
  • Yefimchyk Alexander and Adarov Dmitry for technical support of the championship.

For yet another year, the BSUIR Open championship has taken place within the walls of BSUIR. Each time, the organizers bring something new, unique, and unforgettable. Thanks to them, BSUIR Open continues to delight participants with its friendly atmosphere, engaging problems, and wonderful prizes!

UPD. The score distribution is:

Div 2: $$$500 + 1250 + 1500 + 2250 + 2750 + 3250$$$

Div 1: $$$500 + 1000 + 1500 + 2000 + 3000 + 3500$$$

UPD2: Editorial

Full text and comments »

  • Vote: I like it
  • -784
  • Vote: I do not like it

By teraqqq, 16 months ago, translation, In English

Hello to all fans of competitive programming and Codeforces enjoyers! We're happy to invite you to participate in Hello 2025, which will take place on Jan/04/2025 17:35 (Moscow time).

We, teraqqq, dope, and induk_v_tsiane, have prepared 8 problems for you, and you’ll have 2 hours and 30 minutes to solve them. One of the problems is divided into 2 subtasks. The round will be rated for all participants.

Huge thanks to MikeMirzayanov, geranazavr555, and KAN for great systems Polygon and Codeforces!

Special thanks also go to:

Score distribution: 500 — 1000 — 1500 — 2250 — (1000+2000) — 3500 — 3750 — 4500

UPD (Update!) (Update! Yey!) Editorial — link

This round was prepared with the support of T-Generation. If you are a Russian-speaking student, please switch to the Russian version of this blog. There you will find information about our educational projects that may be interesting for you.

UPDATE

Congratulations to the winners:

  1. jiangly

  2. ksun48

  3. ugly2333

  4. Flamire

  5. Ormlis

  6. hos.lyric

  7. ecnerwala

  8. noimi

  9. maspy

  10. arvindf232

Full text and comments »

Announcement of Hello 2025
  • Vote: I like it
  • +432
  • Vote: I do not like it