Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

twoplusthree's blog

By twoplusthree, history, 7 months ago, In English

Motivation

A few weeks back I had the opportunity to attend the ICPC Asia West Continent Finals 2023 along with my teammates VS-Codes and om_mittal7, where we encountered the following problems as part of the contest problemset. We weren't able to solve E in contest, and that kept bugging me for quite a while. I was especially motivated to try and prove the claims that we had made in the contest, which proved to be quite a hard challenge.

Finally, when I did manage to cook up solutions with reasonably sound proofs, the underlying ideas and the little observations made along the way felt so elegant that I decided to document it here for future reference, and to share with the community. I hope these ideas turn out to be useful for someone stuck in a similar place as I was.

Looking forward to your thoughts and suggestions!

A. Basic Vocabulary

Trivia

We assume WLOG that $$$b_1>b_2$$$. The number of digits in the base-$$$b_1$$$ representation of $$$n$$$ is therefore not greater than that in base-$$$b_2$$$. Hence, if a solution exists, $$$(n)_{b_1}$$$ must be a prefix of $$$(n)_{b_2}$$$. Here $$$(x)_b$$$ represents the base-$$$b$$$ representation of the integer $$$x$$$.

Let's start off by observing that any number $$$n$$$ when represented in base $$$b$$$, where $$$2b>n$$$ is of the form $$$[1,n-b]_b$$$. Considering $$$2b_1>n$$$, what must be the constraint on $$$b_2$$$ for $$$(n)_{b_1}$$$ to be a prefix?

Let $$$n-b_1=x$$$, then $$$n=[1,x]_{b_1}$$$. Note that $$$x>0$$$ since $$$b_1<n$$$.

Thus we have,

$$$n=[1,x,\underbrace{\cdots}_{k \ \text{digits}}]_{b_2} \implies n=(b_2^{k+1}+x \cdot b_2^k)+r \implies n=(b_2+x)b_2^k+r \ \ \ \ \ (k>0, r<b_2^k)$$$

Thus, $$$(b_2+x)$$$ is the quotient obtained on dividing $$$n$$$ by $$$b_2^k$$$.

$$$(b_2+x)b_2^k \leq n \leq (b_2+x+1)b_2^k-1$$$

Now, we note that $$$1 \leq x \leq b_2-1$$$, since $$$x$$$ is a non-zero digit in base-$$$b_2$$$. Therefore,

$$$\boxed{(b_2+1)b_2^k \leq n \leq (2b_2)b_2^k-1} \cdots (1)$$$

If there exists any $$$b_2$$$, $$$k$$$ satisfying the above inequality, then we can derive $$$b_1$$$ as follows:

  • $$$q \leftarrow \Big\lfloor\frac{n}{b_2^k}\Big\rfloor$$$
  • $$$x \leftarrow q-b_2$$$
  • $$$b_1 \leftarrow n-x$$$

We observe that the upper bound of $$$(1)$$$ strictly increases with $$$b_2$$$ for a given $$$k$$$. Therefore, it is optimal to choose the largest value of $$$b_2$$$ such that $$$(b_2+x)b_2^k \leq n$$$, which can be found in $$$\mathcal{O}(\log(n^{\frac{1}{k}}))$$$ by Binary Search. Since $$$b_2 \geq 2$$$, we must test at most $$$\log_2{(n)}$$$ values of $$$k$$$.

Note that all this while, we are operating under the assumption that $$$2b_1<n$$$. Let's observe the case when $$$k=1$$$. We have,

$$$(b_2+1)b_2 \leq n \leq 2b_2^2-1$$$

Now, consider how we find $$$b_2$$$. We choose the largest value that satisfies the lower bound and check whether it satisfies the upper bound. What if the upper bound for a certain $$$b_2$$$ went beyond the lower bound for $$$b_2+1$$$? For visualisation, one could try imagining that for every $$$b_2$$$ there is a certain interval in which $$$n$$$ must lie for the inequality to hold. What happens when these intervals start to overlap? Then we'd be guaranteed a solution for any value of $$$n$$$.

$$$2b_2^2-1 \geq (b_2+1+1)(b_2+1) \implies b_2^2-3b_2-3 \geq 0$$$

which is true $$$\forall b_2 \geq 4$$$.

Therefore, we conclude that $$$\forall n \geq (4+1)(4)=20$$$, $$$\exists b_1,b_2$$$ such that $$$2b_1>n$$$ and $$$(1)$$$ holds for $$$k=1$$$.

Thus, we finally have our complete solution. For $$$n<20$$$ the trivial brute force algorithm is fast enough. For $$$n\geq 20$$$, we test solely for $$$k=1$$$ as described before and report the answer obtained.


Code

E. Parker Rectangle

Trivia

Given $$$r+c=n \implies c=n-r$$$. We assume WLOG that $$$r\leq c \implies 0<r\leq \frac{n}{2}$$$.

