0% found this document useful (0 votes)
100 views15 pages

Nerc 2019 en

Abigail is an apprentice studying to become a blacksmith. She wants to plan her learning trajectory to make as many swords as possible under the supervision of n masters. Each master is available to work for specific periods of time. The problem is to calculate the maximum number of swords Abigail can produce given the masters' schedules and the time required to make each sword.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views15 pages

Nerc 2019 en

Abigail is an apprentice studying to become a blacksmith. She wants to plan her learning trajectory to make as many swords as possible under the supervision of n masters. Each master is available to work for specific periods of time. The problem is to calculate the maximum number of swords Abigail can produce given the masters' schedules and the time required to make each sword.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ICPC 2019–2020, NERC – Northern Eurasia Finals

St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem A. Apprentice Learning Trajectory


Time limit: 3 seconds

Abigail is an apprentice studying to become a blacksmith. She wants to plan her learning trajectory and
make as many swords as possible on her way to becoming a famous expert.
There are n masters willing to host her as their apprentice. The i-th master will start working at the
minute ai and end working at the minute bi , working for a total of bi − ai minutes. During this interval
of time, Abigail can work at this master’s forge. She can enter and leave the forge several times and
produce one or several swords upon each arrival. However, in order to produce a sword under supervision
of the i-th master she has to work there for ti minutes in a row. She can’t leave the sword unfinished and
continue working on it upon her next arrival to this forge.
Help Abigail make an optimal plan and calculate the maximum number of swords she can produce under
the supervision of n masters.

Input
The first line contains integer n (1 ≤ n ≤ 200 000) — the number of masters.
Each of the next n lines contains three integers ai , bi , ti (1 ≤ ai < ai + ti ≤ bi ≤ 1018 ) — the start and the
end time of master’s work, and the time needed to make one sword in their forge.

Output
Output the maximum number of swords Abigail can produce using the optimal learning trajectory.

Examples
standard input standard output Illustration
2 5
5 7 1 2
1 9 2 1

0 1 2 3 4 5 6 7 8 9 10

3 4
1 10 4 3
6 12 3 2
1
9 13 2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

3 4
1 13 4 3
6 11 2 2
1
9 13 3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Page 1 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem B. Balls of Buma


Time limit: 3 seconds

Balph is learning to play a game called Buma. In this game, he is given a row of colored balls. He has
to choose the color of one new ball and the place to insert it (between two balls, or to the left of all the
balls, or to the right of all the balls).
When the ball is inserted the following happens repeatedly: if some segment of balls of the same color
became longer as a result of a previous action and its length became at least 3, then all the balls of this
segment are eliminated.
Consider, for example, a row of balls ‘AAABBBWWBB’. Suppose Balph chooses a ball of color ‘W’ and the
place to insert it after the sixth ball, i. e. to the left of the two ‘W’s. After Balph inserts this ball, the balls
of color ‘W’ are eliminated, since this segment was made longer and has length 3 now, so the row becomes
‘AAABBBBB’. The balls of color ‘B’ are eliminated now, because the segment of balls of color ‘B’ became
longer and has length 5 now. Thus, the row becomes ‘AAA’. However, none of the balls are eliminated now,
because there is no elongated segment.
Help Balph count the number of possible ways to choose a color of a new ball and a place to insert it that
leads to the elimination of all the balls.

Input
The only line contains a non-empty string of uppercase English letters of length at most 3 · 105 . Each
letter represents a ball with the corresponding color.

Output
Output the number of ways to choose a color and a position of a new ball in order to eliminate all the
balls.

Examples
standard input standard output
BBWWBB 3
BWWB 0
BBWBB 0
OOOWWW 0
WWWOOOOOOWWW 7

Page 2 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem C. Cactus Revenge


Time limit: 3 seconds

NE(E)RC featured a number of problems in previous years about cactuses — connected undirected graphs
in which every edge belongs to at most one simple cycle. Intuitively, a cactus is a generalization of a tree
where some cycles are allowed. The traditional cactus that was initially used in NEERC 2005 problem is
given on the second picture in the Examples section.
You are given n integers d1 , d2 , . . . , dn . Construct any cactus with n vertices such that vertex i has degree di
(i. e. exactly di incident edges), or determine that no such cactus exists. Parallel edges and loops are not
allowed.

