Google Kick Start Archive — Round B 2022 problems

Overview

Thank you for participating in Kick Start 2022 Round B!


Cast

Infinity Area: Written by Mohamed Yosri Ahmed and prepared by Vijay Krishan Pandey.

Palindromic Factors: Written by Gagan Madan and prepared by Pranav Gavvaji.

Unlock the Padlock: Written by Aziz Eroui and prepared by Rahul Goswami.

Hamiltonian Tour: Written by David Arthur and prepared by Laksh Nachiappan.

Solutions, other problem preparation, reviews and contest monitoring by Abhilash Tayade, Aditya Kumar, Akriti Anand, Akul Siddalingaswamy, Alan Lou, Anurag Singh, Anushi Maheshwari, Arjun Sanjeev, Ashveen Bansal, Aziz Eroui, Bartosz Kostka, Bohdan Pryshchenko, Chun-nien Chan, Cristhian Bonilha, David Arthur, Deepanshu Aggarwal, Diksha Saxena, Fahim Ferdous Neerjhor, Gagan Kumar, Gagan Madan, Jared Gillespie, Jimmy Dang, Krists Boitmanis, Kunal Verma, Kushagra Srivastava, Laksh Nachiappan, Lizzie Sapiro Santor, Maks Verver, Matt Kenison, Michał Łowicki, Mohamed Yosri Ahmed, Nikita Rungta, Pranav Gavvaji, Pratibha Jagnere, Radhika Agarwal, Rahul Goswami, Rohan Garg, Ruoyu Zhang, Sasha Fedorova, Surya Upadrasta, Suryansh Gupta, Swapnil Gupta, Swapnil Mahajan, Teja Vardhan Reddy Dasannagari, Uday Patel, Vakul Gupta, Vijay Krishan Pandey, Vinay Khilwani.

Analysis authors:

  • Infinity Area: Fahim Ferdous Neerjhor
  • Palindromic Factors: Krists Boitmanis
  • Unlock the Padlock: Swapnil Gupta
  • Hamiltonian Tour: Krists Boitmanis

A. Infinity Area

Problem

Let us assume for the simplicity of this problem that the Infinity symbol is made of circles which touch externally at point $$$S$$$ as shown below, and let us call it the center of the infinity.

You are given three integers $$$\mathbf{R}$$$, $$$\mathbf{A}$$$, $$$\mathbf{B}$$$. You are currently at the center of the infinity. You will first start drawing the right circle with radius $$$\mathbf{R}$$$ and reach again the center of infinity. After that, you start drawing the left circle with the radius equal to the radius of last circle multiplied by $$$\mathbf{A}$$$. After reaching the center of the infinity you again start drawing the right circle with radius equal to the radius of last circle divided by $$$\mathbf{B}$$$ (integer divison). After reaching the center of infinity you again draw the left circle with the radius equal to the radius of last circle multiplied by $$$\mathbf{A}$$$.

Steps to create the infinity symbol with three circles.

You continue to draw the left and right circles as described above until the radius of the circle to be drawn becomes zero. Calculate the sum of areas of all the circles drawn. It is guaranteed that the process will terminate after finite number of steps.

Input

The first line of the input gives the number of test cases, $$$\mathbf{T}$$$. $$$\mathbf{T}$$$ lines follow.

Each line represents a test case and contains three integers $$$\mathbf{R}$$$, $$$\mathbf{A}$$$, $$$\mathbf{B}$$$, where $$$\mathbf{R}$$$ denotes the radius of the first circle, and $$$\mathbf{A}$$$ and $$$\mathbf{B}$$$ are the parameters used to calculate the radii of the subsequent circles.

Output

For each test case, output one line containing Case #$$$x$$$: $$$y$$$, where $$$x$$$ is the test case number (starting from 1) and $$$y$$$ is the sum of areas of all the circles drawn until radius of the circle to be drawn becomes zero.

$$$y$$$ will be considered correct if it is within an absolute or relative error of $$$10^{-6}$$$ of the correct answer. See the FAQ for an explanation of what that means, and what formats of real numbers we accept.

Limits

Time limit: 20 seconds.
Memory limit: 1 GB.

Test Set 1

$$$1 \le \mathbf{T} \le 100$$$
$$$1 \le \mathbf{R} \le 10^5$$$
$$$1 \le \mathbf{A} \le 500$$$
$$$2 \times \mathbf{A} \le \mathbf{B} \le 1000$$$

Sample

Sample Input
content_copy Copied!
2
1 3 6
5 2 5
Sample Output
content_copy Copied!
Case #1: 31.415927
Case #2: 455.530935

In Sample Case #1, you start with drawing the right circle with radius $$$1$$$ unit. After reaching the center of infinity you draw the left circle with radius $$$1 \times 3=3$$$ units. Again after reaching the center of infinity you stop drawing the right circle since the radius becomes $$$\lfloor3/6\rfloor=0$$$ units. Therefore the sum of areas of the circles drawn is $$$\pi \times 1 \times 1 + \pi \times 3 \times 3 \approx 31.415927$$$.

