brain

tamnd's digital brain — notes, problems, research

43815 notes

CF 440A - Forgotten Episode

We are asked to determine which episode Polycarpus has not watched in a season of a TV show. He has watched n - 1 episodes out of a total of n, each numbered consecutively from 1 to n.

codeforcescompetitive-programmingimplementation
CF 439B - Devu, the Dumb Guy

We are asked to teach Devu a set of subjects, each consisting of a certain number of chapters. Devu starts with a fixed amount of time required per chapter, and after completing each subject, the time per chapter decreases by exactly one hour for the next subject, down to a…

codeforcescompetitive-programmingimplementationsortings
CF 439C - Devu and Partitioning of the Array

We are given an array of distinct integers, and we need to split it into exactly k non-empty groups. Among these groups, exactly p must have an even sum, and the remaining k - p must have an odd sum.

codeforcescompetitive-programmingbrute-forceconstructive-algorithmsimplementationnumber-theory
CF 439E - Devu and Birthday Celebration

We are distributing a total of n identical sweets into f distinct friends, with the rule that every friend must receive at least one sweet. So the outcome of a distribution can be viewed as an ordered array a1, a2, ..., af of positive integers whose sum is n.

codeforcescompetitive-programmingcombinatoricsdpmath
CF 437A - The Child and Homework

The task is to simulate how a child chooses an answer on a multiple-choice question with four options, labeled A through D. Each option has a textual description.

codeforcescompetitive-programmingimplementation
CF 437D - The Child and Zoo

We are given a connected undirected graph where each node represents a zoo area and carries a value describing how many animals live there. For any ordered pair of distinct areas $p$ and $q$, we look at all simple paths connecting them.

codeforcescompetitive-programmingdsusortings
CF 437B - The Child and Set

We are asked to reconstruct a set of distinct integers from 1 to a given upper bound, such that the sum of a special function applied to each element equals a target value. The special function, lowbit(x), extracts the lowest set bit of x in its binary representation.

codeforcescompetitive-programmingbitmasksgreedyimplementationsortings
CF 436A - Feed with Candy

We have up to 2000 candies. Each candy has a type, either 0 or 1, a required jump height, and a mass. Om Nom starts with jump power x. He may eat any uneaten candy whose height is at most his current jump power. After eating a candy with mass m, his jump power increases by m.

codeforcescompetitive-programminggreedy
CF 436F - Banners

We are asked to optimize revenue from a mobile app that has both a free version with ads and a paid version without ads.

codeforcescompetitive-programmingbrute-forcedata-structuresdp
CF 436E - Cardboard Box

Each level can end up in one of three states. State 0 means we ignore it and gain no stars. State 1 means we complete it for one star and spend a[i] time. State 2 means we complete it for two stars and spend b[i] time.

codeforcescompetitive-programmingdata-structuresgreedy
CF 436C - Dungeons and Candies

We are given k game levels. Each level is an n × m grid of characters. A level can be transmitted in two different ways. The first option is to send the entire grid from scratch. Since every cell must be transmitted, the cost is n m.

codeforcescompetitive-programmingdsugraphsgreedytrees
CF 436D - Pudding Monsters

We have monsters placed on an infinite integer line. Consecutive monsters immediately stick together and form a block. A move chooses one entire block and slides it left or right until it collides with another block. After the collision, the two blocks merge.

codeforcescompetitive-programmingdp
CF 436B - Om Nom and Spiders

We are asked to compute how many spiders Om Nom sees if he starts walking from each cell in the top row of a rectangular park. The park is represented as an n × m grid where some cells contain spiders with an initial direction: left, right, up, or down.

codeforcescompetitive-programmingimplementationmath
CF 435D - Special Grid

We are given a rectangular grid with n rows and m columns. Each intersection of horizontal and vertical lines - each "node" - is colored either black or white. Additionally, every unit square in the grid has diagonals drawn.

codeforcescompetitive-programmingbrute-forcedpgreedy
CF 435E - Special Graph

We have an $n times m$ grid of vertices. Two vertices are connected if they share a side, and also if they are opposite corners of the same unit square. In other words, every cell contributes all four edges of the square plus both diagonals.

codeforcescompetitive-programming
CF 435B - Pasha Maximizes

We are given a positive integer, which we can treat as a string of decimal digits, and a maximum number of allowed adjacent swaps, k. The goal is to transform this number into the largest possible number by rearranging digits, but each move can only swap two neighboring digits.