Input
The first line contains a single integer n (2 ≤ n ≤ 2 000) — the number of vertices in the cactus.
The second line contains n integers d1 , d2 , . . . , dn (1 ≤ di ≤ n − 1) — the desired vertex degrees.

Output
If it’s impossible to construct a cactus satisfying the conditions, output a single integer −1.
Otherwise, by tradition, output the constructed cactus as a set of edge-distinct paths.
In the first line output an integer m — the number of such paths. Each of the following m lines should
contain a path in the graph. A path should start with an integer ki (ki ≥ 2) followed by ki integers from
1 to n. These ki integers should represent consecutive vertices of this path. Adjacent vertices in the path
should be distinct. The path can visit the same vertex multiple times, but every edge of the cactus should
be traversed exactly once in the whole output.

Examples
standard input standard output Illustration
5 4 2
2 2 3 2 1 2 1 2
2 2 3
3 4 5
2 3 1
3 3 4 5 1

4 -1
3 3 2 2
6 -1
1 2 1 1 2 1
15 3 6
1 4 3 2 2 2 2 2 4 4 2 2 2 2 2 9 1 2 3 4 5 6 7 8 3
7 2 9 10 11 12 13 10 13 12
7 5
5 2 14 9 15 10
8 4
10 11

3 9 15

2 14

Note
Both in the second and the third example, there exist graphs that satisfy the given conditions but none
of them are cactuses.

Page 3 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem D. DevOps Best Practices


Time limit: 3 seconds

Daisy is a senior software engineer at RainyDay, LLC. She has just implemented three new features in
their product: the first feature makes their product work, the second one makes their product fast, and
the third one makes their product correct. The company encourages at least some testing of new features,
so Daisy appointed her intern Demid to write some tests for the new features.
Interestingly enough, these three features pass all the tests on Demid’s development server, which has
index 1, but might fail the tests on some other servers.
After Demid has completed this task, Daisy appointed you to deploy these three features to all n servers
of your company. For every feature f and every server s, Daisy told you whether she wants the feature f
to be deployed on the server s. If she wants it to be deployed, it must be done even if the feature f fails
the tests on the server s. If she does not want it to be deployed, you may not deploy it there.
Your company has two important instruments for the deployment of new features to servers: Continuous
Deployment (CD) and Continuous Testing (CT). CD can be established between several pairs of servers,
forming a directed graph. CT can be set up on some set of servers.
If CD is configured from the server s1 to the server s2 then every time s1 receives a new feature f the
system starts the following deployment process of f to s2 :

• If the feature f is already deployed on the server s2 , then nothing is done.

• Otherwise, if CT is not set up on the server s1 , then the server s1 just deploys the feature f to the
server s2 without any testing.

• Otherwise, the server s1 runs tests for the feature f . If the tests fail on the server s1 , nothing is
done. If the tests pass, then the server s1 deploys the feature f to the server s2 .

You are to configure the CD/CT system, and after that Demid will deploy all three features on his
development server. Your CD/CT system must deploy each feature exactly to the set of servers that
Daisy wants.
Your company does not have a lot of computing resources, so you can establish CD from one server to
another at most 264 times.

Input
The first line contains integer n (2 ≤ n ≤ 256) — the number of servers in your company.
Next n lines contain three integers each. The j-th integer in the i-th line is 1 if Daisy wants the j-th
feature to be deployed to the i-th server, or 0 otherwise.
Next n lines contain three integers each. The j-th integer in the i-th line is 1 if tests pass for the j-th
feature on the i-th server, or 0 otherwise.
Demid’s development server has index 1. It is guaranteed that Daisy wants all three features to be deployed
to the server number 1, and all three features pass their tests on the server number 1.

Output
If it is impossible to configure CD/CT system with CD being set up between at most 264 pairs of servers,
then output the single line “Impossible”.
Otherwise, the first line of the output must contain the line “Possible”.
Next line must contain n space-separated integers — the configuration of CT. The i-th integer should be
1 if you set up CT on the i-th server, or 0 otherwise.
Next line must contain the integer m (0 ≤ m ≤ 264) — the number of CD pairs you want to set up.

Page 4 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Each of the next m lines must describe CD configuration, each line with two integers si and ti
(1 ≤ si , ti ≤ n; si ̸= ti ), establishing automated deployment of new features from the server si to
the server ti .