In Sample Case #2, you start with drawing the right circle with radius $$$5$$$ units. After reaching the center of infinity you draw the left circle with radius $$$5 \times 2=10$$$ units. After reaching the center of infinity you draw the right circle with radius $$$\lfloor10/5\rfloor=2$$$ units. After reaching the center of infinity you draw the left circle with radius $$$2 \times 2=4$$$ units. After reaching the center of infinity, you stop drawing since the radius of next circle becomes $$$\lfloor4/5\rfloor=0$$$ units. Therefore the sum of areas of the circles drawn is $$$\pi \times 5 \times 5 + \pi \times 10 \times 10 + \pi \times 2 \times 2 + \pi \times 4 \times 4 \approx 455.530935$$$.

B. Palindromic Factors

Problem

You are given a positive integer $$$\mathbf{A}$$$. Find the number of factors of $$$\mathbf{A}$$$ which are palindromes. A number is called a palindrome if it remains the same when the digits in decimal representation are reversed. For instance, 121 is a palindrome, while 123 is not.

Input

The first line of the input gives the number of test cases, $$$\mathbf{T}$$$. $$$\mathbf{T}$$$ lines follow.

Each line represents a test case and contains a single integer $$$\mathbf{A}$$$.

Output

For each test case, output one line containing Case #$$$x$$$: $$$y$$$, where $$$x$$$ is the test case number (starting from 1) and $$$y$$$ is the number of factors of $$$\mathbf{A}$$$ which are palindromes.

Limits

Time limit: 2 seconds.
Memory limit: 1 GB.
$$$1 \le \mathbf{T} \le 100$$$.

Test Set 1

$$$1 \le \mathbf{A} \le 10^{3}$$$.

Test Set 2

$$$1 \le \mathbf{A} \le 10^{10}$$$.

Sample

Sample Input
content_copy Copied!
4
6
10
144
242
Sample Output
content_copy Copied!
Case #1: 4
Case #2: 3
Case #3: 7
Case #4: 6

In the first test case, $$$\mathbf{A}$$$ has $$$4$$$ factors which are palindromes: $$$1,2,3,$$$ and $$$6$$$.
In the second test case, $$$\mathbf{A}$$$ has $$$3$$$ factors which are palindromes: $$$1, 2,$$$ and $$$5$$$.
In the third test case, $$$\mathbf{A}$$$ has $$$7$$$ factors which are palindromes: $$$1, 2, 3, 4, 6, 8,$$$ and $$$9$$$.
In the fourth test case, $$$\mathbf{A}$$$ has $$$6$$$ factors which are palindromes: $$$1, 2, 11, 22, 121,$$$ and $$$242$$$.

C. Unlock the Padlock

Problem

Imagine you have a padlock, which is a combination lock consisting of $$$\mathbf{N}$$$ dials, set initially to a random combination. The dials of the padlock are of size $$$\mathbf{D}$$$, which means that they can have values between $$$0$$$ and $$$\mathbf{D}-1$$$, inclusive, and can be rotated upwards or downwards. They are also ordered from left to right, with the leftmost and rightmost dials at positions $$$1$$$ and $$$\mathbf{N}$$$, respectively. The padlock can be unlocked by setting the values of all its dials to $$$0$$$.

You can perform zero or more operations of this kind:

  • Pick any range $$$[l, r]$$$ such that $$$1 \le l \le r \le \mathbf{N}$$$ and rotate all the dials in $$$[l, r]$$$ together, upwards or downwards. Rotating up increases the value of each dial in the range $$$[l, r]$$$ by $$$1$$$, and rotating down decreases its value by $$$1$$$. Note that a dial with value $$$\mathbf{D}-1$$$ becomes $$$0$$$ when increased (rotated up) and a dial with value $$$0$$$ becomes $$$\mathbf{D}-1$$$ when decreased (rotated down).

The series of operations must satisfy the following condition:

  • The range $$$[l_{i-1}, r_{i-1}]$$$ chosen in the $$$(i-1)$$$-th operation needs to be completely contained within the range $$$[l_i, r_i]$$$ chosen in the $$$i$$$-th operation; that is, $$$l_i \le l_{i-1} \le r_{i-1} \le r_i$$$. The initial range ($$$[l_1, r_1]$$$) can be chosen arbitrarily.

Example of a valid sequence of operations to unlock a padlock with initial combination $$$[1, 1, 2, 2, 3, 3]$$$:

  1. Rotate range $$$[5, 6]$$$ downwards.
  2. Rotate range $$$[3, 6]$$$ downwards.
  3. Rotate range $$$[1, 6]$$$ downwards.

The following are some operations that cannot be performed:

  1. Rotating range $$$[1, 4]$$$ after $$$[6, 9]$$$, because $$$[6, 9]$$$ is not completely contained in $$$[1, 4]$$$ (does not satisfy $$$r_{i-1} \le r_{i}$$$ where $$$r_{i-1} = 9$$$ and $$$r_i = 4$$$).
  2. Rotating range $$$[3, 6]$$$ after $$$[2, 7]$$$.

The goal for you is to output the minimum number of valid operations needed to make all dials in the padlock set to $$$0$$$.

Input

