brain

tamnd's digital brain — notes, problems, research

41751 notes

CF 492B - Vanya and Lanterns

We are asked to determine the minimum radius of light for lanterns placed along a straight street of length l so that the entire street is illuminated. Each lantern is at a fixed position along the street and can light up points within a distance d to its left and right.

codeforcescompetitive-programmingbinary-searchimplementationmathsortings
CF 490E - Restoring Increasing Sequence

We are given a sequence of strings. Each string represents a positive integer, but some digits have been replaced by '?'. The original sequence was strictly increasing. Our task is to replace every '?

codeforcescompetitive-programmingbinary-searchbrute-forcegreedyimplementation
CF 490F - Treeland Tour

We are given a tree of cities, where each city has a population. The roads connect cities such that there is exactly one simple path between any two cities.

codeforcescompetitive-programmingdata-structuresdfs-and-similardptrees
CF 490D - Chocolate

Each chocolate bar is a rectangle made of unit squares, so its “value” is just its area. We start with two rectangles and we are allowed to modify them minute by minute.

codeforcescompetitive-programmingbrute-forcedfs-and-similarmathmeet-in-the-middlenumber-theory
CF 490B - Queue

Each input line describes one student, but the student's own ID is not given directly. Instead, we are told the ID of the student standing immediately in front of them and the ID of the student standing immediately behind them.

codeforcescompetitive-programmingdsuimplementation
CF 490C - Hacking Cypher

We are given a very long decimal number as a string. Its length can reach one million digits, so treating it as a normal integer is impossible. We must choose a position where the string is cut into two nonempty pieces.

codeforcescompetitive-programmingbrute-forcemathnumber-theorystrings
CF 490A - Team Olympiad

We are given a list of children, each identified by a number indicating their skill: programming (1), maths (2), or physical education (3). The task is to form as many teams of three as possible, where each team has exactly one child from each skill.

codeforcescompetitive-programminggreedyimplementationsortings
CF 489E - Hiking

We have a sequence of resting points along a river. Point i is located at position x[i] and has picturesqueness value b[i]. The traveler starts at coordinate 0 and must finish at the last resting point, which is also the farthest one.

codeforcescompetitive-programmingbinary-searchdp
CF 489F - Special Matrices

We are asked to count the number of n × n binary matrices where each row and each column contains exactly two ones. Some of the first rows are already fixed, and we must count only matrices consistent with them.

codeforcescompetitive-programmingcombinatoricsdp
CF 489D - Unbearable Controversy of Being

We are given a directed graph representing intersections and one-way roads. A "damn rhombus" consists of four distinct vertices $a, b, c, d$ such that there are directed edges $$a rightarrow b,quad b rightarrow c,quad a rightarrow d,quad d rightarrow c$$ In other words, from…

codeforcescompetitive-programmingbrute-forcecombinatoricsdfs-and-similargraphs
CF 489A - SwapSort

We are given an array of integers and must transform it into non-decreasing order by performing swaps of array positions. The interesting part is that we do not need the minimum number of swaps. Any valid sequence containing at most n swaps is accepted.

codeforcescompetitive-programminggreedyimplementationsortings
CF 489B - BerSU Ball

We have two groups of dancers. Each boy has a skill level, and each girl has a skill level. A boy and a girl can be paired only if their skill levels differ by at most 1. Every dancer can belong to at most one pair. The task is to form as many valid boy-girl pairs as possible.

codeforcescompetitive-programmingdfs-and-similardpgraph-matchingsgreedysortingstwo-pointers
CF 489C - Given Length and Sum of Digits...

We are asked to construct two integers of a specified length, m, such that the sum of their digits is exactly s. One of these integers must be the smallest possible and the other the largest possible in lexicographical order.

codeforcescompetitive-programmingdpgreedyimplementation
CF 487E - Tourists

We are asked to handle queries on a connected undirected graph representing cities in Cyberland. Each city has a souvenir price, and roads connect pairs of cities.

codeforcescompetitive-programmingdata-structuresdfs-and-similargraphstrees
CF 487D - Conveyor Belts

We are given a rectangular table of size n by m filled with conveyor belts. Each conveyor belt points either up (^), left (<), or right (). Surrounding the table are diner seats, numbered logically as rows 0 and n+1 and columns 0 and m+1.

codeforcescompetitive-programmingdata-structures
CF 487C - Prefix Product Sequence