codeforcescompetitive-programminggreedy
CF 435C - Cardiogram

The input describes a polyline that alternates between rising and falling diagonal segments. The length of the $i$-th segment is $ai$.

codeforcescompetitive-programmingimplementation
CF 433B - Kuriyama Mirai's Stones

We have a sequence of stones, each with a numeric cost. Kuriyama Mirai wants to ask two types of questions repeatedly: in the first type, she asks for the sum of the costs of stones in a contiguous segment of the original sequence; in the second type, she asks for the sum of…

codeforcescompetitive-programmingdpimplementationsortings
CF 433E - Tachibana Kanade's Tofu

We are asked to count numbers in a given range [l, r] (expressed in base m) that satisfy a certain “value” constraint. Each number starts with value zero. We are given n patterns, each a sequence of digits in base m, with an associated integer value.

codeforcescompetitive-programmingdp
CF 433C - Ryouko's Memory Note

The notebook pages are numbered from 1 to n. The sequence a describes the order in which Ryouko will read information. If two consecutive pieces of information are on pages a[i] and a[i+1], she must turn The total effort is the sum of these distances over all consecutive pairs.

codeforcescompetitive-programmingimplementationmathsortings
CF 433D - Nanami's Digital Board

We have a dynamic binary grid. A cell containing 1 is lit, a cell containing 0 is dark. Two kinds of operations appear. A modification flips one cell. A query asks for the largest all-1 rectangle whose border contains a given cell (x, y). The cell does not need to be a corner.

codeforcescompetitive-programmingdsuimplementation
CF 433A - Kitahara Haruki's Gift

We are given a collection of apples where every apple weighs either 100 grams or 200 grams. All apples must be distributed between two people, and each apple must go entirely to one person because apples cannot be cut.

codeforcescompetitive-programmingbrute-forceimplementation
CF 432D - Prefixes and Suffixes

We are given a single string of uppercase letters. Our goal is to identify all prefixes of the string that are identical to some suffix, and for each such prefix, count how many times it occurs anywhere inside the string as a contiguous substring.

codeforcescompetitive-programmingdpstring-suffix-structuresstringstwo-pointers
CF 432C - Prime Swaps

We are given a permutation of integers from 1 to n, which means each integer in that range appears exactly once in the array. The goal is to sort this array in increasing order, but with a special restriction on the swaps we can make.

codeforcescompetitive-programminggreedysortings
CF 432B - Football Kit

We are asked to simulate a football tournament between n teams, where each team has a home kit and an away kit with distinct colors. Every team plays a home and away game against each other team. By default, the home team wears its home kit and the away team wears its away kit.

codeforcescompetitive-programmingbrute-forcegreedyimplementation
CF 425C - Sereja and Two Sequences

We have two sequences. A profitable move chooses a non-empty prefix from each sequence, with the requirement that the last element of the chosen prefix in the first sequence is equal to the last element of the chosen prefix in the second sequence.

codeforcescompetitive-programmingdata-structuresdp
CF 425A - Sereja and Swaps

We are given an array of integers and a limited budget of swap operations. Each swap allows exchanging any two positions in the array, and we can perform at most k such swaps.

codeforcescompetitive-programmingbrute-forcesortings
CF 425E - Sereja and Sets

We are asked to count sets of intervals within the integer range from 1 to n, such that the largest collection of non-overlapping intervals in the set has exactly size k.

codeforcescompetitive-programmingdp
CF 425B - Sereja and Table

We are given a table of size n × m, where each cell contains either a zero or a one. Sereja wants to modify at most k cells so that the table satisfies a very specific property: each connected group of identical numbers must form a perfect rectangle aligned with the table’s…

codeforcescompetitive-programmingbitmasksgreedy
CF 412C - Pattern

We are asked to merge multiple patterns into a single pattern that intersects with all of them, minimizing the number of question marks. Each pattern consists of lowercase letters and question marks, where a question mark matches any letter.

codeforcescompetitive-programmingimplementationstrings
CF 412E - E-mail Addresses

We are given one long string that contains only lowercase letters, digits, , @, and .. We must count how many substrings of this string are valid e-mail addresses. Substrings are distinguished by their positions, not by their textual contents.

codeforcescompetitive-programmingimplementation
CF 412B - Network Configuration