The first line of the input contains the number of test cases, $$$\mathbf{T}$$$. $$$\mathbf{T}$$$ test cases follow.

Each test case consists of two lines.

The first line of each test case contains two integers $$$\mathbf{N}$$$ and $$$\mathbf{D}$$$, representing the number of dials in the padlock and the size of the dials, respectively.

The second line of each test case contains $$$\mathbf{N}$$$ integers $$$\mathbf{V_1}, \mathbf{V_2}, \dots, \mathbf{V_N}$$$, where the $$$i$$$-th integer represents the value of the $$$i$$$-th dial in the initial combination of the padlock.

Output

For each test case, output one line containing Case #$$$x$$$: $$$y$$$, where $$$x$$$ is the test case number (starting from $$$1$$$) and $$$y$$$ is the minimum number of operations needed to unlock the padlock as described in the statement.

Limits

Time limit: 30 seconds.
Memory limit: 1 GB.
$$$1 \le \mathbf{T} \le 100$$$.
$$$0 \le \mathbf{V_i} \le \mathbf{D}-1$$$, for all $$$i$$$.

Test Set 1

$$$1 \le \mathbf{N} \le 40$$$.
$$$\mathbf{D} = 2$$$.

Test Set 2

$$$1 \le \mathbf{N} \le 40$$$.
$$$2 \le \mathbf{D} \le 10$$$.

Test Set 3

$$$1 \le \mathbf{N} \le 400$$$.
$$$2 \le \mathbf{D} \le 10^9$$$.

Sample

Note: there are additional samples that are not run on submissions down below.
Sample Input
content_copy Copied!
2
6 2
1 1 0 1 0 1
6 2
0 1 0 0 1 1
Sample Output
content_copy Copied!
Case #1: 3
Case #2: 2

In Sample Case #1, the minimum number of operations needed to unlock the padlock is $$$3$$$. We can unlock it using the following operations:

  1. Rotate range $$$[4, 4]$$$ downwards.
  2. Rotate range $$$[3, 5]$$$ downwards.
  3. Rotate range $$$[1, 6]$$$ downwards.

In Sample Case #2, the minimum number of operations needed to unlock the padlock is $$$2$$$. We can unlock it using the following operations:

  1. Rotate range $$$[3, 4]$$$ upwards.
  2. Rotate range $$$[2, 6]$$$ downwards.


Additional Sample - Test Set 2

The following additional sample fits the limits of Test Set 2. It will not be run against your submitted solutions.
Sample Input
content_copy Copied!
2
6 10
1 1 2 2 3 3
6 10
1 1 9 9 1 1
Sample Output
content_copy Copied!
Case #1: 3
Case #2: 3

In Sample Case #1, the minimum number of operations needed to unlock the padlock is $$$3$$$. We can unlock it using the following operations:

  1. Rotate range $$$[5, 6]$$$ downwards.
  2. Rotate range $$$[3, 6]$$$ downwards.
  3. Rotate range $$$[1, 6]$$$ downwards.

In Sample Case #2, the minimum number of operations needed to unlock the padlock is $$$3$$$. We can unlock it using the following operations:

  1. Rotate range $$$[3, 4]$$$ upwards.
  2. Rotate range $$$[3, 4]$$$ upwards.
  3. Rotate range $$$[1, 6]$$$ downwards.

D. Hamiltonian Tour

Problem

Hamilton is a Canadian city near Toronto, and a nice place to take a walking tour.