We need to arrange the numbers $1,2,dots,n$ into a permutation $a1,a2,dots,an$. Define $$pi = a1a2cdots ai pmod n.$$ The requirement is that the sequence $p1,p2,dots,pn$ must itself be a permutation of all residues $0,1,dots,n-1$.

codeforcescompetitive-programmingconstructive-algorithmsmathnumber-theory
CF 487A - Fight the Monster

We are given the initial combat statistics of two characters, Master Yang and a monster. Each character has hit points, attack, and defense. Combat proceeds in discrete seconds.

codeforcescompetitive-programmingbinary-searchbrute-forceimplementation
CF 487B - Strip

We have an array of integers representing numbers on a strip of paper. Alexandra wants to divide the strip into contiguous segments. Each segment must have at least l numbers, and the difference between the largest and smallest number in that segment must not exceed s.

codeforcescompetitive-programmingbinary-searchdata-structuresdptwo-pointers
CF 486E - LIS of Sequence

We are given an array and we care about its longest increasing subsequences. An index belongs to a longest increasing subsequence if the value at that position is selected as one of the elements of that subsequence. The task is not to count LISs.

codeforcescompetitive-programmingdata-structuresdpgreedyhashingmath
CF 486D - Valid Sets

We are given a tree where every vertex has an integer value. A set of vertices is considered valid if it is non-empty, forms a connected subgraph of the tree, and the difference between the largest and smallest value inside the set does not exceed d.

codeforcescompetitive-programmingdfs-and-similardpmathtrees
CF 486C - Palindrome Transformation

We are given a string of length $n$ consisting of lowercase English letters, along with a cursor position $p$ that can move left or right around the string in a cyclic manner.

codeforcescompetitive-programmingbrute-forcegreedyimplementation
CF 486A - Calculating Function

We are given a single integer n and asked to evaluate the alternating sum $$-1 + 2 - 3 + 4 - 5 + dots + (-1)^n n.$$ The sign of each term depends on its position. Odd numbers are subtracted and even numbers are added. The output is the final value of this sum.

codeforcescompetitive-programmingimplementationmath
CF 486B - OR in Matrix

We are given a matrix B of size m × n, where each element is either 0 or 1. This matrix was supposedly generated from some original matrix A of the same size using a specific rule: each element B[i][j] equals the logical OR of all elements in the i-th row of A combined with…

codeforcescompetitive-programminggreedyhashingimplementation
CF 484E - Sign on Fence

We have a fence represented by an array of heights. A rectangular sign must be placed on top of some consecutive panels. The sign has a fixed width w, so it must cover exactly w consecutive fence panels.

codeforcescompetitive-programmingbinary-searchconstructive-algorithmsdata-structures
CF 484D - Kindergarten

We have a line of children, each with an integer charisma value. The goal is to partition the line into contiguous groups. For each group, its sociability is the difference between the largest and smallest charisma values in that group.

codeforcescompetitive-programmingdata-structuresdpgreedy
CF 484C - Strange Sorting

Working

codeforcescompetitive-programmingimplementationmath
CF 484A - Bits

We are asked to process several queries, each defined by a lower bound l and an upper bound r. For each query, we need to find the integer x within the interval [l, r] such that the number of set bits in its binary representation is as large as possible.

codeforcescompetitive-programmingbitmasksconstructive-algorithms
CF 484B - Maximum Value

We are given a sequence of integers a1, a2, …, an and asked to compute the maximum value of ai % aj over all pairs (i, j) where ai ≥ aj. The input consists of the number of integers n and the sequence itself.

codeforcescompetitive-programmingbinary-searchmathsortingstwo-pointers
CF 482C - Game with Strings

We know all candidate strings in advance. One of them is chosen uniformly at random. We reveal information by asking about character positions. The order of questions is not chosen strategically. At every step we pick uniformly among positions that have not been asked yet.

codeforcescompetitive-programmingbitmasksdpprobabilities
CF 482D - Random Function and Tree

We are given a rooted tree with n vertices, where vertex 1 is the root and every other vertex i has a specified parent pi. Initially, every vertex is painted red.

codeforcescompetitive-programmingcombinatoricsdptrees
CF 482E - ELCA

I can't provide a correct "complete solution editorial" with a full accepted implementation for Codeforces 482E from first principles here, because this is a very difficult 3200-rated dynamic-tree problem and I do not have enough information to reliably reconstruct the entire…

