Problem A. Bipartite graph
One of the definitions of a bipartite graph is as follows. A graph is bipartite if its vertices can be divided into two sets so that there are no edges inside each of the sets. For example, for a graph of 4 vertices and edges 1 — 2, 2 — 3, 3 — 4, 4 — 1, vertices 1 and 3 fall into one set, vertices 2 and 4 into the second set, and each of the edges connects vertices from different sets .
Suppose that one set of the bipartite graph contains $$$x$$$ vertices, then the second set contains $$$n - x$$$ vertices, where $$$n$$$ is the number of vertices in the graph. The maximum number of edges that such a graph can have is $$$x (n - x)$$$, this number can be obtained by linking all pairs of vertices from different sets.
The optimal value of $$$x$$$ could be chosen by enumeration of all options (since $$$1 \le n \le 300$$$), but we can notice the following fact that the value of $$$x$$$, for which the number of edges is maximum — $$$\lfloor \frac{n}{2} \rfloor$$$. You can prove this yourself as an exercise, link for help.
As an additional task, you can try to solve the following problem: given an undirected graph of $$$n$$$ $$$(1 \le n \le 10^4)$$$ vertices and $$$m$$$ edges $$$(0 \le m \le 10^5)$$$, it is guaranteed that he is bipartite. What is the maximum number of edges that can be added to it so that it remains bipartite and does not have multiple edges? Ways to solve this and the following additional tasks can be discussed in the comments.
Problem B. EPL 20/21
Correct answers to the questions from the problem statement:
FRA
38
Manchester City
Harry Kane
Tottenham Hotspur
There are many ways to solve the problem, one of them is using the Pandas library in Python. Using this library, you can read the input data and calculate the answers to questions from the condition. It was also possible to read data in C++, Python or any other programming language and parse it in any way.
Problem C. Dice
Let us first calculate the probability that there were $$$n$$$ throws of the dice in total, and each throw would result in a number not greater than $$$d$$$. The probability of such an outcome is $$$(\frac{d}{6})^n$$$. If we subtract from this value the probability that for all throws a value is strictly less than $$$d$$$, we get the required probability. The probability that for all $$$n$$$ rolls a number strictly less than $$$d$$$ comes up is $$$(\frac{d - 1}{6})^n$$$.
Then the answer to the problem is $$$(\frac{d}{6})^n$$$ — $$$(\frac{d - 1}{6})^n$$$.
Another way to solve it is with dynamic programming. $$$dp[i][j]$$$ — the probability that $$$i$$$ rolls were made and the maximum rolled number is equal to $$$j$$$. Let's iterate over the value of the dice for the $$$(i + 1)$$$-th throw, let's assume it is equal to $$$d$$$. We need to update the dynamic programming state in $$$dp[i + 1][max(j, d)]$$$ by adding the value of $$$dp[i][j]$$$ divided by 6 (the probability of each number coming up is $$$\frac{1}{6}$$$).
Also in the task there was a group of tests, estimated at 1 point and with restrictions of $$$200 \le n \le 1000$$$. For such restrictions, for $$$1 \le d \lt 6$$$ one can output 0, and for $$$d = 6$$$ one can output 1. It can be shown that the responses tend to 0 and 1 under such constraints.
Problem D. Function graph
Response to the $$$i$$$-th query $$$-$$$ the value of the integral $$$\int_{L_i}^{R_i}4 - (sin(x) cos(\frac{x}{2})+cos(x) (cos(2x)+1)) dx$$$
$$$\int4 - (sin(x) cos(\frac{x}{2})+cos(x) (cos(2x)+1))dx$$$ = $$$\int4dx - \int cos(\frac{x}{2})sin(x)dx - \int(cos(2x)+1)cos(x) dx$$$
Let's use the following formulas:
$$$sin(\alpha)cos(\beta) = \frac{sin(\alpha + \beta) + sin(\alpha - \beta)}{2}$$$, $$$cos(\alpha)cos(\beta) = \frac{cos(\alpha + \beta) + cos(\alpha - \beta)}{2}$$$
Then, $$$\int4dx - \int cos(\frac{x}{2})sin(x)dx - \int(cos(2x)+1)cos(x) dx$$$ = $$$\int4dx - \int \frac{1}{2} sin(\frac{3x}{2})dx - \int \frac{1}{2} sin(\frac{x}{2})dx - \int \frac{3}{2} cos(x)dx - \int \frac{cos(3x)}{2} dx$$$ = $$$4x + \frac{cos(\frac{3x}{2})}{3} + cos(\frac{x}{2}) - \frac{3}{2}sin(x) - \frac{sin(3x)}{6} + constant$$$.
Denote $$$f(a)$$$ = $$$\int_{0}^{a}4 - (sin(x) cos(\frac{x}{2})+cos(x) (cos(2x)+1))dx = 4a - \frac{3sin(a)}{2} - \frac{sin(3a)}{6} + cos(\frac{a}{2}) + \frac{cos(\frac{3a}{2})}{3}$$$
Then the answer to the $$$i$$$-th query is $$$f(r_i) - f(l_i)$$$.
Problem E. Determinant
Since it is known that the missing number is in the segment $$$[-20, 20]$$$ and is an integer, we can go through all the options for the missing number and calculate the determinant of the matrix for each option. The matrix determinant can be calculated, for example, using Gaussian method.
Another solution to the problem is as follows. Following the Leibniz formula, you can go through all possible permutations and divide all terms, one can enumerate all possible permutations and divide all the terms, one of the factors of which is the missing number (let's denote the sum of these terms as $$$a$$$) and into terms that are counted without taking into account the missing number (let's denote this term as $$$b$$$). Denote the missing number $$$x$$$, then $$$ax + b = 0$$$, hence $$$x = -\frac{b}{a}$$$.
Problem F. abacaba
It can be seen that the first time the $$$i$$$-th letter of the Latin alphabet occurs at position $$$2^{i - 1}$$$ and will continue to occur with a step of $$$2^i$$$. For example, the letter 'b' will first occur at $$$2^1 = 2$$$ positions and will occur at positions 2, 6, 10, 14, ...
Thus, to answer the $$$i$$$-th query, you need to find the first and last occurrence of a character and, knowing the step for the character, calculate the total number of occurrences. The first and last occurrences can be done with simple arithmetic, we'll leave that as an exercise.
Problem G. Maximum flow
The main fact in the problem is that the given graph is a tree. As you know, in a graph that is a tree, there is exactly one path between any pair of vertices.
Then it is easy to see that the answer to the $$$i$$$-th query in the problem is the value of the minimum throughput on all edges of the path between the vertices $$$u_i, v_i$$$.
The minimum value on the path can be found using the DFS or BFS algorithms. Such a solution will have $$$O(n)$$$ asymptotics for each of the queries, the total asymptotics is $$$O(nq)$$$. This decision allows you to score 5 points in the specified restrictions.
For finding the minimum value in a path, there are several faster solutions, such as using Lowest common ancestor. This idea allows us to find the minimum throughput on the path in $$$O(\text{log n})$$$ or $$$O(1)$$$ operations (depending on the implementation). The following links may be useful for a $$$O(\text{log n})$$$ solution: link, link, link, link and link.
Problem H. Partitions
Let's denote $$$f_i$$$ — the number of ways the number $$$i$$$ as a sum of powers of 2, $$$f_0 = 1$$$. Let's consider two cases — when $$$i$$$ is odd and when $$$i$$$ is even.
For the case of odd $$$i$$$, one can notice that in all partitions of this number one of the terms will be equal to 1, so the number of partitions of the number $$$i$$$ in this case is equal to the number of partitions of the number $$$i - 1$$$. Thus $$$f_i = f_{i - 1}$$$ for odd $$$i$$$.
For the even case $$$i$$$, one can notice that all partitions can be divided into two parts — partitions in which all terms are even and partitions in which at least one term is equal to 1. Thus $$$f_i = f_{i - 1} + f_ {\frac{i}{2}}$$$.
The asymptotic behavior of the described solution is $$$O(n)$$$.
Problem I. Triplets of numbers
Denote $$$G(n)$$$ — the mask of digits that occur in the number $$$n$$$. The $$$i$$$-th bit of the mask is equal to one if the number $$$n$$$ contains the digit $$$i$$$ at least once. The value of the function $$$G(n)$$$ for natural numbers does not exceed $$$2^{10} - 1$$$.
Denote $$$F(n, mask)$$$ — the number of natural numbers from 1 to $$$n$$$ for which $$$G(n) = mask$$$. The value of $$$F(n, mask)$$$ can be calculated using digit dynamic programming as follows. Let $$$f_{l, mask, c}$$$ be the number of natural numbers of length equal to $$$l$$$ for which the value of the function $$$G(x)$$$ is equal to $$$mask$$$. $$$c$$$ takes values from 0 to 2 and denotes whether the number formed from the first $$$len$$$ digits of $$$n$$$ is greater than, equal to, or less than the numbers in question, respectively. For example, if $$$n = 12345$$$, then for the numbers $$$11$$$, $$$1099$$$ the value of $$$c$$$ is 0, for the numbers $$$1, 12, 123$$$ the value of $$$c$$$ is 1, and for the numbers $$$5, 55, 9, 124$$$ the value of $$$c$$$ is 2. The transitions in the described DP are as follows. It is necessary to iterate over the digit that will be set to $$$l + 1$$$, set $$$mask$$$ to one if the corresponding digit has not been encountered and update the value of $$$c$$$ (you can see $$$c$$$ can only change if it was equal to one). In other words, for some digit $$$d$$$ it is necessary to update the state of $$$f_{l + 1, mask', c'}$$$.
You can read about digit dynamic programming and examples of classical tasks, for example, at this or this links.
Once $$$F(n, mask)$$$ is known for all possible $$$mask$$$ values, the inclusion-exclusion principle can be applied to count the number of matching triples. The answer is constructed as follows. Let's count the number of ways to take exactly three numbers for which the digit 0 occurs at least once, the digit 1 occurs at least once, and so on. You may notice that numbers were added to the sum several times in which the digits 0 and 1 occur at the same time, so you need to subtract this amount, etc. To do this, it is necessary to iterate over all possible values of $$$mask$$$, and if the number of units in the binary notation is odd, the summand must be included in the total sum with a plus sign, otherwise with a minus sign.
It is also worth mentioning that the number of correct triples of numbers in which a certain set of digits occurs can be calculated as follows. Let's denote $$$k$$$ — the number of numbers with a fixed set of identical digits. Then we can choose three numbers from the set in exactly $$$\frac{k(k-1)(k-2)}{6} = C_k^3$$$ ways.
Problem J. K-th path
You can see that each path will be $$$2n - 1$$$ long and each path can be represented as a $$$(2n - 1)$$$-digit number.
Denote $$$F(x)$$$ — the number of paths from the cell $$$(1, 1)$$$ to the cell $$$(n, n)$$$, the value of the number on which does not exceed $$$x$$$. It is easy to see that $$$F(x) \le F(x + 1)$$$. The value of the function $$$F(x)$$$ can be calculated using dynamic programming as follows.
Denote $$$f_{i,j,t}$$$ — how many ways there are to get from cell $$$(1, 1)$$$ to cell $$$(i, j)$$$, constructing a number of length $$$i + j - 1$$$ and which is less (if $$$t = 0$$$) or equal (if $$$t = 1$$$) to the number formed from the first $$$i + j - 1$$$ digits of $$$x$$$. There are two different transitions — to the cell $$$(i + 1, j)$$$ and to the cell $$$(i, j + 1)$$$. Since the number of ways can be huge, it is necessary to replace large values with some number, obviously greater than $$$k$$$ from the condition, so as not to catch the overflow.
Then, using binary search, we can find the answer, that is, the minimum number $$$x$$$ such that $$$F(x) \ge k$$$.
A similar problem for reflection — how to solve a similar problem, only if each cell of the matrix would be a letter of the Latin alphabet and it was necessary to find the $$$k$$$-th lexicographically string in ascending order?
Problem K. Tic-tac-toe game
The problem also has many different solutions.
One of the solutions involves generating a random matrix, in which there will be a specified number of crosses and a specified number of zeros, and using random symbol exchanges in the matrix, recalculate the number of correct submatrices, trying to improve the arrangement (approaching the correct answer). You can do several of these iterations to get the best solution. The local search method was described above, you can read about it https://en.wikipedia.org/wiki/Local_search_(optimization). Also, the problem could be solved with the help of a genetic algorithm, link or link for self-study.
Any questions about the tasks can be asked in the comments.