Let $$$M_{r \times c}$$$ be the constructed matrix. Let $$$S$$$ be the sum of all elements in $$$M$$$. Let $$$R_i$$$ denote the sum of elements in the $$$i^{th}$$$ row and $$$C_j$$$ denote the sum of elements in the $$$j^{th}$$$ column of $$$M$$$.

For convenience, we consider the matrices to be $$$0$$$-indexed, that is, the first element of the first row of $$$M$$$ is denoted as $$$M_{00}$$$.

Claim: There exists no Parker Rectangle of odd order greater than $$$5$$$.

Proof

By inspection, it is trivial to construct Parker Rectangles of order $$$3$$$ and $$$5$$$, such as the ones below:

$$$M_3=\begin{bmatrix}0&1\end{bmatrix}$$$

$$$M_5=\begin{bmatrix}0&3&4 \cr 5&2&1\end{bmatrix}$$$

For $$$n=2m$$$, we use the following method of construction:

Let $$$r=c=m$$$. We define two $$$m \times m$$$ matrices $$$A$$$ and $$$B$$$ as follows:

$$$A_{ij}=(i+j)\%m$$$
$$$B_{ij}=(2i+j)\%m$$$

For example, when $$$m=3$$$,

$$$A=\begin{bmatrix}0&1&2 \cr 1&2&0 \cr 2&0&1\end{bmatrix}$$$


$$$B=\begin{bmatrix}0&1&2 \cr 2&0&1 \cr 1&2&0\end{bmatrix}$$$

And, when $$$m=4$$$,

$$$A=\begin{bmatrix}0&1&2&3 \cr 1&2&3&0 \cr 2&3&0&1 \cr 3&0&1&2\end{bmatrix}$$$


$$$B=\begin{bmatrix}0&1&2&3 \cr 2&3&0&1 \cr 0&1&2&3 \cr 2&3&0&1\end{bmatrix}$$$

Claim: For any given $$$i,j$$$ the ordered pair $$$(A_{ij},B_{ij})$$$ is unique.

Proof

We now define an injection $$$g:\mathbb{Z}_m\times \mathbb{Z}_m \rightarrow \mathbb{Z}$$$ as $$$g(x,y)=\alpha x+y$$$ where $$$\alpha \geq m.$$$ For the purposes of this construction let us consider $$$\alpha = m$$$.

We then construct the Parker Rectangle $$$M_n$$$ as $$$M_{ij}=g(A_{ij},B_{ij})$$$.

For example, when $$$m=3$$$,

$$$M_6=\begin{bmatrix}0&4&8 \cr 5&6&1 \cr 7&2&3\end{bmatrix}$$$

And, when $$$m=4$$$,

$$$M_8=\begin{bmatrix}0&5&10&15 \cr 6&11&12&1 \cr 8&13&2&7 \cr 14&3&4&9\end{bmatrix}$$$

Proof of correctness

Note that by construction, each row in matrices $$$A$$$ and $$$B$$$ contains the numbers $$$0 \ ... \ m-1$$$ exactly once (can be proved using the Pigeonhole principle).

Hence $$$\forall i$$$,

$$$R_i=\displaystyle\sum_{j=0}^{m-1}M_{ij}=\displaystyle\sum_{j=0}^{m-1}\alpha A_{ij}+B_{ij}=\alpha \displaystyle\sum_{j=0}^{m-1}A_{ij}+\displaystyle\sum_{j=0}^{m-1}B_{ij}=\frac{(m-1)m}{2}(\alpha +1)$$$

When $$$m$$$ is odd, each column in $$$A$$$ and $$$B$$$ also contains contains the numbers $$$0 \ ... \ m-1$$$ exactly once. Thus $$$C_i=\frac{(m-1)m}{2}(\alpha +1) \ \forall i$$$, and so the maximum absolute difference $$$=0 \leq \lceil \frac{n}{2}\rceil$$$.

When m is even, the even-indexed columns in $$$B$$$ contain the even numbers $$$0,2,\ ... \ m-2$$$ repeated twice, and the odd-indexed columns contain the odd numbers $$$1,3, \ ... \ m-1$$$ repeated twice.

Hence,

$$$\displaystyle\sum_{i=0}^{m-1}B_{ij}=\begin{cases}2(0+2+...+m-2)=\frac{m(m-2)}{2} & \text{if } j \text{ is even} \cr 2(1+3+...+m-1)=\frac{m^2}{2} & \text{if } j \text{ is odd}\end{cases}$$$

And therefore,

$$$C_j=\begin{cases}\frac{(m-1)m}{2}\alpha+\frac{m(m-2)}{2} & \text{if } j \text{ is even} \cr \frac{(m-1)m}{2}\alpha+\frac{m^2}{2} & \text{if } j \text{ is odd}\end{cases}$$$

Hence, the maximum absolute difference $$$=\frac{m^2}{2}-\frac{m(m-2)}{2}=m \leq \lceil \frac{n}{2}\rceil$$$.