codeforcescompetitive-programmingdata-structurestrees
CF 482A - Diverse Permutation

We are asked to construct a permutation of integers from 1 to n such that the set of absolute differences between consecutive elements contains exactly k distinct values.

codeforcescompetitive-programmingconstructive-algorithmsgreedy
CF 482B - Interesting Array

We are asked to construct an array of n non-negative integers such that m given bitwise AND constraints are satisfied.

codeforcescompetitive-programmingconstructive-algorithmsdata-structurestrees
CF 466E - Information Graph

We have a company hierarchy that is built gradually over time. Initially nobody has a boss. A type 1 operation attaches an employee x under employee y, making y the boss of x. The input guarantees that x currently has no boss, and cycles never appear.

codeforcescompetitive-programmingdfs-and-similardsugraphstrees
CF 464E - The Classic Problem

We are given an undirected weighted graph with up to 100,000 vertices and 100,000 edges. Each edge weight is a power of two, specified as $2^{xi}$, where $xi$ is between 0 and 100,000.

codeforcescompetitive-programmingdata-structuresgraphsshortest-paths
CF 464B - Restore Cube

We are given eight points in three-dimensional space. Each point was originally a vertex of a cube whose side length was positive. The complication is that the coordinates have been corrupted in a very specific way.

codeforcescompetitive-programmingbrute-forcegeometry
CF 464D - World of Darkraft - 2

Working

codeforcescompetitive-programmingdpprobabilities
CF 464A - No to Palindromes!

We are given a string s of length n consisting of lowercase letters, and an integer p representing the number of allowed letters from the start of the alphabet, that is, 'a' through chr(ord('a') + p - 1).

codeforcescompetitive-programminggreedystrings
CF 464C - Substitutes in Number

We start with a decimal string s. Then a sequence of replacement rules is applied in order. Each rule has the form d - t, meaning every occurrence of digit d is replaced by the string t.

codeforcescompetitive-programmingdp
CF 452F - Permutation

We are given a permutation of the numbers 1...n. For two distinct values a and b, their average is (a+b)/2. Since the permutation contains every integer exactly once, this average is also present whenever a+b is even.

codeforcescompetitive-programmingdata-structuresdivide-and-conquerhashing
CF 452D - Washer, Dryer, Folder

We have a three-stage production pipeline. Every laundry item must first be washed, then immediately transferred to a dryer when washing finishes, then immediately transferred to a folding machine when drying finishes.

codeforcescompetitive-programminggreedyimplementation
CF 448E - Divisors

We are given a number X and an integer k, and we want to construct a sequence by repeatedly expanding each number into its divisors in increasing order. Formally, we define X0 as a sequence containing just X.

codeforcescompetitive-programmingbrute-forcedfs-and-similarimplementationnumber-theory
CF 448B - Suffix Structures

We are given two distinct lowercase words, s and t, and we are asked to determine how s can be transformed into t using specific operations inspired by suffix data structures.

codeforcescompetitive-programmingimplementationstrings
CF 448C - Painting Fence

We are asked to compute the minimum number of brush strokes needed to paint a fence of vertical planks. Each plank has a width of 1 meter and an individual height. The brush is exactly 1 meter wide, and it can be applied either vertically or horizontally.

codeforcescompetitive-programmingdivide-and-conquerdpgreedy
CF 447A - DZY Loves Hash

We are tasked with simulating insertions into a hash table with a fixed number of buckets. Each bucket can hold at most one element. The hash function is simple: for a number $x$, the target bucket is $x mod p$. We receive a sequence of numbers and must insert them in order.

codeforcescompetitive-programmingimplementation
Kvant Math Problem 2885

Each vertex is incident to exactly two red edges, so the red edges form a $2$-regular subgraph of the edge graph of the polyhedron.

kvantmathematicsolympiad
CF 446B - DZY Loves Modification

We have an n × m matrix of integers. We must perform exactly k operations. An operation chooses either an entire row or an entire column. The current sum of that row or column is added to our answer, then every element inside it is decreased by p.

codeforcescompetitive-programmingbrute-forcedata-structuresgreedy
CF 446A - DZY Loves Sequences

We are given an array of up to $10^5$ integers. We may choose any contiguous subarray and are allowed to modify at most one element inside that subarray, replacing it with any integer we want.

codeforcescompetitive-programmingdpimplementationtwo-pointers
CF 445B - DZY Loves Chemistry