We are given a set of computers, each with a measured maximum Internet speed. There are fewer participants than computers, and each participant must get a separate computer.

codeforcescompetitive-programminggreedysortings
CF 412A - Poster

We are asked to simulate painting a slogan on a linear banner that is divided into n squares, one character per square. The painter can use a ladder that initially stands in front of the k-th square.

codeforcescompetitive-programminggreedyimplementation
CF 411C - Kicker

We are given four players split into two teams of two. Each player has two independent strengths: one for defending and one for attacking. Before the match, each team assigns one player to attack and the other to defend.

codeforcescompetitive-programming*specialimplementation
CF 411B - Multi-core Processor

We are simulating a processor with several cores and several memory cells. Time is divided into cycles. For every cycle, each core receives either a command to do nothing or a command to write into a specific memory cell. The interesting part is how deadlocks occur.

codeforcescompetitive-programmingimplementation
CF 411A - Password Check

We are asked to check whether a password string is "complex enough" based on four criteria. The password is a sequence of up to 100 characters containing uppercase letters, lowercase letters, digits, and a few special characters.

codeforcescompetitive-programming*specialimplementation
CF 409G - On a plane

We are given a set of $n$ points on a 2D plane with floating-point coordinates. The task is to find the smallest possible angle of rotation around the origin that ensures all points can be covered by a half-plane (a straight line that divides the plane into two parts).

codeforcescompetitive-programming*specialgeometry
CF 409I - Feed the Golorp

The input is a single string that visually looks like a tiny ASCII “program”. Inside it there are special symbols forming a structure, and within this structure there are placeholder positions that behave like variables.

codeforcescompetitive-programming*special
CF 409H - A + B Strikes Back

The task is to read a single line containing two non-negative integers separated by whitespace and output their arithmetic sum.

codeforcescompetitive-programming*specialbrute-forceconstructive-algorithmsdsuimplementation
CF 409F - 000001

We are given a single integer $a$, and we are asked to compute a certain count associated with binary strings of length $a$. Each position in such a string can be thought of as a switch that is either off or on, but the strings we are allowed to consider are not arbitrary.

codeforcescompetitive-programming*special
CF 409E - Dome

The problem presents a number x that represents a dome's height in some scaled system. Our task is to find two positive integers, a and b, both between 1 and 10 inclusive, that encode this height according to the formula $x = a cdot sqrt{b}$.

codeforcescompetitive-programming*special
CF 409B - Mysterious Language

This is one of Codeforces' April Fools problems. There is no real algorithmic input. The judge provides a special language called "Secret" through the custom invocation system. The task is to identify what that language actually is and submit a program written in that language.

codeforcescompetitive-programming*special
CF 409D - Big Data

At first glance, the problem seems almost absurdly simple: you are given an integer between 1 and 16, and you need to output a single integer corresponding to that input.

codeforcescompetitive-programming*special
CF 409A - The Great Game

We are given two short strings representing sequences of moves in a fictional duel between two teams. Each string is built from a small alphabet that visually looks like emoticon fragments, and each valid move is actually encoded using two characters.

codeforcescompetitive-programming*special
CF 409C - Magnum Opus

The problem gives us a list of integers representing quantities of some alchemical ingredients. The goal is to determine the greatest common divisor (GCD) of these quantities. In other words, we want to find the largest integer $d$ such that each given number is divisible by $d$.

codeforcescompetitive-programming*special
CF 407E - k-d-sequence

We are given an integer sequence of length $n$ and two additional parameters: $k$, the maximum number of elements we are allowed to insert, and $d$, the intended difference of an arithmetic progression.

codeforcescompetitive-programmingdata-structures
CF 407D - Largest Submatrix 3

We are given an integer matrix of size n by m. Each cell contains a positive integer. The task is to find the largest rectangular submatrix where all the elements are distinct. The “largest” is measured by area, meaning the number of cells inside the rectangle.

codeforcescompetitive-programmingdphashing
CF 407C - Curious Array

We are given an array and a sequence of range operations. Each operation picks a segment $[l, r]$ and a parameter $k$, and for every position $j$ inside that segment we add a value that depends on how far $j$ is from the left endpoint $l$.

codeforcescompetitive-programmingbrute-forcecombinatoricsimplementationmath
CF 407A - Triangle

We are given two positive integers, a and b, which are the lengths of the legs of a right triangle. The task is not to check whether such a triangle exists, it obviously does.