In this problem, Hamilton is represented by a grid of unit cells with $$$2\mathbf{R}$$$ rows and $$$2\mathbf{C}$$$ columns, where each cell is either empty (represented by *) or contains a building (represented by #). The cell on the $$$i$$$-th row and $$$j$$$-th column is represented by $$$A_{i,j}$$$ where $$$1 \le i \le 2\mathbf{R}$$$ and $$$1 \le j \le 2\mathbf{C}$$$. It is not possible to enter cells containing buildings and you can only move to an adjacent cell that shares a side with the current cell (not just a corner). The grid is such that if it is divided evenly into $$$2\times2$$$ blocks of unit cells, then in each of those blocks, either all four cells are empty, or all four cells are occupied by a building. Let us represent the block formed by $$$ A_{2i-1,2j-1}, A_{2i-1,2j}, A_{2i,2j-1},$$$ and $$$A_{2i,2j}$$$ cells as $$$\mathbf{B_{i,j}}$$$ where $$$1 \le i \le \mathbf{R}$$$ and $$$1 \le j \le \mathbf{C}$$$.

Grace is a tourist in Hamilton and wants to visit all the empty cells in Hamilton. Grace is currently in cell $$$A_{1,1}$$$. Visiting the same cell twice could be boring for her. Hence, Grace wants to visit each of the empty cells exactly once and finally end in cell $$$A_{1,1}$$$. Can you help Grace by providing a string (consisting of directional moves {N, E, S, W} representing the unit moves to the north, east, south, or west respectively) which Grace can follow to visit every empty cell once and end again in $$$A_{1,1}$$$.

Input

The first line of the input gives the number of test cases, $$$\mathbf{T}$$$. $$$\mathbf{T}$$$ test cases follow.
The first line of each test case contains two integers $$$\mathbf{R}$$$ and $$$\mathbf{C}$$$.
The next $$$\mathbf{R}$$$ lines of each test case contains $$$\mathbf{C}$$$ characters each.

The $$$j$$$-th character on the $$$i$$$-th of these lines represents the block $$$\mathbf{B_{i,j}}$$$ formed by the following four cells: $$$ A_{2i-1,2j-1}, A_{2i-1,2j}, A_{2i,2j-1}, $$$ and $$$ A_{2i,2j} $$$.
If $$$\mathbf{B_{i,j}=}$$$ #, all four of the cells in $$$\mathbf{B_{i,j}}$$$ are occupied by a building.
Otherwise, if $$$\mathbf{B_{i,j}=}$$$ *, all four of the cells in $$$\mathbf{B_{i,j}}$$$ are empty.

Output

For each test case, output one line containing Case #$$$x$$$: $$$y$$$, where $$$x$$$ is the test case number (starting from 1) and $$$y$$$ is the answer to the problem as follows.

If there is no solution to the problem, $$$y$$$ should be IMPOSSIBLE. Otherwise, $$$y$$$ should be a sequence of characters from the set {N, E, S, W}, representing the unit moves (to the north, east, south, or west respectively) in a valid route, starting from $$$A_{1,1}$$$, as described in the statement above.

Note that your last move should take you to $$$A_{1,1}$$$; this move does not count as visiting the same cell twice.

If there are multiple valid solutions, you may output any one of them.

Limits

Time limit: 25 seconds.
Memory limit: 1 GB.
$$$1 \le \mathbf{T} \le 100$$$.
$$$1 \le \mathbf{R} \le 200$$$.
$$$1 \le \mathbf{C} \le 200$$$.
All characters in the grid are from the set {#,*}.
The first character of the first line of the input grid for each test case is a * character, i.e. $$$\mathbf{B_{1,1}=}$$$*.

Test Set 1

A block contains buildings if and only if the row number and column number of it are divisible by 2. i.e. $$$\mathbf{B_{i,j}=}$$$ # $$$\iff ((i \bmod 2 = 0)$$$ and $$$ (j \bmod 2 =0))$$$.

Test Set 2

No extra constraints.

Sample

Note: there are additional samples that are not run on submissions down below.
Sample Input
content_copy Copied!
3
1 1
*
2 2
**
*#
3 4
****
*#*#
****
Sample Output
content_copy Copied!
Case #1: SENW
Case #2: SSSENNEENWWW
Case #3: ESSSSEEENNNWWNEEEEESWWSSSEESWWWWWWWNNNNN

The sample output displays one set of answers to the sample cases. Other answers may be possible.

In Sample Case #1, Grace will follow the route $$$A_{1,1}$$$, $$$A_{2,1}$$$, $$$A_{2,2}$$$, $$$A_{1,2}$$$, and finally $$$A_{1,1}$$$. Note that ESWN is the only other possible valid answer.
The image below shows one of the possible routes for Sample Case #1.
One of the possible routes for Case 1.

The image below shows one of the possible routes for Sample Case #2.
One of the possible routes for Case 2.


Additional Sample - Test Set 2

The following additional sample fits the limits of Test Set 2. It will not be run against your submitted solutions.
Sample Input
content_copy Copied!
3
3 1
*
*
#
1 3
*#*
3 4
**#*
**#*
****
Sample Output
content_copy Copied!
Case #1: SSSENNNW
Case #2: IMPOSSIBLE
Case #3: ESSSSENNNNESSSSEEENNNNESSSSSWWWWWWWNNNNN

The image below shows one of the possible routes for Sample Case #1.
One of the possible routes for Case 1.

In Sample Case #2, it is impossible for Grace to travel to any cell in $$$\mathbf{B_{1,3}}$$$ from $$$A_{1,1}$$$.

Analysis — A. Infinity Area

The idea is to use brute force. You can simulate the multiplication and division operations and stop when $$$\mathbf{R}$$$ reaches zero. As it is integer division, it is guaranteed that after a finite number of steps, $$$\mathbf{R}$$$ will eventually become zero. At first it might look like that it will exceed the TL, but the $$$2 \times \mathbf{A} \leq \mathbf{B}$$$ condition ensures that the value of $$$\mathbf{R}$$$ will be halved each time a division by $$$\mathbf{B}$$$ occurs. The area of a circle with radius $$$\mathbf{R}$$$ is $$$\pi\mathbf{R}^{2}$$$ which can be calculated in $$$O(1)$$$. So, if the value of $$$\mathbf{R}$$$ is halved with each division, it leads to a final time complexity of $$$O(\log(\mathbf{R}))$$$.

A pseudocode would look something like this:


  ans = R*R
  while R > 0:
    R *= A
    ans += R*R
    R /= B
    ans += R*R
  return ans*pi

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — B. Palindromic Factors

A simple way to test if a number is palindromic is to convert it to a string first and then check if the string equals its reverse. Since $$$\mathbf{A}$$$ has no more than $$$11$$$ digits, we will assume that it is a constant time operation.

C++:

  bool isPalindrome(long long a) {
    string s = to_string(a);
    string rev(s.rbegin(), s.rend());
    return s == rev;
  }

Python:

  def isPalindrome(a):
    s = str(a)
    rev = s[::-1]
    return s == rev

Java:

  public static boolean isPalindrome(long a) {
    String s = Long.toString(a);
    StringBuilder rev = new StringBuilder(s);
    rev.reverse();
    return s.equals(rev.toString());
  }

Test Set 1

For the small test set, we can afford to find all factors of $$$\mathbf{A}$$$ by checking every integer $$$a \in \{1,2, \ldots, \mathbf{A}\}$$$. For each factor of $$$\mathbf{A}$$$, we also check if it is a palindrome and increment the answer accordingly.

The time complexity of this brute-force solution is $$$O(\mathbf{A})$$$.

Test Set 2

Let $$$a$$$ and $$$b$$$ be two factors of $$$\mathbf{A}$$$ such that $$$\mathbf{A}=ab$$$ and $$$a \le b$$$. Then $$$a \le \sqrt{\mathbf{A}}$$$. It follows that we can find all factors of $$$\mathbf{A}$$$ by checking the first $$$\sqrt{\mathbf{A}}$$$ numbers only. For each factor $$$a \le \sqrt{\mathbf{A}}$$$, the number $$$b=\frac{\mathbf{A}}{a} \ge \sqrt{\mathbf{A}}$$$ is also a factor of $$$\mathbf{A}$$$.

The time complexity of the optimized algorithm is $$$O(\sqrt{\mathbf{A}})$$$.

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — C. Unlock the Padlock

In the given problem, the range $$$[l_{i-1}, r_{i-1}]$$$ chosen in the $$$(i-1)$$$-th operation needs to be completely contained within the range $$$[l_i, r_i]$$$ chosen in the $$$i$$$-th operation; that is, $$$l_i \le l_{i-1} \le r_{i-1} \le r_i$$$. The initial range ($$$[l_1, r_1]$$$) can be chosen arbitrarily. If we reverse the order of these operations, we would get the same final combination. For the remaining analysis, we would assume the reverse order of operations, that is, $$$l_{i-1} \le l_i \le r_i \le r_{i-1}$$$. Whenever we say rotations in the analysis, it means downward by default unless mentioned otherwise.

Test set 1

In this test set, we can start from range $$$[1, \mathbf{N}]$$$ and greedily choose the side that requires lesser rotations and make it zero, and solve for the next number on that side. We should also keep track of the number of rotations done till now. Let the current range that we are trying to solve be $$$[i,j]$$$ and let the number of rotations modulo $$$\mathbf{D}$$$ be $$$k$$$. Then, the current value of index $$$i$$$ would be $$$(\mathbf{V_{i}} - k + \mathbf{D}) \enspace mod \enspace \mathbf{D}$$$. Similarly, the current value of index $$$j$$$ would be $$$(\mathbf{V_{j}} - k + \mathbf{D}) \enspace mod \enspace \mathbf{D}$$$. Let the current values of indexes $$$i$$$ and $$$j$$$ be $$$currentVal_{i}$$$ and $$$currentVal_{j}$$$ respectively. The number of rotations required to make dial at index $$$i$$$ zero would be $$$\min(currentVal_{i}, \mathbf{D} - currentVal_{i})$$$. The number of rotations required to make dial at index $$$j$$$ zero would be $$$\min(currentVal_{j}, \mathbf{D} - currentVal_{j})$$$. If index $$$i$$$ requires less number of rotations, we then add $$$\min(currentVal_{i}, \mathbf{D} - currentVal_{i})$$$ to the answer and solve for range $$$[i+1, j]$$$. Otherwise, if index $$$j$$$ requires less number of rotations, we then add $$$\min(currentVal_{j}, \mathbf{D} - currentVal_{j})$$$ to the answer and solve for range $$$[i, j-1]$$$. If the minimum number of rotations done are $$$0$$$, $$$k$$$ remains same for the subsequent range. Otherwise, we do $$$k = (k + 1) mod \mathbf{D}$$$ as we would need to do $$$1$$$ rotation. The transition is explained below in detail.

Why would the greedy approach work? Let $$$solve(i, j, k)$$$ denote the number of operations required to make all the elements in the range $$$[i,j]$$$ zero given that $$$k$$$ rotations have happened. When we are solving for range $$$[i,j]$$$, there are $$$4$$$ possible cases that could occur:

  • $$$currentVal_{i} = 0, currentVal_{j} = 0$$$. In this case, we can reduce the range from both the sides. Hence, $$$solve(i, j, k) = solve(i+1, j-1, k)$$$.
  • $$$currentVal_{i} = 1, currentVal_{j} = 1$$$. In this case, we can reduce the range from both the sides. Hence, $$$solve(i, j, k) = 1 + solve(i+1, j-1, (k + 1) mod \mathbf{D})$$$.
  • $$$currentVal_{i} = 1, currentVal_{j} = 0$$$. In this case, it is optimal to reduce the range from the right side because the value of index $$$j$$$ is already zero and we would not require any further operations. If we choose to convert index $$$i$$$ to zero, we would need one operation to convert the value of index $$$i$$$ to zero. And this would change the value of index $$$j$$$ to $$$1$$$ which may require one more operation to convert it to zero. So, we might end up taking at least $$$1$$$ more operation if we choose to make index $$$i$$$ to $$$0$$$. So, it is optimal to exclude the index $$$j$$$. Hence, $$$solve(i, j, k) = solve(i, j-1, k)$$$.
  • $$$currentVal_{i} = 0, currentVal_{j} = 1$$$. Similar explanation as above, it is optimal to exclude the index $$$i$$$. Hence, $$$solve(i, j, k) = solve(i+1, j, k)$$$.

At each step, we perform constant operations to check which index requires less number of rotations and then make that element zero. Hence, it would take $$$O(\mathbf{N})$$$ time complexity to make all the elements zero.

Test set 2

In this test set, the greedy approach would not work because the number of digits is more. We can use dynamic programming to solve this test set. Let $$$dp(i, j, k)$$$ denote the minimum number of operations required to make all elements from $$$i$$$ to $$$j$$$ equal to $$$0$$$ and $$$k$$$ be the number of rotations modulo $$$\mathbf{D}$$$ that have been done till now. Then, $$$currentVal_{i} = (\mathbf{V_{i}} - k + \mathbf{D}) \enspace mod \enspace \mathbf{D}$$$. Similarly, $$$currentVal_{j} = (\mathbf{V_{j}} - k + \mathbf{D}) \enspace mod \enspace \mathbf{D}$$$. We then solve for both the cases.

  • For converting index $$$i$$$ to $$$0$$$, we could do one of the following:
    • Rotate downwards, it would take $$$currentVal_{i}$$$ operations. We then recursively solve for $$$dp(i+1, j, (k+currentVal_{i}) \enspace mod \enspace \mathbf{D})$$$.
    • Rotate upwards, it would take $$$\mathbf{D}$$$ - $$$currentVal_{i}$$$ operations. We then recursively solve for $$$dp(i+1, j, (k - (\mathbf{D} - currentVal_{i}) + \mathbf{D}) \enspace mod \enspace \mathbf{D})$$$.
    Let $$$val_{1} = \min(currentVal_{i} + dp(i+1, j, (k+currentVal_{i}) \enspace mod \enspace \mathbf{D}), \mathbf{D} - currentVal_{i} + dp(i+1, j, (k - (\mathbf{D} - currentVal_{i}) + \mathbf{D}) \enspace mod \enspace \mathbf{D}))$$$.
  • For converting index $$$j$$$ to $$$0$$$, we could do one of the following:
    • Rotate downwards, it would take $$$currentVal_{j}$$$ operations. We then recursively solve for $$$dp(i, j - 1, (k+currentVal_{j}) \enspace mod \enspace \mathbf{D})$$$.
    • Rotate upwards, it would take $$$\mathbf{D}$$$ - $$$currentVal_{j}$$$ operations. We then recursively solve for $$$dp(i, j - 1, (k - (\mathbf{D} - currentVal_{j}) + \mathbf{D}) \enspace mod \enspace \mathbf{D})$$$.
    Let $$$val_{2} = \min(currentVal_{j} + dp(i, j - 1, (k+currentVal_{j}) \enspace mod \enspace \mathbf{D}), \mathbf{D} - currentVal_{j} + dp(i, j - 1, (k - (\mathbf{D} - currentVal_{j}) + \mathbf{D}) \enspace mod \enspace \mathbf{D}))$$$.
Finally, $$$dp(i,j,k) = \min(val_{1}, val_{2})$$$. Please take a look at the pseudocode below:

Sample Code(C++)

int solve(int i, int j, int k) {
  if(i > j) {
    return 0;
  }
  if(dp[i][j][k] != -1) {
    return dp[i][j][k];
  }
  int currentVal_i = (V[i] - k + D) % D;
  int currentVal_j = (V[j] - k + D) % D;
  // Convert index i to 0.
  int val_1 = min(currentVal_i + solve(i+1, j, (k + currentVal_i) % D), D - currentVal_i + solve(i+1, j, (k - (D - currentVal_i) + D) % D));
  // Convert index j to 0.
  int val_1 = min(currentVal_j + solve(i, j-1, (k + currentVal_j) % D), D - currentVal_j + solve(i, j-1, (k - (D - currentVal_j) + D) % D));
  return dp[i][j][k] = min(val_1, val_2);
}

We can get the final answer from $$$dp(1, \mathbf{N}, 0)$$$. There are a total of $$$O(\mathbf{N}^{2} \cdot \mathbf{D})$$$ number of states. And we perform constant operations for each state. Hence, the overall complexity of the solution is $$$O(\mathbf{N}^{2} \cdot \mathbf{D})$$$.

Test set 3

In this test set, $$$\mathbf{D}$$$ is very large, so the solution for the previous test set would time out. An interesting observation is that we do not need to keep track of the number of rotations when solving for a range $$$[i,j]$$$. If we keep track of which side (left or right) was made zero in the previous operation, we can calculate the number of rotations modulo $$$\mathbf{D}$$$ that has been done till now. The last element that becomes zero will be because of the combination of all the rotations done so far. So, let us say initial value of the element that was made zero in the previous operation was $$$x$$$. Then, total rotations applied so far have combinely made it zero. Then all numbers inside the range $$$[i,j]$$$ are affected by $$$x$$$ downward rotations.

We can then solve the current test set by having $$$dp(i,j,bit)$$$ denoting the minimum number of operations required to make all elements from $$$i$$$ to $$$j$$$ equal to $$$0$$$ given that the element that was made zero in the previous operation is on the left side if $$$bit = 0$$$, and is on the right side if $$$bit = 1$$$.

Let the number of rotations done till now modulo $$$\mathbf{D}$$$ be $$$k$$$. The value of $$$k$$$ can be determined as follows:

  • If $$$bit = 0$$$:
    • If $$$i \gt 1$$$, then $$$k = \mathbf{V_{i-1}}$$$.
    • Otherwise, $$$k = 0$$$.
  • If $$$bit = 1$$$:
    • If $$$j \lt \mathbf{N}$$$, then $$$k = \mathbf{V_{j+1}}$$$.
    • Otherwise, $$$k = 0$$$.
Then $$$currentVal_{i} = (\mathbf{V_{i}} + k) \enspace mod \enspace \mathbf{D}$$$ and $$$currentVal_{j} = (\mathbf{V_{j}} + k) \enspace mod \enspace \mathbf{D}$$$. For a particular state $$$dp(i,j,bit)$$$, we would transition to the next state as follows.
  • For converting index $$$i$$$ to $$$0$$$, it would take $$$\min(currentVal_{i}, \mathbf{D} - currentVal_{i})$$$ operations. Let $$$currOperations = \min(currentVal_{i}, \mathbf{D} - currentVal_{i})$$$. We then recursively solve for $$$dp(i+1, j, 0)$$$. Let $$$val_{1} = dp(i+1, j, 0) + currOperations$$$.
  • For converting index $$$j$$$ to $$$0$$$, it would take $$$\min(currentVal_{j}, \mathbf{D} - currentVal_{j})$$$ operations. Let $$$currOperations = \min(currentVal_{j}, \mathbf{D} - currentVal_{j})$$$. We then recursively solve for $$$dp(i, j-1, 1)$$$. Let $$$val_{2} = dp(i, j-1, 1) + currOperations$$$.
Finally, $$$dp(i,j,bit) = \min(val_{1}, val_{2})$$$. Please take a look at the pseudocode below:

Sample Code(C++)

int solve(int i, int j, bool bit) {
  if(i > j) {
    return 0;
  }
  if(dp[i][j][bit] != -1) {
    return dp[i][j][bit];
  }
  int k = 0;
  if(!bit) {
    if(i > 1) {
      k = V[i-1];
    }
  }
  else {
    if(j < N ) {
       k = V[j+1];
    }
  }
  int currentVal_i = (V[i] - (D - k) + D) % D;
  int currentVal_j = (V[j] - (D - k) + D) % D;
  // Convert index i to 0.
  int currOperations = min(currentVal_i, D - currentVal_i);
  int val_1 = currOperations + solve(i+1, j, 0);
  // Convert index j to 0.
  currOperations = min(currentVal_j, D - currentVal_j);
  int val_2 = currOperations + solve(i, j - 1, 1);
  return dp[i][j][bit] = min(val_1, val_2);
}

We can get the final answer from $$$dp(1, \mathbf{N}, 0)$$$. There are a total of $$$O(\mathbf{N}^{2})$$$ number of states. And we perform constant operations for each state. Hence, the overall complexity of the solution is $$$O(\mathbf{N}^{2})$$$.
Note that, if we solve the dynamic programming solution mentioned in Test Set 2 recursively, we will only visit $$$O(\mathbf{N}^2)$$$ states because the number of rotations from left side and right side will always be fixed. Hence, the recursive version of Test Set 2 solution where we keep track of only the states that we visit would pass this test set.

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Analysis — D. Hamiltonian Tour

Test Set 1

The regular structure of the grid allows for many simple constructions of a Hamiltonian cycle by using repeated patterns as building blocks. For example, if both $$$\mathbf{R}$$$ and $$$\mathbf{C}$$$ are even, we can define $$$P=E^{2\mathbf{C}-2}S(W^2S^2WN^2W)^\frac{\mathbf{C}-2}{2}W^2S^2$$$ and use $$$E(PS)^{\frac{\mathbf{R}}{2}-1}PWN^{2\mathbf{R}-1}$$$ as our Hamiltonian cycle. This is illustrated in the picture below, where the same pattern $$$P$$$ is used to walk between cells $$$S$$$ and $$$T$$$ or cells $$$S'$$$ and $$$T'$$$.

An example grid for Test Set 1, R = 4, and C = 6 with one potential Hamiltonian cycle.

Similar patterns can be defined when one or both of $$$\mathbf{R}$$$ and $$$\mathbf{C}$$$ are odd. For example:

  • If $$$\mathbf{R}$$$ is even and $$$\mathbf{C}$$$ is odd, use the cycle $$$E(PS)^{\frac{\mathbf{R}}{2}-1}PWN^{2\mathbf{R}-1}$$$, where $$$P=E^{2\mathbf{C}-2}S(S^2WN^2W^3)^\frac{\mathbf{C}-1}{2}S^2$$$.
  • If $$$\mathbf{R}$$$ is odd and $$$\mathbf{C}$$$ is even, use the cycle $$$EP^\frac{\mathbf{R}-1}{2}E^{2\mathbf{C}-2}SW^{2\mathbf{C}-1}N^{2\mathbf{R}-1}$$$, where $$$P=E^{2\mathbf{C}-2}S(W^2S^2WN^2W)^\frac{\mathbf{C}-2}{2}W^2S^3$$$.
  • If both $$$\mathbf{R}$$$ and $$$\mathbf{C}$$$ are odd, use the cycle $$$EP^\frac{\mathbf{R}-1}{2}E^{2\mathbf{C}-2}SW^{2\mathbf{C}-1}N^{2\mathbf{R}-1}$$$, where $$$P=E^{2\mathbf{C}-2}S(S^2WN^2W^3)^\frac{\mathbf{C}-1}{2}S^3$$$.

The time complexity of this construction is proportional to the length of the resulting cycle, which is $$$O(\mathbf{R}\mathbf{C})$$$.

Test Set 2

We are going to discuss two different approaches here. In both of them, we will use an undirected graph $$$G$$$ with the empty $$$2 \times 2$$$ blocks as nodes and two blocks connected by an edge if and only if the blocks share a side. If the graph is disconnected, then the answer is IMPOSSIBLE, as there are empty cells that are not even reachable from the cell $$$A_{1,1}$$$. We will see that a Hamiltonian cycle exists if $$$G$$$ is connected.

Merging Cycles

The idea in this approach is to perform a depth-first search (DFS) on the graph $$$G$$$, starting at the top-left block, and maintain a cycle traversing all four empty cells of every visited $$$2 \times 2$$$ block. The cycle is extended incrementally as more blocks are visited by the DFS. A detailed description follows.

First, let us connect the four cells of each empty $$$2 \times 2$$$ block in a clockwise cycle of length four. Technically, it can be implemented by maintaining pointers next[i,j], where next[i,j]=={p,q} if the cell $$$A_{p,q}$$$ follows the cell $$$A_{i,j}$$$ in a cycle.

The following picture shows the construction for the third example test case.

The image shows the grid of the third example test case, where the empty cells of each
          2x2 block are connected in a clockwise cycle.

Now perform a DFS on $$$G$$$ starting at the top-left block. During the search, we maintain a single cycle containing all four cells of all empty blocks that are visited by the DFS so far. Let us call it the main cycle, which is initially the small $$$4$$$-cycle of the top-left block. As we visit a new block $$$X$$$ from its parent block $$$P$$$ in the DFS tree, we merge the $$$4$$$-cycle of $$$X$$$ into the main cycle at the common side between blocks $$$P$$$ and $$$X$$$. The merging operation can be implemented in constant time by reconnecting two pointers. For example, if $$$X$$$ has $$$A_{i,j}$$$ as its top-left cell and $$$X$$$ is on the right of $$$P$$$, then the cycles can be merged by setting next[i][j-1]={i,j} and next[i+1][j]={i+1,j-1}.

The following picture illustrates the merging operations for our example.

The image shows the merging operations for the third example test case.

Since cycles can be merged in constant time and DFS is a linear time algorithm, the overall time complexity of this approach is $$$O(\mathbf{R}\mathbf{C})$$$.

Following the Walls

If you are familiar with the Wall follower algorithm for escaping a maze, the same intuition can be applied directly to construct the Hamiltonian tour in our grid. The idea is to keep walking while sliding one hand along the wall without ever loosing contact with the wall. In the context of a maze, you would either exit the maze eventually or return to the starting position. Similarly, if we were to use the wall follower algorithm in our grid of Hamilton starting at the top-left cell $$$A_{1,1}$$$, we would eventually return to that cell and thus form a cycle.

If $$$G$$$ is a tree, then every empty cell is touching a wall at least at a corner. Therefore, the wall follower algorithm would necessarily visit every empty cell and result in a Hamiltonian cycle.

But what if $$$G$$$ is not a tree? We can construct a spanning tree $$$T$$$ of $$$G$$$ and introduce thin walls between $$$2 \times 2$$$ blocks that are connected in $$$G$$$, but not in the tree $$$T$$$. And again, using the wall follower algorithm would yield a Hamiltonian cycle.

This whole construction is illustrated in the following picture. The spanning tree $$$T$$$ of $$$G$$$ is marked with blue lines, the wall being followed is outlined in red, and the green cycle is the resulting Hamiltonian tour.

The image shows a spanning tree of G and the Hamiltonian tour constructed by the wall follower algorithm.

The time complexity of both the spanning tree construction and the wall follower algorithm is $$$O(\mathbf{R}\mathbf{C})$$$.

Test Data
info We recommend that you practice debugging solutions without looking at the test data.

Statistics — A. Infinity Area

Statistics — B. Palindromic Factors

Statistics — C. Unlock the Padlock

Statistics — D. Hamiltonian Tour