Examples
standard input standard output
3 Possible
1 1 1 1 1 1
1 0 1 2
1 1 1 3 2
1 1 1 1 3
0 0 0
1 0 1
2 Impossible
1 1 1
0 0 1
1 1 1
1 1 0

Note
CD/CT system for the first sample test is shown below.

Page 5 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem E. Elections
Time limit: 3 seconds

Byteburg Senate elections are coming. Usually “United Byteland”, the ruling Byteland party, takes all the
seats in the Senate to ensure stability and sustainable development. But this year there is one opposition
candidate in one of the constituencies. Even one opposition member can disturb the stability in the Senate,
so the head of the Party asks you to ensure that the opposition candidate will not be elected.
There are n candidates, numbered from 1 to n. Candidate n is the opposition candidate. There are
m polling stations in the constituency, numbered from 1 to m. You know the number of votes cast for
each candidate at each polling station. The only thing you can do to prevent the election of the opposition
candidate is to cancel the election results at some polling stations. The opposition candidate will be elected
if the sum of the votes cast in their favor at all non-canceled stations will be strictly greater than the
analogous sum for every other candidate.
Your task is to prevent the election of the opposition candidate by canceling the election results at the
minimal possible number of polling stations. Notice that solution always exists, because if you cancel
the elections at all polling stations, the number of votes for each candidate will be 0, and the opposition
candidate will not be elected.

Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100; 1 ≤ m ≤ 100) — the number
of candidates and the number of polling stations. The next m lines contain the election results at each
polling station with n numbers on each line. In the i-th line the j-th number is ai,j — the number of votes
cast for the candidate j at the station i (0 ≤ ai,j ≤ 1 000).

Output
In the first line output integer k — the minimal number of the polling stations in which you need to cancel
the election results. In the second line output k integers — the indices of canceled polling stations, in any
order. If there are multiple ways to cancel results at k stations, output any one of them.

Examples
standard input standard output
5 3 2
6 3 4 2 8 3 1
3 7 5 6 7
5 2 4 7 9
2 1 0
1 1
3 3 3
2 3 8 1 2 3
4 2 9
3 1 7

Note
In the first example, the candidates from 1 to 5 received 14, 12, 13, 15, and 24 votes correspondingly.
The opposition candidate has the most votes. However, if you cancel the election results at the first and
the third polling stations, then only the result from the second polling station remains and the vote sums
become 3, 7, 5, 6, and 7, without the opposition candidate being in the lead anymore.

Page 6 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem F. Foolprüf Security


Time limit: 3 seconds

Alice and Bob obtained a map of the secret underground facility. The facility consists of n security units
and m chemical labs, connected by bidirectional tunnels. The map of this facility forms a tree: there are
exactly n + m − 1 tunnels, and there are no cycles. Vertices corresponding to security units have numbers
from 1 to n, chemical labs have numbers from n + 1 to n + m. Each tunnel connects a security unit to a
chemical lab; there are no tunnels between two security units or two chemical labs.
In case Alice or Bob gets captured, they decided to split the map into two pieces. To do that, they
calculated the Prüfer code of the tree. Alice then saved some of the numbers between 1 and n to her data
storage in the same order as they go in the original code, and Bob saved some of the numbers from n + 1
to n + m to his storage in the same way.
A Prüfer code of a tree on k vertices is a sequence of k − 2 integers from 1 to k, constructed as follows.
Find the leaf (a vertex with degree one) with the smallest label, remove it from the tree, then print the
label of its only neighbor. Repeat this k − 3 more times, until only one edge remains. The printed sequence
of k − 2 vertex labels is the Prüfer code.
Alice and Bob safely returned and they are ready to combine their data to restore the original map. They
could make a mistake during the backup, meaning no such map exists. Alice and Bob need your help to
restore any possible map of the facility consistent with the collected data, so that both Alice’s and Bob’s
parts are subsequences of the Prüfer code of the map.

Input
The first line of the input contains four integers n, m, ka , and kb (2 ≤ n, m ≤ 105 ; 1 ≤ ka , kb ;
ka + kb ≤ n + m − 2). The second line contains ka integers a1 , a2 , . . . , aka (1 ≤ ai ≤ n) — Alice’s
part of the map. The third line contains kb integers b1 , b2 , . . . , bkb (n + 1 ≤ bi ≤ n + m) — Bob’s part of
the map.

