I invite everyone to the contest, which will take place on January 11, 2025 at 14:35UTC. Interesting problems await you in the contest.
Thanks to MathModel for ideas for several problems and Timosh for inspiring ideas.
Contest info:
Number of problems: 13
Difficulty of problems: < 2400
Duration: 3 hours
Contest type: ACM20M
Languages: C++, Python, C, Kotlin, Haskell, R, Nodejs, PHP, C#, Java, Rust, Go
Prizes:
First place – 25$ (USDT)
Second place – 20$
Third place – 15$
Fouth place – 12$
Fifth place – 10$
Link to the platform: https://kep.uz/competitions/contests/
P.S. Registration on the platform only via Gmail/Github.
UPD
Thank you for participating. I hope you enjoyed the contest.
Results
- physics0523 — 13 (398)
- shnirelman — 13 (410)
- jeroenodb — 13 (445)
- liympanda — 13 (454)
- vtb — 13 (454)
- Sunnatov — 13 (467)
- MDSPro — 13 (487)
- iakovlev.zakhar — 13 (583)
- bashkort — 12 (398)
Editorial
Check if $$$r$$$ is less than $$$1200$$$: if true, output Noob
; otherwise, output Not a noob
.
Check if $$$r < 1200$$$: if true, output $$$1200$$$
If $$$r = 10000$$$ or $$$r = 9999$$$, output $$$100000$$$
Otherwise, output $$$r + 1$$$, which guarantees the conditions are met
Iterate over $$$k$$$ from $$$1$$$ to $$$X$$$. For each $$$k$$$, check if $$$X - k = S(Y + k)$$$ (where $$$S$$$ is the sum of the digits)
Output the smallest $$$k$$$ that satisfies the condition or $$$-1$$$ if no such $$$k$$$ exists
Calculate the total scores for all players. Check if the highest score is unique by counting its occurrences. If the count is 1, print YES
; otherwise, print NO
.
Short Solution Explanation
- We have $$$P_m = 2^{\frac{2^m - 1}{k}}$$$.
- $$$P_m$$$ is integer $$$\iff \frac{2^m - 1}{k} \in \mathbb{Z} \iff 2^m \equiv 1 \pmod{k}.$$$
- If $$$k=1$$$, answer $$$m=1.$$$
- If $$$k>1$$$ is even, then $$$2^m-1$$$ is odd and cannot be divisible by $$$k,$$$ so $$$m$$$ does not exist ($$$-1$$$).
- Otherwise, we find the minimal $$$m$$$ such that $$$2^m \equiv 1 \pmod{k}.$$$
- Iterate $$$m=1,\dots,k.$$$
- Check if $$$2^m \bmod k=1.$$$
- The first such $$$m$$$ is the answer; if none, output $$$-1.$$$
Explanation with MathJax
Let’s maintain a function $$$f(n, m)$$$ to decide if a pair is good or not.
Case 1: Odd values of $$$n$$$
For odd values of $$$n$$$, we can re-express the string $$$s$$$ as:
The sum:
and the middle digit can be denoted as $$$h$$$.
Thus, the total sum is:
Since $$$h$$$ can be any digit, the sum $$$2f + h$$$ lies between the minimum and maximum possible values, inclusive.
Conditions for a "good" pair: [ \begin{cases} n \leq m \leq 9n, \ n \mod 2 = 1, \end{cases} ] then the pair is YES.
Case 2: Even values of $$$n$$$
For even values of $$$n$$$, we re-express the string $$$s$$$ as:
Using symmetry:
Let $$$f$$$ denote the left and right segments. The sum of medians is $$$2h$$$, and the total becomes:
For even $$$n$$$, the sum must also satisfy the condition of being even.
Conditions for a "good" pair: [ \begin{cases} n \leq m \leq 9n, \ (n + m) \mod 2 = 0, \end{cases} ] then the pair is YES.
Complexity
For the easy version, we can iterate through all pairs $$$(a, b)$$$, and the complexity will be: [ \mathcal{O}(n^2). ]
Optimized Solution
We can iterate for each $$$a$$$ from $$$1$$$ to $$$n$$$. This allows us to count valid answers:
For odd $$$a$$$, all answers in $$$[a, 9a]$$$ are valid.
Thus, there are $$$8a + 1$$$ valid answers for odd $$$a$$$.For even $$$a$$$, only even indices in $$$[a, 9a]$$$ are valid.
Thus, for even $$$a$$$, we add $$$4a + 1$$$ valid answers.
Complexity
The total complexity is:
Optimized Solution with Closed Formula
From the Medium version, we obtained the following conditions: [ \begin{cases} 8a + 1, & a \mod 2 = 1 \ 4a + 1, & a \mod 2 = 0 \end{cases} ]
Step 1: Sum for Odd Numbers
For odd numbers, there are $$$\left\lceil \frac{n}{2} \right\rceil$$$ odd integers. We want to calculate:
Using the formula for the sum of the first $$$m$$$ odd numbers ($$$m^2$$$), we find that the count for odd numbers is:
Step 2: Sum for Even Numbers
For even numbers, there are $$$\left\lfloor \frac{n}{2} \right\rfloor$$$ even integers. We want to calculate:
Using the formula for the sum of the first $$$m$$$ even numbers ($$$m(m + 1)$$$), we find that the count for even numbers is:
Final Formula
Combining the results for odd and even numbers, the final formula is:
Complexity
This solution has a complexity of:
To solve the problem of transforming an array into an "almost sorted" array with minimal operations, the solution uses a dynamic programming approach combined with a balanced structure for optimization. Here's a breakdown:
Explanation:
- Dynamic Programming Approach:
- We iterate over the array while maintaining a
dp
array, wheredp[i]
stores the minimal operations needed to fix the array up to indexi
. - For each element, calculate the operations needed to ensure it is at least as large as the previous element. If it's smaller, add the difference to the operations count.
- Efficient Tracking of State:
- To minimize redundant calculations for future indices, we maintain a data structure (
set
) that efficiently tracks previous adjustments and allows merging ranges.
- Optimization with Sliding Window:
- The algorithm uses a sliding window over the array while calculating the total adjustments needed (
now
) for transforming the remaining elements into valid states. - By combining the
dp
values with the adjustments tracked in theset
, the solution finds the minimal operations.
Key Functions:
get(l, r)
: Calculates the sum of integers froml
tor
. This helps in efficient range operations.maxs
andmins
: Utility functions to update values efficiently while iterating.
Complexity:
- Time Complexity: $$$O(nlogn)$$$, due to the use of a balanced structure (
set
) for maintaining state. - Space Complexity: $$$O(n)$$$, for storing the
dp
array and state.
Implementation:
The solution calculates the minimal operations by: 1. Iterating through the array and updating dp
. 2. Using a set
to manage adjustments dynamically. 3. Combining results from dp
and the tracked state to compute the answer.
Conclusion:
This approach ensures efficient computation for large arrays (( n \leq 5 \times 10^6 )) while maintaining correctness. The use of dynamic programming and an optimized state-tracking mechanism makes the solution both elegant and scalable.
Key Observations
- The sum of cubes can be expressed as:
For large values of $$$r$$$, the sum can quickly grow to exceed $$$n$$$, so efficient calculations are critical.
- The sum of cubes from $$$1$$$ to $$$x$$$ is given by:
Using this formula, the sum $$$S(l, r)$$$ can be computed as:
- For each $$$l$$$, the problem is reduced to finding the smallest $$$r$$$ such that $$$S(l, r) = n$$$.
Algorithm
Step 1: Precomputation of Sum Function
- Define a helper function $$$s(x)$$$ that computes:
This function avoids overflow checks by using conditionals to cap results at $$$2 \times 10^{18}$$$.
Step 2: Binary Search for $$$r$$$
- For a fixed $$$l$$$, use binary search to find $$$r$$$ such that $$$S(l, r) = n$$$:
- Calculate $$$S(l, r)$$$ directly using $$$s(r)$$$ and $$$s(l-1)$$$.
- If $$$S(l, r) < n$$$, increase $$$r$$$.
- If $$$S(l, r) > n$$$, decrease $$$r$$$.
Step 3: Iterate Over $$$l$$$
- Iterate over $$$l$$$ from $$$1$$$ to $$$\sqrt[3]{n}$$$ (approximation of possible lower bounds).
- Use binary search for each $$$l$$$ to find a valid $$$r$$$.
Implementation
Key Functions
s(x)
: Calculates $$$\left( \frac{x(x+1)}{2} \right)^2$$$ with overflow checks.f(l, r)
: Calculates $$$S(l, r)$$$ using the formula:
Includes overflow checks.
- Binary Search: Finds $$$r$$$ for a given $$$l$$$ such that $$$S(l, r) = n$$$.
Key Observations
- Cubic Sum Representation: The sum of cubes can be expressed as:
Here, $$$\text{prefix_sum}[i]$$$ stores the sum of cubes from $$$1$$$ to $$$i$$$.
- Two-Pointer Technique: By maintaining two pointers, $$$l$$$ and $$$r$$$, and using the prefix sums:
- Increase $$$r$$$ when the sum is less than $$$n$$$.
- Increase $$$l$$$ when the sum exceeds $$$n$$$.
- Stop when the sum equals $$$n$$$ or when $$$r$$$ exceeds the maximum limit.
- Precomputation: Precompute the cubes and their prefix sums for all integers up to $$$10^6$$$:
This allows efficient calculation of the sum of cubes for any segment $$$[l, r]$$$.
Algorithm
Precomputation: Precompute the cubes and their prefix sums up to $$$10^6$$$ in $$$\mathcal{O}(n)$$$ time.
Two-Pointer Search: For each query:
- Initialize $$$l = 1$$$, $$$r = 1$$$.
- Use the two-pointer approach to find a range $$$[l, r]$$$ such that the sum equals $$$n$$$:
- If the sum of cubes from $$$l$$$ to $$$r$$$ equals $$$n$$$, output $$$l$$$ and $$$r$$$.
- If the sum is less than $$$n$$$, increment $$$r$$$.
- If the sum is greater than $$$n$$$, increment $$$l$$$.
- If no such range is found, output $$$-1$$$.
- Complexity:
- Precomputation takes $$$\mathcal{O}(n)$$$ for $$$n = 10^6$$$.
- Each query runs in approximately $$$\mathcal{O}(n)$$$ due to the two-pointer traversal.
- Total complexity: $$$\mathcal{O}(n + qt)$$$, where $$$q$$$ is the number of queries.
GCD Problem Explanation
Key Idea:
- Fix the leftmost element of the segment.
- The greatest common divisor (GCD) of (A[l, r)) changes at most (\log A_i) times.
- This is because the GCD value decreases as (r) increases and can only take specific values that divide the elements in the segment.
Example:
Consider the array:
Fix the leftmost element of the segment, (A[l] = 12).
Now, calculate the GCD for increasing values of (r):
- $$$ \text{GCD}(12) = 12 ) for \; r = 1, 2 $$$
- $$$ \text{GCD}(12, 18, 30) = 6 ) for \; r = 3, 4, 5 $$$
- $$$ \text{GCD}(12, 18, 30, 36, 16) = 2 ) for \; r = 6, 7 $$$
- $$$ \text{GCD}(12, 18, 30, 36, 16, 6, 9) = 1 ) for \; r = 8 $$$
Marked positions where the GCD changes: ( r = 2, 5, 7, 8 ).
Observations:
- If $$$[1, \text{marked}]$$$ is completely contained within a query, we can ignore the unmarked rightmost elements.
- This allows us to reduce the number of checks.
Efficient Implementation:
- Use a GCD segment tree to manage the structure of the array and determine the GCD for any segment efficiently.
- Sort the queries by $$$l_i$$$ in descending order $$$( l = N, N-1, \ldots, 1 )$$$.
- Utilize a max segment tree to answer the queries:
- First, check the query range $$$[l_i, r_i]$$$.
- Then, verify the GCD change points (marked positions) for fixed $$$r_i$$$.
Complexity:
The time complexity is:
where: - $$$N$$$: Length of the array. - $$$Q$$$: Number of queries. - $$$A_i$$$: Maximum value in the array.