codeforcescompetitive-programmingbrute-forcegeometryimplementationmath
CF 407B - Long Path

We are given a linear maze with n+1 rooms, numbered from 1 to n+1. Each room 1 through n has two portals. The first portal always moves forward to the next room, i+1. The second portal moves backward to some previous room, p₁ through pₙ, where pᵢ ≤ i.

codeforcescompetitive-programmingdpimplementation
CF 405D - Toy Sum

We are given a large universe of numbered blocks from 1 to 1,000,000. Some subset of these blocks, called $X$, has been removed from Chris’s set. From the remaining blocks, we need to choose a non-empty subset $Y$ so that a very specific weighted balance condition holds.

codeforcescompetitive-programminggreedyimplementationmath
CF 405E - Graph Cutting

We are given a connected undirected graph, and we are asked to completely decompose its edges into length-2 paths.

codeforcescompetitive-programmingdfs-and-similargraphs
CF 405C - Unusual Product

We are given an n×n binary matrix, meaning every element is either 0 or 1. The "unusual square" of the matrix is defined as the sum over all rows of the dot product of row i with column i, computed modulo 2.

codeforcescompetitive-programmingimplementationmath
CF 405B - Domino Effect

We are given a row of dominoes, some of which are initially pushed to fall either left or right, while others are standing upright. Each second, a falling domino pushes its immediate neighbor in the same direction.

codeforcescompetitive-programming
CF 405A - Gravity Flip

We are given a row of vertical stacks of cubes. Each position in the row holds a column, and the input array describes how many cubes are stacked at each position. Then a “gravity switch” happens.

codeforcescompetitive-programminggreedyimplementationsortings
CF 404E - Maze 1D

We have a robot standing on an infinite 1D strip of cells indexed by integers, starting at cell 0. The robot is given a sequence of moves, each either left (L) or right (R). Before the robot starts, we can place obstacles on some cells, except cell 0.

codeforcescompetitive-programmingbinary-searchgreedyimplementation
CF 404D - Minesweeper 1D

We have a one-dimensional Minesweeper field represented as a string of length n. Each character can be a bomb '', an unknown '?', or a number 0, 1, or 2. The numbers indicate how many bombs are immediately adjacent to that cell.

codeforcescompetitive-programmingdpimplementation
CF 404C - Restore Graph

We are asked to reconstruct an undirected connected graph from a list of shortest distances from one vertex, under the constraint that each vertex can have at most k edges.

codeforcescompetitive-programmingdfs-and-similargraphssortings
CF 404A - Valera and X

We are given an odd-sized square grid of letters. Each cell contains a lowercase English letter. The task is to check whether the letters form a perfect "X" pattern.

codeforcescompetitive-programmingimplementation
CF 404B - Marathon

Valera runs around the perimeter of a square stadium. The square has side length a, and the route follows the boundary in counterclockwise order. The starting point is the bottom-left corner (0, 0).

codeforcescompetitive-programmingimplementationmath
CF 402E - Strictly Positive Matrix

We are given a square matrix $a$ of size $n times n$ whose elements are all non-negative integers. The matrix can be thought of as a weighted adjacency matrix of a graph with $n$ nodes, where $a{ij} 0$ indicates a direct edge from node $i$ to node $j$.

codeforcescompetitive-programminggraphsmath
CF 402C - Searching for Graph

We are asked to construct a special type of undirected graph. The graph has n vertices and a parameter p, and it must satisfy two global conditions. First, the total number of edges is exactly 2n + p. Second, no subgraph of k vertices may have more than 2k + p edges.

codeforcescompetitive-programmingbrute-forceconstructive-algorithmsgraphs
CF 402A - Nuts

We are asked to distribute a given number of nuts into boxes, but the boxes are not simple containers. Each box can be split into sections using “divisors,” and every section can hold at most a certain number of nuts.

codeforcescompetitive-programminggreedymath
CF 402B - Trees in a Row

We have a row of trees, each with a given height. The Queen wants the trees arranged in perfect arithmetic progression: the difference between consecutive tree heights must be exactly $k$.

codeforcescompetitive-programmingbrute-forceimplementation
CF 401A - Vanya and Cards

Vanya has found a subset of his playing cards, each card labeled with an integer between $-x$ and $x$. He wants the total sum of all his cards to be zero. We need to determine the minimum number of additional cards he should find to achieve that zero sum.