Code

Full text and comments »

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

By twoplusthree, history, 14 months ago, In English

One of my friends VS-Codes recently introduced me to an Alice-Bob game problem (Newton School CodeRush September '23 — Problem C) which goes as follows —

The Problem

The solution to this is a pretty straightforward result which I arrived at after making a few lucky guesses as to how the game would proceed.

The Solution

However, the real challenge was to prove that this solution works. After many trials and revisions, I came up with a proof that looked reasonably complete. Would love to hear the community's suggestions on it, and if I could have made it shorter somehow (I tried my best).

Proof
Less mathy (proof?)

Full text and comments »

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

By twoplusthree, history, 17 months ago, In English

Hello Codeforces!

I noticed that many online ICPC problem archives (specifically codedrills.io, which hosts the official ICPC contests for Asia-West) do not provide an option for virtual participation.

Thus, while attempting to practice those rounds out-of-contest, it became quite tedious to manually keep track of submission times and verdicts in order to calculate total Time Penalty.

In order to make this process easier, I wrote a basic GUI Application in Java to keep a record of submissions and automate calculation of scores. It also features a contest timer and an ICPC-style dynamic scorecard to provide a simulated contest environment.

start_new_contest_dialog_screenshot

contest_window_screenshot

Link to GitHub repo: https://github.com/de-upayan/icpc-scorer

Do let me know your suggestions and feedback in the comments!

Full text and comments »

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

By twoplusthree, 18 months ago, In English

Thank you for participating! We hope you enjoyed the problems!

Links:

A. Crush-ed

Author: VS-Codes

Tutorial
Implementation

B. Yet Another Binary String Matching Problem

Author: VS-Codes

Tutorial
Implementation

C. Good Subarrays

Author: Yomapeed

Tutorial
Implementation

D1. Valley Dominoes (Easy Version)

Author: twoplusthree

Tutorial
Implementation

D2. Valley Dominoes (Hard Version)

Author: twoplusthree

Tutorial
Implementation

E. Stack Queries

Author: beedle

Tutorial
Implementation

F. Step One Missing

Author: twoplusthree

Tutorial
Implementation

G. Gringotts

Author: twoplusthree

Tutorial
Implementation

H. Fine De

Author: twoplusthree

Tutorial
Implementation

I. Queen

Author: om_mittal7

Tutorial
Implementation

J. Plag Incoming

Author: Anupam_Ghosh

Tutorial
Implementation

K. Zombies Are Fun

Author: Anupam_Ghosh

Tutorial
Implementation

L. Square Trees

Author: DrearyJoke

Tutorial
Implementation

Full text and comments »

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

By twoplusthree, 18 months ago, In English

Hello Codeforces!

We (twoplusthree, VS-Codes, Anupam_Ghosh and om_mittal7) are glad to invite everyone to Replay of JU BitFest Season One 2023 which is scheduled on [contest_time:445869].

BitFest is an annual ICPC-style Programming Contest organised for freshers and sophomores of Jadavpur University. This year marked the very first edition of this contest, which consisted of two rounds — online Prelims and on-site Finals. We hope you like the problems we have created!

You will be offered $$$10 - 15$$$ problems (from the combined problemset of both the rounds), and $$$4$$$ hours to solve them. You may participate alone, or in teams of not more than $$$3$$$ people. The problems vary in difficulty, ranging from Easy to Intermediate. Some of the problems may also require a classical solution. Note that the contest is unrated.

We would also like to thank:

Links:

Good luck, and have fun!

UPD 1: Apart from the Replay problemset, there will be a non-zero number of new problems set by our testers — (Yomapeed, beedle and DrearyJoke), to make the round more interesting :-D. The total number of problems will remain $$$10 - 15$$$.

UPD 2: Registration for the contest is now live.

UPD 3: Contest is live now. Best of luck!

UPD 4: Contest is over. We hope you enjoyed the problems. Feel free to discuss them here. We will release the editorial soon.

UPD 5: Thank you all for participating! Here are the winners:

Rank Contestant(s)
1 kasparovian
2 gupta_nitin
3 Kira_1234
4 Team Badut (floralfield, VincentK, aufannn)
5 Team Kindered_spirits (Coder_Nayak, sloppy_coder, _gyrFalcon_)

First solve for every problem:

Problem Contestant(s)
A benzyl
B Team BhavyaKashyap (bhavya_rajdev, zxy0909)
C gupta_nitin
D1 Team BhavyaKashyap (bhavya_rajdev, zxy0909)
D2 kasparovian
E kasparovian
F kasparovian
G kasparovian
H Team crush ki smile :uwu: (18o3)
I Team skull issu (X-OR, TECHlES, -ZORD)
J Jishudip
K prodipdatta7
L YouKn0wWho

UPD 6: The contest editorial is out! All contest submissions have been made public.

Full text and comments »

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