Output
If there’s no such map, print “No”.
Otherwise, print “Yes” on the first line, followed by n + m − 1 lines describing the possible facility map.
Each line should contain two integers ui and vi — the security unit and the chemical lab connected by
the i-th tunnel of the facility.

Examples
standard input standard output Illustration
4 5 4 2 Yes
1 3 3 4 1 5 1 2 3 4
7 8 1 6
2 7
6 3
3 7 5 6 7 8 9
9 4
3 8
4 8
4 3 3 1 No
3 2 2
6

Note
The Prüfer code of the tree in the first example is (7, 1, 6, 3, 3, 8, 4).

Page 7 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem G. Game Relics


Time limit: 3 seconds

Esports is a form of competitive sports using video games. Dota 2 is one of the most popular competitive
video games in Esports. Recently, a new video game Dota 3 was released. In Dota 3 a player can buy
some relics for their hero. Relics are counters that track hero’s actions and statistics in a game.
Gloria likes to play Dota 3, so she wants to buy all n available relics for her favorite hero.
Relics can be bought using an in-game currency called shards. Each relic has its own price — ci shards
for the i-th relic. A player can buy a relic using one of the following options:

• Pay ci shards to buy the i-th relic;

• Pay x shards and randomly get one of all n relics. The probability of getting a relic is the same for
all n relics. If a duplicate relic is received, then the relic is recycled and x2 shards are given back to
the player.

Gloria wants to buy all n relics. Help her minimize the expected number of shards she spends to buy all
the relics.

Input
The first line contains two integers n and x (1 ≤ n ≤ 100; 1 ≤ x ≤ 10 000) — the number of relics and
the cost to receive a random relic.

The second line consists of n integers c1 , c2 , . . . , cn (x ≤ ci ≤ 10 000; ci ≤ 10 000) — the prices of
n relics.

Output
Print a single real number — the minimum expected number of shards that Gloria must spend to buy all
the relics.
The absolute or relative error should not exceed 10−9 .

Examples
standard input standard output
2 20 47.50000000000000000
25 100
4 30 171.25000000000000000
60 50 60 80

Note
In the first example, the optimal strategy is to randomly get one of the two relics paying 20 shards. Then
there are two scenarios.
The first one happens if Gloria receives the first relic. Then she keeps getting random relics until she
obtains the second relic. The expected number of shards to spend in this scenario is 20 + 30 = 50.
In the second scenario, Gloria initially gets the second relic. Then it is better to buy the first relic for 25
shards, so the expected number of shards to spend in this scenario is 20 + 25 = 45.
50+45
Thus, the expected number of shards to spend is 2 = 47.5.

Page 8 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem H. Help BerLine


Time limit: 5 seconds

Very soon, the new cell phone services provider “BerLine” will begin its work in Berland!
The start of customer service is planned along the main street of the capital. There are n base stations
that are already installed. They are located one after another along the main street in the order from
the 1-st to the n-th from left to right.
Currently, all these base stations are turned off. They will be turned on one by one, one base station per
day, according to some permutation p = [p1 , p2 , . . . , pn ] (1 ≤ pi ≤ n), where pi is the index of a base
station that will be turned on on the i-th day. Thus, it will take n days to turn on all base stations.
Each base station is characterized by its operating frequency fi — an integer between 1 and 24, inclusive.
There is an important requirement for operating frequencies of base stations. Consider an arbitrary
moment in time. For any phone owner, if we consider all base stations turned on in the access area
of their phone, then in this set of base stations there should be at least one whose operating frequency
is unique among the frequencies of these stations. Since the power of the phone and the position are not
known in advance, this means that for any nonempty subsegment of turned on base stations, at least one
of them has to have the operating frequency that is unique among the stations of this subsegment.
For example, let’s take a look at a case of n = 7, all n stations are turned on, and their frequencies are
equal to f = [1, 2, 1, 3, 1, 2, 1]. Consider any subsegment of the base stations — there is a base station
with a unique frequency within this subsegment. However, if f = [1, 2, 1, 2, 3, 2, 1], then there is no unique
frequency on the segment [1, 2, 1, 2] from the index 1 to the index 4, inclusive.
Your task is to assign a frequency from 1 to 24 to each of n base stations in such a way that the frequency
requirement is met at every moment. Remember that the base stations are turned on in the order of the
given permutation p.