codeforcescompetitive-programmingimplementationmath
CF 401C - Team

We are asked to arrange a set of cards, some marked with zero and some with one, in a line so that no two zeros are adjacent and no three ones are consecutive. We know the counts of each type of card upfront: n zeros and m ones.

codeforcescompetitive-programmingconstructive-algorithmsgreedyimplementation
CF 353A - Domino

We have a row of domino tiles. Each tile has a number on its upper half and a number on its lower half. For every tile, we may either leave it as it is or rotate it by 180 degrees. Rotating a tile simply swaps its upper and lower values. Each rotation costs one second.

codeforcescompetitive-programmingimplementationmath
CF 353C - Find Maximum

We are given an array of non-negative integers a with length n and a number m in binary form. We want to select a subset of indices from 0 to n-1 and sum the corresponding a[i] values, but with a twist: the subset corresponds to the binary representation of some integer x…

codeforcescompetitive-programmingimplementationmathnumber-theory
CF 353E - Antichain

Codeforces 353E: Antichain

codeforcescompetitive-programmingdpgraph-matchingsgreedy
CF 353D - Queue

We are given a line of schoolchildren represented as a string of letters M and F, where M indicates a boy and F a girl. Each second, any boy standing immediately in front of a girl swaps positions with her. This process repeats until no boy is in front of a girl.

codeforcescompetitive-programmingconstructive-algorithmsdp
CF 353B - Two Heaps

Each cube contains a two digit number. We must split the 2n cubes into two heaps of exactly n cubes each. After the split, Valera may choose any cube from the first heap and any cube from the second heap.

codeforcescompetitive-programmingcombinatoricsconstructive-algorithmsgreedyimplementationmathsortings
CF 351C - Jeff and Brackets

We are asked to construct a bracket sequence of length n·m, where n is a small number up to 20 and m is much larger, up to 10^7, and is even. Each position in the sequence can be either an opening bracket ( or a closing bracket ).

codeforcescompetitive-programmingdpmatrices
CF 351A - Jeff and Rounding

Jeff has a list of 2n real numbers and he wants to round them in pairs so that the total sum changes as little as possible. Each operation consists of taking two unused numbers: one is rounded down (floor) and the other rounded up (ceiling).

codeforcescompetitive-programmingdpgreedyimplementationmath
CF 351E - Jeff and Permutation

We are given a sequence of integers, and we are allowed to flip the sign of any element. The goal is to minimize the number of inversions in the sequence. An inversion occurs whenever a larger number appears before a smaller number in the sequence.

codeforcescompetitive-programminggreedy
CF 351D - Jeff and Removing Periods

For each query we look at a subarray of the given array and ask for its beauty. A single operation chooses several equal values whose positions form an arithmetic progression, removes them, and then allows us to reorder everything that remains. That last sentence is the key.

codeforcescompetitive-programmingdata-structures
CF 351B - Jeff and Furik

We are asked to model a two-player game played on a permutation of integers from 1 to n. Jeff moves first and can swap any adjacent elements.

codeforcescompetitive-programmingcombinatoricsdpprobabilities
Kvant Math Problem 2867

Label the people around the circle as a cyclic sequence of 60 vertices, each colored either $B$ (boy) or $G$ (girl), with exactly 30 of each color.

kvantmathematicsolympiad
Kvant Math Problem 2738

Let

kvantmathematicsolympiad
Kvant Math Problem 1592

Let integers $a \le b$ be such that the required sum is

kvantmathematicsolympiad
Kvant Math Problem 1528

The relation

kvantmathematicsolympiad
CF 350C - Bombs

We are given a set of points on an infinite grid, each point containing a single bomb. A robot starts at the origin and must eventually destroy every bomb, but it cannot simply “teleport” to them.

codeforcescompetitive-programminggreedyimplementationsortings
CF 350E - Wrong Floyd

We are given an undirected, simple, connected graph with $n$ vertices and $m$ edges. In addition, a subset of vertices $a1, a2, dots, ak$ is designated as “special”.

codeforcescompetitive-programmingbrute-forceconstructive-algorithmsdfs-and-similargraphs
CF 350D - Looking for Owls

We are asked to count "owls" in a geometric picture composed of segments and circles. An owl is defined as a combination of two circles and a segment where the circles are reflections of each other across the segment, have identical radii, do not overlap, and the segment…

