brain

tamnd's digital brain — notes, problems, research

41650 notes

CF 1009G - Allowed Letters

We are given a string made only from the first six lowercase letters. We are allowed to rearrange its characters arbitrarily by swapping any positions any number of times, so effectively we can treat it as a multiset of letters with full permutation freedom.

codeforcescompetitive-programmingbitmasksflowsgraph-matchingsgraphsgreedy
CF 1009E - Intercity Travelling

We are given a road split into $n$ unit segments, and each segment has a “base fatigue cost” $ai$ that applies when Leha starts a fresh driving session.

codeforcescompetitive-programmingcombinatoricsmathprobabilities
CF 1009F - Dominant Indices

We are working with a rooted tree where vertex 1 is considered the root. For every vertex $x$, we conceptually look at all nodes in its subtree and group them by their distance from $x$ in terms of edges downward in the tree.

codeforcescompetitive-programmingdata-structuresdsutrees
CF 1009C - Annoying Present

We start with an array of length $n$ that initially contains only zeros. Bob performs $m$ operations. Each operation is defined by a pair $(x, d)$, and Bob is allowed to pick a center position $i$ in the array.

codeforcescompetitive-programminggreedymath
CF 1009D - Relatively Prime Graph

We are asked to build a simple undirected graph on vertices numbered from 1 to n. The graph must have exactly m edges, must be connected, and must avoid both self-loops and repeated edges.

codeforcescompetitive-programmingbrute-forceconstructive-algorithmsgraphsgreedymath
CF 1009B - Minimum Ternary String

We are given a string made only of digits 0, 1, and 2. We are allowed to repeatedly swap adjacent pairs if they are 0 and 1 in either direction, or 1 and 2 in either direction.

codeforcescompetitive-programminggreedyimplementation
CF 1009A - Game Shopping

The shop presents a line of games, each with a fixed price, and Maxim walks through them strictly from left to right.

codeforcescompetitive-programmingimplementation
CF 1010E - Store

We are given a 3D calendar system where every moment is uniquely identified by a triple consisting of a month, a day inside that month, and a second inside that day.

codeforcescompetitive-programmingdata-structures
CF 1010F - Tree

We are given a rooted tree where the root is fixed at vertex 1 and every vertex has at most two children. After a process of “pruning”, we keep a connected set of vertices that must still contain the root.

codeforcescompetitive-programmingfftgraphstrees
CF 1010C - Border

We are given a set of banknotes, each with a fixed positive value. Natasha can use any number of each type of banknote, including zero, so in effect she can form any total sum that is a non-negative integer linear combination of the given values.

codeforcescompetitive-programmingnumber-theory
CF 1010D - Mars rover

The structure we are given is a rooted tree where every node behaves like a logic component. Leaves are fixed boolean inputs, while internal nodes compute boolean values from their children using standard gates such as AND, OR, XOR, and NOT.

codeforcescompetitive-programmingdfs-and-similargraphsimplementationtrees
CF 1010B - Rocket

We are trying to determine an unknown integer $x$ in the range from 1 to $m$, but we are not allowed to see it directly. Instead, we can probe it by asking queries with a chosen number $y$.

codeforcescompetitive-programmingbinary-searchinteractive
CF 1010A - Fly

We are given a fixed route of space travel that starts at Earth, visits several intermediate planets in order, reaches Mars, and then returns back to Earth.

codeforcescompetitive-programmingbinary-searchmath
CF 1011A - Stages

We are given a multiset of characters, each character representing a rocket stage with an intrinsic cost equal to its position in the alphabet.

codeforcescompetitive-programminggreedyimplementationsortings
CF 1011B - Planning The Expedition

We are given a collection of food packages, each labeled by a type. There are also $n$ participants in an expedition, and time is measured in days. Each day, every participant consumes exactly one package.

codeforcescompetitive-programmingbinary-searchbrute-forceimplementation
CF 1012F - Passports

We are given a small collection of trips, each with a fixed start day and duration, and for each trip a visa process time that determines how long a passport is tied up after an application. Gleb has at most two passports, and each visa must be assigned to one of them.

codeforcescompetitive-programmingdpimplementation
CF 1012E - Cycle sort

We are given an array of integers and allowed to rearrange it using a very specific primitive: we can take any set of indices and rotate the values sitting at those positions cyclically.

codeforcescompetitive-programmingdsumath
CF 1012D - AB-Strings