Input
The first line of the input contains an integer t (1 ≤ t ≤ 50) — the number of test cases in the input.
Then t test case descriptions follow.
The first line of a test case contains an integer n (1 ≤ n ≤ 8 500) — the number of “BerLine” base stations.
The following line contains n distinct integers p1 , p2 , . . . , pn (1 ≤ pi ≤ n) — the order in which the base
stations are turned on, i. e. on the i-th day the base station with the index pi is turned on.
It is guaranteed that a correct answer exists for all test cases in the input.

Output
Print exactly t lines, where the j-th line contains the answer for the j-th test case in the input. Print the
required frequencies f1 , f2 , . . . , fn (1 ≤ fi ≤ 24). If there are several possible answers, print any of them.

Page 9 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Example
standard input standard output
5 1 3 2
3 10 20 10
1 3 2 1
3 2 3 4 5 3 1 3 5 4 2
1 2 3 1 2 3 4 5 6 7 8 9 10
1
1
10
6 10 4 2 7 9 5 8 3 1
10
2 4 6 9 1 8 10 5 3 7

Note
In the first test case n = 3 and p = [1, 3, 2]. The base stations can be assigned frequencies [1, 3, 2].

• Day 1: only the base station 1 is turned on, its frequency is 1.

• Day 2: the base stations 1 and 3 are turned on, their frequencies are [1, 2].

• Day 3: all base stations are turned on, their frequencies are [1, 3, 2] (in the direction along the street).

On each day, each nonempty subsegment of turned on base stations has a base station with a unique
frequency among this subsegment. It can be shown that three distinct frequencies are necessary in this
test case.

Page 10 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem I. Intriguing Selection


Time limit: 5 seconds

This is an interactive problem.


You are the head coach of a chess club. The club has 2n players, each player has some strength which
can be represented by a number, and all those numbers are distinct. The strengths of the players are not
known to you.
You need to select n players who would represent your club in the upcoming championship. Naturally,
you want to select n players with the highest strengths.
You can organize matches between the players to do that. In every match, you pick two players, they play
some games, and you learn which one of the two has higher strength. You can wait for the outcome of a
match before deciding who will participate in the next one.
However, you do not want to know exactly how those n players compare between themselves, as that
would make the championship itself less intriguing. More formally, you must reach a state where there is
exactly one way to choose n players with the highest strengths that is consistent with the outcomes of the
matches you organized, but there must be at least two possible orderings of those n players by strength
that are consistent with the outcomes of the matches you organized.

Interaction Protocol
Your program has to process multiple test cases in one run. First, it should read the integer t (t ≥ 1) —
the number of test cases. Then, it should process the test cases one by one.
In each test case, your program should start by reading the integer n (3 ≤ n ≤ 100) — the number of
players to select out of 2n players. The sum of squares of the values of n over all test cases does not exceed
10 000.
Then your program can organize matches zero or more times. To organize a match, your program should
print a match description formatted as ? i j — a question mark followed by two distinct numbers of
players participating in the match. The players are numbered from 1 to 2n, inclusive. Remember to flush
the output after printing the match description. Then your program should read the match outcome —
it will be either the greater-than character (>), if the first player in the match description has higher
strength, or the less-than character (<), if the second player in the match description has higher strength.
Your program can organize at most 4n2 matches. After it is done organizing matches, it should print the
exclamation mark (!) and continue to the next test case, or exit gracefully if this was the last test case.
Remember to flush the output after printing the exclamation mark.
There must be exactly one way to choose n players with the highest strength that is consistent with the
outcomes of the matches you organized, but there must be at least two possible orderings of those n
players by their strength that are consistent with the outcomes of the matches you organized.
The judging program picks some distinct numbers as the strengths of all players before your program
starts organizing matches and uses them to answer the requests.

Page 11 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Example
standard input standard output
2
3
? 1 3
>
? 4 2
<
? 4 5
>
? 6 5
<
? 3 4
>
? 5 6
>
!
3
? 3 4
<
? 4 2
<
? 5 3
<
? 6 4
>
? 3 1
>
!

