You are on page 1of 2

Problem А.

Editorial:
In this task, we need to count the number of vowels in a given string. We start by
reading the string. If using C++, it is recommended to use the getline() function, as
the string may contain spaces. After obtaining the string, we initiate a loop that
iterates through each of its characters. To keep track of the number of vowels, we
introduce a variable, let's say 'count,' with an initial value of 0. During the loop
iteration, each character is checked to determine if it is a vowel using a conditional
statement. If the character turns out to be a vowel, the count is incremented by one.
Upon completion of the loop, the value of count reflects the total number of vowels
in the string, which is then output as the result.

Author's solution: https://pastebin.com/BWTdNuxp

Problem B.

Algorithm Analysis:

Number Iteration: Iterate through 100 numbers to the left and right of the
given number.

Digit Sum Calculation: Determine the sum of digits for each of these
numbers.

Prime Check: Verify if the obtained sum is a prime number.

Selecting the Nearest Prime Sum: Choose the prime sum closest to the given
number from all the prime sums.

Special Case: If the sum of digits of the original number is prime, the answer
will be 0.

Proof of Correctness:
Maximum Digit Sum: Consider the maximum possible digit sum. For the
number 999999999999999999, the digit sum is 162.

List of Prime Numbers: List all prime numbers less than 162.

Analysis of the Difference Between Prime Numbers: The largest difference


between adjacent prime numbers in this list is 14 (between 113 and 127).

Worst-Case Estimate: In the worst case, when the digit sum of the number is
120, the nearest prime number will be at a distance of 7.

Conclusion: By iterating through numbers in the range of +/-100 from the


original number, it is always possible to find a number with a prime digit sum.

Note: Although the number of iterated numbers can be reduced, it is not necessary
for this task due to its conditions.
Author's solution: https://pastebin.com/yYKjvSn5

Problem С:
Editorial: First, we find the leftmost number, let it be x, for which the condition L <= x * (x + 1)
<= R holds. Now, we also find the rightmost number, let it be y, for which the condition L <= y
* (y - 1) <= R holds. The answer will be y - x. This is because any multiplication of neighboring
numbers within the range [x; y] will be within the range [L; R]. The values of x and y can be
found using binary search or by solving a quadratic equation.

Author's solution: https://pastebin.com/dZRc8pg1

Problem D:
Editorial:
This problem could be solved in many ways, but I will discuss the simplest one.
It is always optimal when teams do not play a draw, as it earns fewer points than a
win for any team (1 + 1 < 3). From this, we can conclude that the first team won a / 3 times,
the second team won b / 3 times. And their remainders are draws, which must be equal, as
teams earn the same number of points in a draw.

Author's solution: https://pastebin.com/xFiATATw

Problem E:
Editorial:
For each prefix, calculate the occurrence count for each letter. To determine the
number of occurrences of the letter 'a' in the range from L to R, subtract the count of
'a' occurrences at prefix L-1 from their count at prefix R. Two situations are possible:
the count of letter occurrences is even or odd. If a letter occurs an even number of
times, it does not affect the formation of a palindrome substring. However, if the
count is odd, the letter must become the central symbol of the palindrome.
Therefore, a palindrome in the range from L to R is impossible if two or more letters
occur an odd number of times. A palindrome is possible if all letters occur an even
number of times or only one letter occurs an odd number of times.

For each prefix, keep track of letters occurring an odd number of times. Check if the
same set of letters occurring an odd number of times is present on prefix j as on
prefix i. If yes, then all letters occur an even number of times in the range from j + 1
to i. Also, check prefixes that differ by one character. For each i, add the count of
corresponding prefixes to the answer. Count the determined prefixes using a data
structure like a dictionary (dict in Python, map in C++).

The time complexity of this solution is O(26 * N * logN). The author's solution
includes an optimization with the 'mask' variable, where each bit corresponds to the
evenness of the occurrence count of a particular letter. This optimization reduces
the time complexity to O(N * logN).

Author's solution: https://pastebin.com/tzUEMbEp

You might also like