We are given two binary strings, each consisting only of the characters a and b. The only allowed operation is to choose a prefix of the first string and a prefix of the second string, then swap those prefixes in a single move.

codeforcescompetitive-programmingconstructive-algorithmsstrings
CF 1012C - Hills

We are given a line of hills, each with a fixed initial height. We are allowed to repeatedly pick any single hill and decrease its height by one unit per operation.

codeforcescompetitive-programmingdp
CF 1012B - Chemical table

We are given an $n times m$ grid where each cell is a distinct chemical element. Some of these cells are already available in the laboratory. From any three elements forming three corners of an axis-aligned rectangle, the scientists can always synthesize the fourth corner.

codeforcescompetitive-programmingconstructive-algorithmsdfs-and-similardsugraphsmatrices
CF 1012A - Photo of The Sky

We are given a multiset of numbers of size 2n, and we are told that these numbers originally came from n points in the plane, where each point contributes exactly two integers: its x-coordinate and its y-coordinate.

codeforcescompetitive-programmingbrute-forceimplementationmathsortings
CF 1013B - And

We are given a list of integers and a special integer x. The only allowed transformation is choosing an index and replacing that element with its bitwise AND with x. This operation can only decrease bits, since AND can only turn 1 bits into 0 depending on x.

codeforcescompetitive-programminggreedy
CF 1013A - Piles With Stones

We are given two snapshots of the same system of stone piles. In the first snapshot, each pile has some number of stones, and in the second snapshot the piles have different counts.

codeforcescompetitive-programmingmath
CF 1015E2 - Stars Drawing (Hard Edition)

The grid contains two kinds of cells, filled and empty. We are allowed to place geometric objects called stars, where each star consists of one central filled cell and four straight arms extending in the four cardinal directions.

codeforcescompetitive-programmingbinary-searchdpgreedy
CF 1015F - Bracket Substring

We are asked to build full valid bracket sequences of length 2n, and we want to count how many of those sequences contain a given string s as a contiguous block somewhere inside them. So conceptually, imagine all balanced parentheses strings of fixed size 2n.

codeforcescompetitive-programmingdpstrings
CF 1015D - Walking Between Houses

We are standing at the leftmost house in a long line of houses numbered from 1 to $n$. We must perform exactly $k$ moves, and each move consists of jumping from our current house to any different house.

codeforcescompetitive-programmingconstructive-algorithmsgreedy
CF 1015E1 - Stars Drawing (Easy Edition)

We are given a rectangular grid filled with two types of cells, occupied cells marked as and empty cells marked as .. The task is to explain the grid as a superposition of geometric objects called stars.

codeforcescompetitive-programmingbrute-forcedpgreedy
CF 1015C - Songs Compression

We are given a collection of songs, each with an original size and a smaller size if we choose to compress it. All songs must be copied onto a flash drive whose total capacity is fixed. The only decision available is which songs to compress.

codeforcescompetitive-programmingsortings
CF 1015B - Obtaining the String

We are given two strings of equal length, where one string represents a starting arrangement of characters and the other represents a target arrangement. The only allowed operation is swapping two adjacent characters in the starting string.

codeforcescompetitive-programmingimplementation
CF 1015A - Points in Segments

We are given a one-dimensional number line from 1 to m, and several closed intervals placed on it. Each interval covers every integer point between its endpoints, including both ends.

codeforcescompetitive-programmingimplementation
CF 1016G - Appropriate Team

We are given a list of candidate values, and we want to count how many ordered pairs of candidates can simultaneously support a hidden integer value $v$ under two arithmetic constraints involving gcd and lcm with fixed constants $X$ and $Y$.

codeforcescompetitive-programmingbitmasksmathnumber-theory
CF 1016D - Vasya And The Matrix

We are asked to construct a grid of size n by m where each cell contains a non-negative integer. What we know in advance is not the grid itself, but two sets of constraints derived from it: the XOR of every row is fixed, and the XOR of every column is fixed.

codeforcescompetitive-programmingconstructive-algorithmsflowsmath
CF 1016F - Road Projects

We are given a weighted tree whose nodes are cities and whose edges are existing roads with travel times. The structure guarantees a unique simple path between any two cities, so distances are well-defined tree distances.

codeforcescompetitive-programmingdfs-and-similardptrees
CF 1016E - Rest In The Shades