Note
In the example, the players in the first test case are sorted by strength in decreasing order. From the
matches in the example output, we can deduce that players 1, 2, and 3 have the highest strength, but we
do not know how the player 1 compares to the player 2.

Page 12 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem J. Just Arrange the Icons


Time limit: 5 seconds

BerPhone X is almost ready for release with n applications being preinstalled on the phone. A category of
an application characterizes a genre or a theme of this application (like “game”, “business”, or “education”).
The categories are given as integers between 1 and n, inclusive; the i-th application has category ci .
You can choose m — the number of screens and s — the size of each screen. You need to fit all n icons of
the applications (one icon representing one application) meeting the following requirements:

• On each screen, all the icons must belong to applications of the same category (but different screens
can contain icons of applications of the same category);

• Each screen must be either completely filled with icons (the number of icons on the screen is equal
to s) or almost filled with icons (the number of icons is equal to s − 1).

Your task is to find the minimal possible number of screens m.

Input
The first line contains an integer t (1 ≤ t ≤ 10 000) — the number of test cases in the input. Then t test
cases follow.
The first line of each test case contains an integer n (1 ≤ n ≤ 2 · 106 ) — the number of the icons. The
second line contains n integers c1 , c2 , . . . , cn (1 ≤ ci ≤ n), where ci is the category of the i-th application.
It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 2 · 106 .

Output
Print t integers — the answers to the given test cases in the order they follow in the input. The answer to
a test case is an integer m — the minimum number of screens on which all n icons can be placed satisfying
the given requirements.

Example
standard input standard output
3 3
11 3
1 5 1 5 1 5 1 1 1 1 5 4
6
1 2 2 2 2 1
5
4 3 3 1 2

Note
In the first test case of the example, all the icons can be placed on three screens of size 4: a screen with 4
icons of the category 1, a screen with 3 icons of the category 1, and a screen with 4 icons of the category 5.

Page 13 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem K. Key Storage


Time limit: 3 seconds

Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store
a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he
invented his own one.
Karl’s fingerprint is calculated by the following process: divide the given integer by 2, then divide the result
by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about
integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl’s fingerprint algorithm is applied to the key 11: 11 divided by 2 has
remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has
remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the
fingerprint multiset {1, 1, 2}.
Ksenia wants to prove that Karl’s fingerprint algorithm is not very good. For example, she found that
both keys 178800 and 123456 produce the fingerprint of {0, 0, 0, 0, 2, 3, 3, 4}. Thus, users are at risk of
fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that
have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to
help her.

Input
The first line contains an integer t (1 ≤ t ≤ 50 000) — the number of commonly used keys to examine.
Each of the next t lines contains one integer ki (1 ≤ ki ≤ 1018 ) — the key itself.

Output
For each of the keys print one integer — the number of other keys that have the same fingerprint.

Example
standard input standard output
3 0
1 1
11 127
123456

Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both
numbers have the fingerprint multiset {1, 1, 2}.

Page 14 of 15
ICPC 2019–2020, NERC – Northern Eurasia Finals
St. Petersburg, Barnaul, Tbilisi, and Almaty, December 1, 2019

Problem L. Lexicography
Time limit: 3 seconds

Lucy likes letters. She studied the definition of the lexicographical order at school and plays with it.
At first, she tried to construct the lexicographically smallest word out of given letters. It was so easy!
Then she tried to build multiple words and minimize one of them. This was much harder!
Formally, Lucy wants to make n words of length l each out of the given n · l letters, so that the k-th of
them in the lexicographic order is lexicographically as small as possible.

Input
The first line contains three integers n, l, and k (1 ≤ k ≤ n ≤ 1 000; 1 ≤ l ≤ 1 000) — the total number
of words, the length of each word, and the index of the word Lucy wants to minimize.
The next line contains a string of n · l lowercase letters of the English alphabet.

Output
Output n words of l letters each, one per line, using the letters from the input. Words must be sorted in
the lexicographic order, and the k-th of them must be lexicographically as small as possible. If there are
multiple answers with the smallest k-th word, output any of them.

Examples
standard input standard output
3 2 2 af
abcdef bc
ed
2 3 1 aab
abcabc bcc

Page 15 of 15

You might also like