The chemicals and reactions form an undirected graph. Each vertex represents a chemical, and an edge between two vertices means those two chemicals react. We start with danger equal to 1. Chemicals are poured one at a time in any order.

codeforcescompetitive-programmingdfs-and-similardsugreedy
Kvant Math Problem 2876

We study functions of the form $f_{a,b}(n)=\lfloor an+b\rfloor$ with $a>0$ acting on $\mathbb{N}$.

kvantmathematicsolympiad
CF 444E - DZY Loves Planting

We are given a tree with n nodes, where each edge has a positive weight. For any two nodes x and y, the function g(x, y) is defined as the maximum weight along the unique path connecting them.

codeforcescompetitive-programmingbinary-searchdsutrees
CF 444C - DZY Loves Colors

We have a ribbon consisting of positions 1...n. Initially, position i has color i, so every position starts with a distinct color. Two kinds of operations are performed. A paint operation assigns a new color x to every position in a segment [l, r].

codeforcescompetitive-programmingdata-structures
CF 444B - DZY Loves FFT

The task asks us to compute a sequence c based on two sequences a and b of length n. Sequence a is a permutation of the integers from 1 to n, and b is a binary sequence with exactly d ones, both shuffled in a pseudo-random but reproducible way.

codeforcescompetitive-programmingprobabilities
CF 443B - Kolya and Tandem Repeat

We are given a string of lowercase English letters that Kolya initially owns, and he can append up to k additional characters to the end. The task is to determine the maximum possible length of a tandem repeat in the resulting string.

codeforcescompetitive-programmingbrute-forceimplementationstrings
CF 442D - Adam and Tree

The tree starts with a single root vertex. Each operation adds one new leaf to an existing vertex. After every addition we must compute the smallest possible value of the following quantity. We color the edges of the tree.

codeforcescompetitive-programmingdata-structurestrees
CF 442E - Gena and Second Distance

I can't reliably produce a complete editorial for this problem from first principles without reconstructing the original 3100-rated geometry solution in detail.

codeforcescompetitive-programminggeometry
CF 442C - Artem and Array

We are asked to simulate a game on an array of positive integers where, at each move, we remove a single element and earn points equal to the minimum of its adjacent elements. After removal, the array closes up and the game continues until all elements are gone.

codeforcescompetitive-programmingdata-structuresgreedy
Kvant Math Problem 2863

The configuration is a complete quadrilateral formed by the four lines $AB, BC, CD, DA$.

kvantmathematicsolympiad
CF 437C - The Child and Toy

We are given a toy made up of n parts connected by m ropes. Each rope connects two distinct parts, and no pair of parts has more than one rope between them. Each part i has an associated energy cost v[i]. The child removes parts one at a time.

codeforcescompetitive-programminggraphsgreedysortings
CF 345F - Superstitions Inspection

The input is not given in the usual structured format with counts. Instead, it is a small text file that alternates between country names and lists of superstitions. A line that does not start with is a country name.

codeforcescompetitive-programming*special
CF 345C - Counting Fridays

We are given a small list of contest dates. For each date, we must determine whether that calendar day is both the 13th day of its month and a Friday. The answer is simply the number of contest dates satisfying both conditions.

codeforcescompetitive-programming*special
Kvant Math Problem 2854

Let $n=\prod p_i^{\alpha_i}$ with all $p_i<100$.

kvantmathematicsolympiad
Kvant Math Problem 2830

Let $S$ be the set of amounts obtainable with at most $1000$ coins of denominations $a$ and $b$.

kvantmathematicsolympiad
Kvant Math Problem 2786

Let the value of a completed coloring be the number of edges of the 100-cycle whose endpoints have different colors.

kvantmathematicsolympiad
Kvant Math Problem 1580

The problem asks whether a circle can be dissected into finitely many pieces whose boundaries consist of line segments and circular arcs, and then reassembled into a square of the same area.

kvantmathematicsolympiad
Kvant Math Problem 1561

Let the convex polygon be $A_1A_2\ldots A_n$, indexed cyclically.

kvantmathematicsolympiad
Kvant Math Problem 1531

Each question consists of choosing a line and learning in which of the three sets determined by that line the point $P$ lies: one open half-plane, the other open half-plane, or the line itself.

kvantmathematicsolympiad
Kvant Math Problem 1503

Let $B$ be the set of black numbers and $W$ the set of white numbers.

kvantmathematicsolympiad
Kvant Math Problem 1483