The task revolves around a point light source moving horizontally at a fixed negative height, and a set of disjoint segments lying on the x-axis that act as obstacles.

codeforcescompetitive-programmingbinary-searchgeometry
CF 1016C - Vasya And The Mushrooms

We are given a grid with two rows and $n$ columns. Each cell contains a value that represents how many mushrooms grow per minute in that cell. Vasya starts at the top-left cell and must move every minute to a neighboring cell sharing an edge.

codeforcescompetitive-programmingdpimplementation
CF 1016B - Segment Occurrences

We are given a base string $s$ and a pattern string $t$, both made of lowercase letters. The task is to answer many independent queries, where each query asks: inside a given substring of $s$, how many times does $t$ appear as a contiguous substring?

codeforcescompetitive-programmingbrute-forceimplementation
CF 1016A - Death Note

We are simulating how writing progresses through an infinite notebook where each page can store a fixed number of names.

codeforcescompetitive-programminggreedyimplementationmath
CF 1017H - The Films

We are given a shelf containing an ordered sequence of films, where each film has a “type” or ending label from 1 to m. This initial sequence is fixed and indexed from 1 to n.

codeforcescompetitive-programmingbrute-force
CF 1017G - The Tree

We are working with a rooted tree where vertex 1 is the root, and every node is initially colored white. Over time, we apply three kinds of operations that either flip colors, reset parts of the tree, or ask for the current color of a node.

codeforcescompetitive-programmingdata-structures
CF 1017F - The Neutral Zone

We are asked to evaluate a number-theoretic sum over all integers from 1 to $n$, where each integer contributes a value defined through its prime factorization. For a number $x$, we factor it as a product of primes $x = prod pi^{ai}$.

codeforcescompetitive-programmingbrute-forcemath
CF 1017E - The Supersonic Rocket

Each engine is a finite set of points in the plane, and we are allowed to rigidly move each set independently before they interact. Rigid motion here means we can translate and rotate a set arbitrarily, but not deform it.

codeforcescompetitive-programminggeometryhashingstrings
CF 1017D - The Wu

We are given a collection of binary strings, all of the same fixed length $n le 12$. Each string in this collection appears with multiplicity, so duplicates matter. Alongside this, every position $i$ has a non-negative weight $wi$.

codeforcescompetitive-programmingbitmasksbrute-forcedata-structures
CF 1017B - The Bits

We are given two binary strings of equal length. You are allowed to pick any two positions in the first string and swap their bits. The second string stays fixed.

codeforcescompetitive-programmingimplementationmath
CF 1017C - The Phone Number

We are given a number $n$, and we must construct a permutation of the integers from $1$ to $n$. Among all such permutations, we are asked to minimize a quantity defined on the permutation. This quantity is the sum of two classical subsequence measures.

codeforcescompetitive-programmingconstructive-algorithmsgreedy
CF 1017A - The Rank

We are given a class of students, each identified by a unique integer id starting from 1. Every student has four exam scores corresponding to different subjects. The task is to rank all students by their total score across the four subjects.

codeforcescompetitive-programmingimplementation
CF 1019E - Raining season

We are given a tree where each edge represents a bidirectional road between two houses. Every road has two parameters: a base traversal time and a rate at which it worsens each day. On day $t$, the cost of an edge becomes a linear function $bi + ai cdot t$.

codeforcescompetitive-programmingdata-structuresdivide-and-conquertrees
CF 1019D - Large Triangle

We are given a set of points on a plane, each point representing a city. The task is to select three distinct cities such that the triangle formed by them has a prescribed area $S$. If no such triple exists, we must report failure.

codeforcescompetitive-programmingbinary-searchgeometrysortings
CF 1019B - The hat

We are given a circle of $n$ students, where $n$ is even. Each student holds an unknown integer, and the only structural guarantee about these values is local: any two neighbors on the circle differ by exactly one in value.

codeforcescompetitive-programmingbinary-searchinteractive
CF 1019C - Sergey's problem

We are given a directed graph with up to one million vertices and one million edges. The task is to choose a subset of vertices $Q$ with two properties that interact in a non-trivial way.

codeforcescompetitive-programmingconstructive-algorithmsgraphs
CF 1019A - Elections

The election has a fixed set of voters, each initially committed to one of several parties. The United Party is party 1, and its goal is not just to get a lot of votes, but to strictly outperform every other party in final vote count.

