N CPC 2023 Problems
N CPC 2023 Problems
NCPC 2023
October 7, 2023
Problems
A Aperiodic Appointments
B Berry Battle 2
C Converting Romans
D Die Hard
E Electronic Components
F Factor-Full Tree
G Groups of Strangers
H Heroes of Velmar
I Intertwined
J Jamboree
K Karl Coder
• Your solution programs must read input from standard input (e.g. System.in in Java or
cin in C++) and write output to standard output (e.g. System.out in Java or cout
in C++). For further details and examples, please refer to the documentation in the help
pages for your favorite language on Kattis.
• For information about which compiler flags and versions are used, please refer to the
documentation in the help pages for your favorite language on Kattis.
• Your submissions will be run multiple times, on several different inputs. If your submission
is incorrect, the error message you get will be the error exhibited on the first input on which
you failed. E.g., if your instance is prone to crash but also incorrect, your submission
may be judged as either “Wrong Answer” or “Run Time Error”, depending on which is
discovered first. The inputs for a problem will always be tested in the same order.
• If you think some problem is ambiguous or underspecified, you may ask the judges for a
clarification request through the Kattis system. The most likely response is “No comment,
read problem statement”, indicating that the answer can be deduced by carefully reading
the problem statement or by checking the sample test cases given in the problem, or that
the answer to the question is simply irrelevant to solving the problem.
• In general we are lenient with small formatting errors in the output, in particular whitespace
errors within reason, and upper/lower case errors are often (but not always) ignored. But
not printing any spaces at all (e.g. missing the space in the string “1 2” so that it becomes
“12”) is typically not accepted. The safest way to get accepted is to follow the output
format exactly.
• For problems with floating point output, we only require that your output is correct up to
some error tolerance. For example, if the problem requires the output to be within either
absolute or relative error of 10−4 , this means that
– If the correct answer is 0.05, any answer between 0.0499 and .0501 will be accepted.
– If the correct answer is 500, any answer between 499.95 and 500.05 will be accepted.
Any reasonable format for floating point numbers is acceptable. For instance, “17.000000”,
“0.17e2”, and “17” are all acceptable ways of formatting the number 17. For the definition
of reasonable, please use your common sense.
Problem A
Aperiodic Appointments
Nick has always struggled with maintaining habits. The problem is that he just can’t stop
maintaining them. If Nick does something K times in a row, he has to keep doing it forever.
Luckily, he has started visiting Dr Patternson, an expert in PBT (Pattern Breaking Therapy).
The principle of PBT is simple: Nick will visit Dr Patternson every day, and if he has done the
same thing K times in a row on a specific visit, the doctor will charge him money. This will
motivate Nick to not continue this habit.
PBT has worked out great for Nick, as he has now successfully quit all his habits. Except for
one, the habit of visiting Dr Patternson. The frequent visits are starting to take a toll on Nick’s
economy, so your task is to calculate how many times he has to pay the doctor for the next N
days.
Formally, let s = s1 s2 s3 . . . sN be a string consisting of zeroes and ones. A one means that
Nick has to pay the doctor on the ith day. This string is generated one character at a time, in the
following way:
1. si = 0 if i ≤ K.
2. If i > K, then si = 1 if the previous characters contains a pattern that repeats K times.
More specifically, let s0 = s1 s2 . . . si−1 . If there is a nonempty string t such that the last
|t| · K characters of s0 can be written as t + t + · · · + t, then si = 1. Otherwise si = 0.
You are given the numbers N and K, and your task is to calculate the number of ones in the
string s.
The picture represents Sample 1. An angry face means that Nick had to pay on the
corresponding day.
Input
The input consists of one line with the integers N and K (1 ≤ N ≤ 109 , 2 ≤ K ≤ 109 ).
Output
Print one integer, the number of ones in the string s.
berry there. There will be exactly N/2 berries to start with, and
they are distributed uniformly at random.
The berry picking takes place in turns. In one move, a person can choose a position i
(1 ≤ i ≤ N − 3), and pick all the berries at positions i, i + 1, i + 2, and i + 3. Grandpa makes
the first move, then Erik makes a move, then grandpa, and so on. Grandpa will always greedily
make a move that picks as many berries as possible. Among all such moves, he will pick one
uniformly at random.
You are given the string s, and your task is to write a program that decides what moves Erik
should make in order to pick at least as many berries as grandpa.
Interaction
The first line of input contains the number N , the length of the string s. This number will always
be equal to 105 , except for in the sample. Your program does not have to solve the sample to get
accepted.
The second line contains the string s of length N . This string has exactly N/2 characters b,
and their positions are generated uniformly at random.
Then, your program should start interacting with the grader. When it is grandpa’s turn,
you should read a single integer i (1 ≤ i ≤ N − 3), meaning that grandpa picks the berries at
positions i, i + 1, i + 2, and i + 3. When it is your turn, you should instead print a number i,
meaning that you pick the berries at positions i, i + 1, i + 2, and i + 3.
If it is your turn and there are no more berries, your program should terminate without
printing anything more. If it is grandpa’s turn and there are no berries, then the grader will not
make any more moves, and your program should also terminate without printing anything more.
If you picked at least as many berries as grandpa, you pass the test case.
Each turn, grandpa greedily makes a move that picks as many berries as possible, and among
all such moves, he chooses one uniformly at random.
The strings s were generated beforehand, so they are the same across different submissions.
However, grandpa’s behaviour is randomized every submission, so it is possible to get different
results if you submit the same code twice.
There will be 100 test cases. It is guaranteed that there exists a solution that passes this many
cases with very high probability.
Input
The inputs starts with an integer 0 < n ≤ 103 . Then follow n lines, each containing up to 3 · 105
Roman digits. The total number of Roman digits in the input is at most 3 · 105 .
Output
Output the corresponding Arabic numbers, one number per line.
Input
The input consists of three lines. Line i contains 6 positive integers xj (1 ≤ xj ≤ 1000),
describing the sides of the i’th die.
Output
Output the smallest i ∈ {1, 2, 3}, such that John can pick die i and be guaranteed to win with
probability at least 12 . If no such die exists, output “No dice”.
Input
The first line of input contains the integer N (1 ≤ N ≤ 1000).
The following N lines each contain two integers fi and ti (1 ≤ fi ≤ 104 , 1 ≤ ti ≤ 109 ).
Output
Print one integer, the minimum time to place all components.
Input
The first line contains an integer N (1 ≤ N ≤ 60).
The following N − 1 lines each contain two integers u and v (1 ≤ u, v ≤ N , u 6= v),
meaning that an edge goes between vertices u and v. These edges will form a tree.
Output
Print one line with N integers, the numbers x1 , x2 , . . . xN . These numbers must satisfy 1 ≤
xi ≤ 1018 . It can be shown that under these constraints, an answer always exists.
Input
The input consists of one line with the integers N and M (2 ≤ N ≤ 1000, 1 ≤ M ≤ 100000),
the number of employees and the number of pairs of employees that know each other. Next
follow M lines each containing two integers a and b (1 ≤ a 6= b ≤ N ), indicating that employee
a knows employee b.
It is guaranteed that for the given test cases, there is at least one way to divide the employees
into offices with HR managers and a CEO, like in the description. To summarize, the CEO is an
employee who knows at most 15 other people. The rest of the employees can be partitioned into
at most 8 offices, where each office has an employee (the HR manager) who knows all of the
other employees in that office. In addition, the CEO knows all of the HR managers. Note that
employees from different offices can know each other.
Output
If it is possible to divide the employees in at most three groups so that no pair that know each
other are in the same group, output one row with the group identity 1, 2, 3 for each employee in
the same order they were numbered in the input. Use a single space between two subsequent
employees’ group identities. If multiple solutions exist, you may output any. If no such group
partitioning exists, output “Impossible”.
Setup
1. Each player selects a deck of cards containing heroes with different abilities and power
levels.
2. The players shuffle their decks and draw a starting hand of 7 cards each.
3. Designate three distinct locations for the battle. Each location will have its own separate
battlefield.
Gameplay
1. Each player draws 1 card from the deck, to their hand, if possible. Maximum cards per
hand is 7.
2. Players take turns playing cards from their hand on any of the three locations.
3. On each turn, players have energy equal to the turn number, for example, on Turn 3, they
have 3 energy to spend.
4. Each card has a cost that must be paid using energy to play it on a location.
5. Players can play as many cards as they want as long as the following conditions are met:
6. The power level of each card represents its strength in the game.
2. The player who wins the most locations is the overall winner.
3. If the players win an equal number of locations, the total power level across all locations
is used as a tiebreaker to determine the winner.
4. If the total power level across all locations is also tied, then the game results in a tie.
Location Resolution
1. Locations are resolved separately at the end of the game.
2. Compare the total power levels of all cards played by each player at the location after
applying special abilities.
3. The player with the higher total power level wins the location.
Special Abilities
1. Some cards have special abilities that can affect the game. These abilities trigger at the
end of the game, before location resolution.
2. Abilities can buff the card’s power level, interact with other cards, or manipulate the game
state.
3. Two cards that portray the same character are considered to portray distinct characters for
the sake of abilities.
The game Heroes of Velmar offers a mix of strategic card play, resource management, and
tactical decision-making. Players must choose their heroes wisely, coordinate their plays, and
employ their unique abilities to outwit their opponents and emerge victorious in the epic battle
for control over Velmar!
Card design
Card Explanation:
• Top Left Corner: The energy cost required to play the card on the battlefield.
• Top Right Corner: The power level of the card, representing its strength.
• Text Underneath: The card’s ability, describing its unique effect during gameplay.
Input
Input consists of six lines representing the state of the three locations after six turns of play. This
means that there will be at most 24 cards total listed in the input.
First the left location is described, then the center location is described, and finally the right
location is described. Each location is described by two lines, the first of which represents player
1’s cards and the second of which represents player 2’s cards. Each line lists the number of cards
in the line and then the names of the cards played by the player, separated by spaces. There will
be at most four cards in each line.
Note that a player may leave a location empty. Each input is guaranteed to be a valid
reachable final game state according to the game rules.
Output
Output "Player 1" if player 1 won, "Player 2" if player 2 won, or "Tie" if there was
no victor.
Sample Input 1 Sample Output 1
3 Shadow Seraphina Ironwood Player 1
2 Voidclaw Voidclaw
1 Vexia
0
1 Ranger
0
Input
The first line contains an integer n, the number of pillars, and an integer d, the length of the rope
(1 ≤ n ≤ 105 , 1 ≤ d ≤ 109 ).
The following n lines each contain two integers xi , yi , the coordinates of the ith pillar
(−109 ≤ xi , yi ≤ 109 ) for i ∈ 1, 2, . . . , n. None of these pillars will lie on the rope.
Output
Print one line with an integer i, meaning that the rope will end up spinning around the ith pillar
in the input. Note that this index is 1-indexed. If the rope doesn’t collide with any pillars i = −1.
It is guaranteed that changing the input d by at most ±10−6 will not change i.
Input
The first line of input contains two positive integers N and M (1 ≤ N ≤ 2M , 1 ≤ M ≤ 100).
N is the number of useful items, and M is the number of scouts. The second line contains N
positive integers ai (1 ≤ ai ≤ 107 ) giving the sizes of the items.
Output
Print one integer, the smallest total size that any scout has to carry.
index · · · -1 0 1 2 3 4 5 6 7 8 ···
buf · · · × 78 67 80 67 0 0 0 0 × ···
Interaction
This is an interactive problem where interaction proceeds in rounds. In each round your program
can attempt to read a byte from the buffer or report the answer (the value of N ):
read: Write a line containing “buf[i]” if you want to try to read byte i of the buffer. If
0 ≤ i < 2N , then you will get the value of the byte stored in the buffer at index i. If
i < N then this will be an integer between 1 and 255, otherwise it will be 0. If i ≥ 2N or
i < 0, you will get the response “Segmentation fault (core dumped)” and
your program should exit.
answer: Write a line containing “strlen(buf) = M ” if you want to report that you think
that N = M . Afterwards your program should exit. Your submission will be accepted
if you answered correctly, if you did not trigger a segmentation fault, and if you did not
attempt to read entries from the buffer too many times.
A maximum of 2dlog2 N e reads can be made. If your program attempts to read from the buffer
after reaching the limit, it will get the response “Too many reads” and your program should
then exit.
It is guaranteed that 2 ≤ N ≤ 1018 .