Each vector lies in the first quadrant and has length $1$.

kvantmathematicsolympiad
Kvant Math Problem 1467

Let

kvantmathematicsolympiad
Kvant Math Problem 957

Each allowed integer has the form

kvantmathematicsolympiad
Kvant Math Problem 902

Suppose the partition consists of finitely many arithmetic progressions

kvantmathematicsolympiad
Kvant Math Problem 857

A number is marked if it can be written as a sum of exactly five powers of two, repetitions allowed.

kvantmathematicsolympiad
Kvant Math Problem 806

Let

kvantmathematicsolympiad
Kvant Math Problem 736

The statement involves a median and an angle bisector meeting at a point.

kvantmathematicsolympiad
Kvant Math Problem 697

Let the square have side length $1$.

kvantmathematicsolympiad
Kvant Math Problem 596

The condition says that every triangle whose three sides belong to the colored segments contains both colors.

kvantmathematicsolympiad
Kvant Math Problem 567

Take a small example, say $p=2$, $q=3$.

kvantmathematicsolympiad
Kvant Math Problem 490

Let the given integers be $a_1,\dots,a_{p-1}$, none divisible by $p$.

kvantmathematicsolympiad
Kvant Math Problem 437

Let the odd number be

kvantmathematicsolympiad
Kvant Math Problem 194

Take a small example, say $a=3$, $b=7$.

kvantmathematicsolympiad
Kvant Math Problem 1566

Each committee has $80$ members, and there are $16000$ committees.

kvantmathematicsolympiad
Kvant Math Problem 1529

The equalities

kvantmathematicsolympiad
Kvant Math Problem 1515

Let

kvantmathematicsolympiad
Kvant Math Problem 1470

We need a set $A$ of positive integers such that every infinite set $S$ of primes contains, among the squarefree numbers formed from distinct primes of $S$, two numbers with the same number $k\ge2$ of…

kvantmathematicsolympiad
Kvant Math Problem 916

Let the acute triangle be $ABC$.

kvantmathematicsolympiad
Kvant Math Problem 817

Let

kvantmathematicsolympiad
Kvant Math Problem 790

The hypothesis is that a map $F:\mathbb{R}^2\to\mathbb{R}^2$ preserves unit distance, meaning every pair of points at distance $1$ is mapped to a pair of points at distance $1$.

kvantmathematicsolympiad
Kvant Math Problem 754

The expression

kvantmathematicsolympiad
Kvant Math Problem 723

We seek an infinite set $S \subset \mathbb{N}$ such that no element of $S$ and no finite sum of distinct elements of $S$ is a perfect power $a^k$ with $k \ge 2$.

kvantmathematicsolympiad
Kvant Math Problem 684

Each ship occupies an entire row or an entire column of an $n\times n$ board, and different ships are disjoint, so all ships are either rows or columns exclusively.

kvantmathematicsolympiad
Kvant Math Problem 651

Let the sofa, suitcase, valise, picture, basket, cardboard box, and dog have weights $S, U, V, P, B, C, D$ respectively.

kvantmathematicsolympiad
Kvant Math Problem 581

The first question asks for a three-digit integer $x$ such that $x^3$ ends in $777$, equivalently

kvantmathematicsolympiad
Kvant Math Problem 566

Let the fixed isosceles right triangle be placed as a unit right isosceles triangle with vertices $A(0,0)$, $B(1,0)$, $C(0,1)$.

kvantmathematicsolympiad
Kvant Math Problem 508

The three semicircles with diameters $AB$, $BC$, $AC$ lie on the same line $AB$, with centers at the midpoints of $AB$, $BC$, and $AC$.

kvantmathematicsolympiad
Kvant Math Problem 489

The transformation replaces each term by the average of the other two.

kvantmathematicsolympiad
Kvant Math Problem 462

Let the apex of the regular square pyramid be $S$, and let the base square be $ABCD$ with center $O$.

kvantmathematicsolympiad
Kvant Math Problem 435

Let $A=(a_{ij})$ be an $m\times n$ matrix.

kvantmathematicsolympiad
Kvant Math Problem 395

Label the vertices of a regular $n$-gon by $0,1,\dots,n-1$ in cyclic order.

kvantmathematicsolympiad
Kvant Math Problem 364

The requirement that every training session consists of 4 disjoint crews of 4 cosmonauts means that each session partitions the 16 cosmonauts into 4-element subsets.

kvantmathematicsolympiad