codeforcescompetitive-programmingbrute-forcegreedy
CF 1020A - New Building for SIS

The building can be thought of as a grid of points arranged in $n$ vertical columns (towers) and $h$ horizontal levels (floors).

codeforcescompetitive-programmingmath
CF 1020B - Badge

We are given a directed structure over students where each student points to exactly one other student. This forms a functional graph: every node has outdegree one, so starting from any node and repeatedly following pointers eventually forces us into a cycle.

codeforcescompetitive-programmingbrute-forcedfs-and-similargraphs
CF 1023G - Pisces

We are given a weighted tree where each edge represents a river with a travel time. Fish can move continuously along these edges: traversing an edge of length $l$ takes exactly $l$ days, and fish may also wait arbitrarily at vertices.

codeforcescompetitive-programmingdata-structuresflowstrees
CF 1023F - Mobile Phone Network

We are given a connected graph with two types of edges. One set is already fixed by a competitor, each with a known cost. The second set is ours: these edges form a forest, and we are allowed to assign any integer weights to them.

codeforcescompetitive-programmingdfs-and-similardsugraphstrees
CF 1023D - Array Restoration

We are given a final array of length n that was produced by repeatedly painting segments with increasing labels from 1 to q. During the i-th operation, a chosen segment is overwritten entirely with value i, and later operations can overwrite earlier ones.

codeforcescompetitive-programmingconstructive-algorithmsdata-structures
CF 1023E - Down or Right

We are given an unknown $n times n$ grid where each cell is either open or blocked. Movement is only allowed from a cell to its right neighbor or its bottom neighbor, and only if the destination cell is open.

codeforcescompetitive-programmingconstructive-algorithmsinteractivematrices
CF 1023A - Single Wildcard Pattern Matching

We are given a pattern string s and a target string t. The pattern looks almost like a normal string of lowercase letters, except that it may contain a single special character .

codeforcescompetitive-programmingbrute-forceimplementationstrings
CF 1023C - Bracket Subsequence

We are given a correctly balanced bracket sequence, meaning every prefix of the string never has more closing brackets than opening ones, and in total the counts match perfectly.

codeforcescompetitive-programminggreedy
CF 1023B - Pair of Toys

We are given a range of toy prices that is completely regular: there are toys priced from 1 up to n, each integer price appearing exactly once. We want to count how many distinct unordered pairs of different toys have a combined price exactly equal to k.

codeforcescompetitive-programmingmath
CF 1025G - Company Acquisitions

We are given a system of startups arranged into a structure where some startups are already “active” and others are “acquired” and attached to exactly one active startup.

codeforcescompetitive-programmingconstructive-algorithmsmath
CF 1025E - Colored Cubes

We are given an $n times n$ grid and $m$ identical-sized cubes, each having a unique color. Each cube starts on a distinct cell, and each also has a target cell where it must eventually be placed.

codeforcescompetitive-programmingconstructive-algorithmsimplementationmatrices
CF 1025F - Disjoint Triangles

We are given a set of points in the plane with two strong structural guarantees: no two points coincide and no three are collinear. From these points we can form triangles by choosing any three vertices, and every such triangle is non-degenerate.

codeforcescompetitive-programminggeometry
CF 1025D - Recovering BST

We are given a sorted list of distinct integers, and we want to build a binary search tree using exactly these values as node keys.

codeforcescompetitive-programmingbrute-forcedpmathnumber-theorytrees
CF 1025C - Plasticine zebra

We are given a binary string made of two symbols, black and white, and we want to extract a long contiguous segment that alternates perfectly between the two colors.

codeforcescompetitive-programmingconstructive-algorithmsimplementation
CF 1025B - Weakened Common Divisor

We are given a collection of pairs of integers. From each pair, we are allowed to pick exactly one number. After making one choice per pair, we obtain a multiset of selected values. The task is to find an integer greater than 1 that divides every chosen value.

codeforcescompetitive-programmingbrute-forcegreedynumber-theory
CF 1025A - Doggo Recoloring

We are given a string representing the colors of a line of puppies, where each character is a color from 'a' to 'z'. The goal is to determine whether we can transform this string so that all characters become the same, using a very specific operation.

codeforcescompetitive-programmingimplementationsortings
CF 1027F - Session in BSU