codeforcescompetitive-programmingbinary-searchdata-structuresgeometryhashingsortings
CF 350A - TL

We are asked to help Valera set a time limit for a programming problem. He has a set of correct solutions, each with a known runtime, and a set of wrong solutions, also with runtimes.

codeforcescompetitive-programmingbrute-forcegreedyimplementation
Kvant Math Problem 1487

Place the circumcenter $O$ as a reference point and represent the triangle on its circumcircle.

kvantmathematicsolympiad
CF 349B - Color the Fence

Igor wants to paint the largest possible number on a fence using a limited amount of paint. Each digit from 1 to 9 has a specific paint cost, and zero cannot be used.

codeforcescompetitive-programmingdata-structuresdpgreedyimplementation
CF 349A - Cinema Line

We are given a queue of people waiting to buy cinema tickets. Each person holds a bill worth 25, 50, or 100 rubles, and each ticket costs 25 rubles. The clerk starts with no money and must sell tickets in the exact order of the line, giving correct change if necessary.

codeforcescompetitive-programminggreedyimplementation
CF 348C - Subset Sums

We are given an array of integers and a collection of subsets, each referencing indices in the array. Two types of operations are performed repeatedly: querying the sum of the elements of a subset, and adding a value to all elements of a subset.

codeforcescompetitive-programmingbrute-forcedata-structures
Kvant Math Problem 1453

Let $n_k$ be the repunit of length $k$, so

kvantmathematicsolympiad
CF 348B - Apple Tree

We are given a rooted tree where only the leaves initially contain apples, while internal nodes are empty. Each leaf contributes a fixed number of apples, and the “weight” of any subtree is defined as the total number of apples in all leaves inside that subtree.

codeforcescompetitive-programmingdfs-and-similarnumber-theorytrees
CF 348E - Pilgrims

We are given a weighted tree with n towns. Some of these towns contain monasteries, and each monastery hosts exactly one pilgrim.

codeforcescompetitive-programmingdfs-and-similardptrees
CF 348D - Turtles

We are given a rectangular grid of size n by m, where each cell is either free or blocked. Two turtles start at the top-left corner, cell (1,1), and both want to reach the bottom-right corner, cell (n,m).

codeforcescompetitive-programmingdpmatrices
CF 348A - Mafia

We are given a group of friends who will repeatedly play a game. Each round of the game has exactly one person acting as a supervisor, while the remaining $n-1$ people participate as players.

codeforcescompetitive-programmingbinary-searchmathsortings
Kvant Math Problem 962

Let $P(x)\in\mathbb{Z}[x]$ and suppose there exist distinct integers $x_1,\dots,x_n$ with $n\ge 3$ such that

kvantmathematicsolympiad
CF 347A - Difference Row

We are given a list of n integers and need to arrange them in a row such that the sum of differences between consecutive numbers is maximized. Concretely, if the arrangement is $x1, x2, dots, xn$, the value is calculated as $(x1 - x2) + (x2 - x3) + dots + (x{n-1} - xn)$.

codeforcescompetitive-programmingconstructive-algorithmsimplementationsortings
Kvant Math Problem 873

The process can be encoded in terms of the coefficients $a$ and $b$ of the quadratic $x^2+ax+b$, starting from $(a,b)=(10,20)$ and ending at $(20,10)$.

kvantmathematicsolympiad
CF 347B - Fixed Points

We are given a permutation of numbers from 0 to n − 1, stored in an array where each index represents a position and the value at that index represents where that position “points”. A position i is called a fixed point if the value stored at that position is exactly i.

codeforcescompetitive-programmingbrute-forceimplementationmath
CF 346C - Number Transformation II

We start with a number a and want to reduce it down to b using two types of moves. One move simply decreases the current value by 1. The other move is more interesting: we pick one of the given numbers xi and subtract the remainder of the current value when divided by xi.

codeforcescompetitive-programminggreedymath
CF 346A - Alice and Bob

We start with a finite set of distinct positive integers. Two players alternate turns, and on each turn a player is allowed to take any two existing numbers, compute their absolute difference, and insert that value into the set if it is not already present.

codeforcescompetitive-programminggamesmathnumber-theory
Kvant Math Problem 803

Let $x,y \in \mathbb{Q}\setminus{0}$ satisfy

kvantmathematicsolympiad