We are given a collection of tasks, each task representing an exam that must be scheduled on exactly one of two possible days. For exam $i$, there are two candidate days $ai$ and $bi$, and we must choose one of them.

codeforcescompetitive-programmingbinary-searchdfs-and-similardsugraph-matchingsgraphs
CF 1027G - X-mouse in the Campus

We are given a system of rooms labeled from 0 to $m-1$. A token, called the x-mouse, starts in an unknown room and evolves deterministically: if it is in room $i$, after one second it moves to $i cdot x bmod m$.

codeforcescompetitive-programmingbitmasksmathnumber-theory
CF 1027E - Inverse Coloring

We are counting binary matrices of size $n times n$, where each cell is either black or white, but with two structural restrictions. The first restriction is a strong symmetry condition called “beauty”.

codeforcescompetitive-programmingcombinatoricsdpmath
CF 1027D - Mouse Hunt

We can think of the dorm as a directed graph where each room has exactly one outgoing edge. From every room i, the mouse deterministically moves to a[i] after one second.

codeforcescompetitive-programmingdfs-and-similargraphs
CF 1027C - Minimum Value Rectangle

We are given several independent sets of stick lengths. For each set, we need to pick exactly four sticks such that they form a rectangle, meaning we need two equal pairs of lengths: one pair for the height and one pair for the width.

codeforcescompetitive-programminggreedy
CF 1027A - Palindromic Twist

We are given several independent strings. Each string has even length, and every character is a lowercase English letter. For each character, we are forced to modify it exactly once, and the modification rule is fixed: we can only move one step in the alphabet either down or up.

codeforcescompetitive-programmingimplementationstrings
CF 1027B - Numbers on the Chessboard

We are working with an $n times n$ grid whose cells are filled with the numbers from $1$ to $n^2$, but not in a simple linear order. Instead, the grid is split into two disjoint groups of cells based on the parity of the sum of coordinates.

codeforcescompetitive-programmingimplementationmath
CF 1028H - Make Square

We are given a long array, and we repeatedly answer queries on subsegments of it. For each query range, we are allowed to slightly modify numbers in that segment by multiplying or dividing by primes, where each such operation changes a number by a single prime factor.

codeforcescompetitive-programmingmath
CF 1028G - Guess the number

We are trying to identify an unknown integer $x$ in a very large range from $1$ to $M = 10004205361450474$. Instead of asking direct yes or no questions, we are allowed to submit up to five queries, where each query is an increasing list of integers.

codeforcescompetitive-programmingdpinteractive
CF 1028E - Restore Array

We are given a cyclic structure of length $n$, where each position in the hidden array $a$ produces an observed value $bi$ through a modulo operation with its next neighbor.

codeforcescompetitive-programmingconstructive-algorithms
CF 1028F - Make Symmetrical

We maintain a dynamic set of points on the integer grid. The set changes over time through insertions and deletions.

codeforcescompetitive-programmingbrute-force
CF 1028D - Order book

We are given a chronological log of events in a trading system where orders are inserted and then later executed. Each order has a unique price and, when it is created, it could be either a buy order or a sell order, but this direction is not recorded in the log.

codeforcescompetitive-programmingcombinatoricsdata-structuresgreedy
CF 1028C - Rectangles

We are given a collection of axis-aligned rectangles on a 2D integer grid. Each rectangle is described by its bottom-left corner and top-right corner, and it includes its boundary as well as its interior.

codeforcescompetitive-programminggeometryimplementationsortings
CF 1028B - Unnatural Conditions

We are asked to construct two positive integers, call them $a$ and $b$, such that their digit sums are both large enough, while the digit sum of their sum is kept small.

codeforcescompetitive-programmingconstructive-algorithmsmath
CF 1028A - Find Square

We are given a grid of size $n times m$ consisting of two types of cells, white and black. Initially the entire grid is white, but at some point a single square region with an odd side length was painted black. That square is axis-aligned and fully filled with black cells.

codeforcescompetitive-programmingimplementation
CF 1029E - Tree with Small Distances

We are given a tree where vertex 1 plays a special role: it is the root of our concern. The structure is initially fixed, but we are allowed to add new edges between any two previously unconnected vertices.

codeforcescompetitive-programmingdpgraphsgreedy
CF 1029D - Concatenated Multiples

We are given a list of positive integers and a modulus $k$. For every ordered pair of distinct indices $(i, j)$, we form a new number by writing $ai$ directly followed by $aj$ in decimal representation. We need to count how many such ordered concatenations are divisible by $k$.

codeforcescompetitive-programmingimplementationmath
CF 1029F - Multicolored Markers

We are given two counts, $a$ red tiles and $b$ blue tiles, and we must place them on an infinite grid so that all colored tiles together form a single axis-aligned rectangle of area $a+b$.

codeforcescompetitive-programmingbinary-searchbrute-forcemathnumber-theory
CF 1029C - Maximal Intersection

We are given a collection of segments on a number line, and we want to understand how much they overlap if we remove exactly one of them.

codeforcescompetitive-programminggreedymathsortings
CF 1029B - Creating the Contest

We are given a sorted list of distinct problem difficulties. From this list, we must choose a subsequence, not necessarily contiguous, that will form a contest.

codeforcescompetitive-programmingdpgreedymath
CF 1029A - Many Equal Substrings

We are given a pattern string t of length n and a target number k. We need to build a new string s as short as possible such that when we slide a window of length n across s, the pattern t appears exactly k times as a substring.

codeforcescompetitive-programmingimplementationstrings
CF 1030F - Putting Boxes Together

We are given a fixed line of cells where each box sits at a distinct integer coordinate. Each box also has a weight, and that weight determines how expensive it is to move that box one step left or right.

codeforcescompetitive-programmingdata-structures
CF 1030G - Linear Congruential Generator

We are given a system that evolves an $n$-dimensional vector over discrete steps. Each coordinate evolves independently using the same type of rule: a linear transformation modulo a prime.

codeforcescompetitive-programmingnumber-theory
CF 1030E - Vasya and Good Sequences

We are given an array of integers, and we need to count how many contiguous subarrays have a special property. A subarray is considered valid if we can “rearrange bits inside each number independently” by swapping any two bits in its binary representation any number of…

codeforcescompetitive-programmingbitmasksdp
CF 1030D - Vasya and Triangle

We are working inside an integer grid that forms a rectangle from the origin to the point $(n,m)$. We need to pick three lattice points inside or on the border of this rectangle and form a triangle whose area is exactly $frac{nm}{k}$.

codeforcescompetitive-programminggeometrynumber-theory
CF 1030B - Vasya and Cornfield

The task is to classify points in a plane relative to a fixed geometric region defined by four boundary points: $(0, d)$, $(d, 0)$, $(n, n-d)$, and $(n-d, n)$.

codeforcescompetitive-programminggeometry
CF 1030C - Vasya and Golden Ticket

We are given a sequence of digits written as a single string. The task is to decide whether we can split this sequence into several consecutive parts, at least two parts, such that every part has exactly the same sum of digits.

codeforcescompetitive-programmingimplementation
CF 1030A - In Search of an Easy Problem

We are given a small group of people, each giving a binary opinion about a single problem. Each response is either 0, meaning the person considers the problem easy, or 1, meaning the person considers it hard.

codeforcescompetitive-programmingimplementation
CF 1031F - Familiar Operations

Stopped thinking

codeforcescompetitive-programmingbrute-forcegraphsmath
CF 1031E - Triple Flips

We are given a binary array and a single operation that flips exactly three positions, but those three positions must form an arithmetic progression.

codeforcescompetitive-programmingconstructive-algorithms
CF 1031B - Curiosity Has No Limits

We are given two arrays of length $n-1$. Each position $i$ describes a relationship between two unknown consecutive values $ti$ and $t{i+1}$, where each $ti$ is an integer in the range $[0, 3]$, meaning we can treat every value as a 2-bit number.

codeforcescompetitive-programming
CF 1031C - Cram Time

Lesha has two separate time budgets, one for today and one for tomorrow. Each lecture note has a fixed reading cost equal to its index: note 1 takes 1 hour, note 2 takes 2 hours, and so on.

codeforcescompetitive-programminggreedy
CF 1031D - Minimum path

We are given an $n times n$ grid of lowercase letters. We are allowed to modify at most $k$ cells, changing their letters to any lowercase character we want.

codeforcescompetitive-programminggreedy
CF 1031A - Golden Plate

We are working with a rectangular grid of size $w times h$, where each cell can be thought of as a unit square on a plate. We repeatedly draw “rings” of gilding on this grid. The first ring covers the outer border of the whole rectangle.

codeforcescompetitive-programmingimplementationmath