NCPC 2024 Problems
NCPC 2024 Problems
NCPC 2024
October 5, 2024
Problems
A Avoiding the Abyss
B Baseball Court
C Composed Rhythms
D Double Deck
E Elapid Errands
F Fence Fee
G Guessing Passwords
H Hotfix
I Infinite Cash
J Jungle Game
K Knitting Pattern
• 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
Avoiding the Abyss
Input
The first line contains two integers xs and ys (−104 ≤ xs , ys ≤ 104 ).
The second line contains two integers xt and yt (−104 ≤ xt , yt ≤ 104 ).
The third line contains two integers xp and yp (−104 ≤ xp , yp ≤ 104 ).
The problem is not adaptive, i.e. for every test case there exist four integers xl , yl , xr , yr
(−104 ≤ xl < xr ≤ 104 , −104 ≤ yl < yr ≤ 104 ) that constitute a swimming pool. The start
and end points are always strictly outside the swimming pool, and the point (xp , yp ) is inside (or
on the border). The start and end points are always distinct.
Output
First, print one integer N (0 ≤ N ≤ 10), the number of points in between the start and end
point that you want to visit. Then, print N lines, the ith containing two integers xi , yi . These
coordinates must satisfy −109 ≤ xi , yi ≤ 109 . Note that these are not the same bounds than on
the other coordinates.
This means that you will walk along straight line segments between (xs , ys ), (x1 , y1 ), . . . , (xN , yN ), (xt , yt )
such that none of the line segments touch the swimming pool. It can be proven that a solution
always exists.
Input
The first line of the input contains a single positive integer N (1 ≤ N ≤ 104 ), the number
of grass tiles available. The second line of the input contains two positive integers a and b
(1 ≤ a, b ≤ 104 ), the size of the land the court can be made within.
Output
Print the number of valid ways to place the grass tiles to make up a baseball court. All n grass
tiles must be used. Since this number might be very large, print the answer modulo 109 + 7.
Input
The first and only line of input contains a single integer N (2 ≤ N ≤ 106 ), denoting the number
of beats in the rhythm.
Output
First output one line with an integer K, the number of groups of which your decomposition
consists. Then output a line with K space-separated integers, each of which is a 2 or a 3. Your
decomposition must be made up of the correct number of beats.
If there are multiple correct answers, you may output any of them.
Input
The first line of the input contains two integers N and K (1 ≤ N ≤ 104 , 1 ≤ K ≤ 15). The
second and third line of the input each contain N · K integers xi (1 ≤ xi ≤ N ), describing the
layout of the decks. The first number x1 is the topmost card in the deck, x2 is the second, and so
on.
No integer in the second line and third line is repeated more than K times per line.
Output
Print a single integer, the maximum possible score.
Output
Print a string consisting of the characters ‘<’, ‘>’, ‘^’, ‘v’. This is the list of moves you should
make so that you visit all the points in order without ever going to the same point more than
once. The string must have length at most 2 · 106 .
Input
The first line contains an integer F (3 ≤ F ≤ 1000), the number of fence line segments. The
next F lines contain four integers each, x1 , y1 , x2 , y2 (0 ≤ x1 , y1 , x2 , y2 ≤ 1000), representing a
straight-line fence section.
No two fence line segments intersect. Since fencing is both expensive and tedious, you may
assume that every fence line segment is necessary and serves to bound fields. All fences farmers
have are connected.
In other words, the graph that consists of endpoints and fences is planar, connected, and has
no bridges.
Output
Output a single line representing the sum of the squared areas of all the fields formed by the
given fence sections. Your answer will be correct if it has an absolute or relative error of at most
10−6 .
Explanation of sample
The picture represents sample 2. The areas of the two fields are 1 and 0.5, so the sum of their
squares is 1 + 0.25 = 1.25.
Input
The first line of the input contains two positive integers N and M (1 ≤ N, M ≤ 100). N is
the number of characters in the password and M is the number of guesses in the screenshot.
Next there are N lines, each with M characters, the i-th of which gives the colours of the i-th
guess. G denotes gray and Y denotes yellow. The number of Ys is constant across all lines. The
characters are given without spaces between them. Finally there is a single line with a single
positive integer Σ (1 ≤ Σ ≤ 106 ), giving the number of valid characters for the passwords.
Output
If there is no way to achieve the given colours print Bugged!. Otherwise print N + 1 lines
with M numbers each, separated by spaces. The first N lines should give the colouring in the
input if the number i denotes the i-th character in the alphabet. The final and (N + 1)-st line
should give a secret word that could have resulted in this sequence of guesses being valid. If
there are multiple possible solutions any one of them will be accepted.
Input
The input contains a single string of length at least 1 and at most 106 . It contains only ASCII
upper and lower case characters. This string is then followed by a single newline character.
Output
For each non-whitespace character that appears a non-zero number of times in the output of
the problem described above, print it along with its number of occurrences on a single line,
separated by a space. Print the lines ordered by ascending values of the ASCII characters.
Input
The input has three lines, each containing the positive integers s, d, m respectively. They satisfy
1 ≤ s, d, m ≤ 21000 . As these payment details are for a computer science job the numbers are
all given in binary, naturally.
Output
Print the number of the day that Svalur wants to spend money, but has none. This should
naturally also be printed in binary. If he can support his spending habits indefinitely instead
print Infinite money!.
pi + pj = Pk and si + sj = Sk
should never occur for any triple i, j, k (note that i can be equal to j).
The only thing left to do is to decide the N distinct pairs (p1 , s1 ), (p2 , s2 ) . . . , (pN , sN ) such
that 1 ≤ pi , si ≤ N and the situation above never happens.
Input
The first line contains the integer N (1 ≤ N ≤ 2000).
The following N lines contain the values of the challenge cards Pi , Si (2 ≤ Pi , Si ≤ 2 · N ).
Output
If there is no solution, print “NO”. Otherwise, print “YES” followed by N lines, each containing
a pair of integers pi , si (1 ≤ pi , si ≤ N ). These pairs of integers must be distinct. In other words,
you may not have two indices i 6= j with pi = pj and si = sj .
Input
The input contains two positive integers N , the length of the sweater, and P , the length of the
pattern. They satisfy 1 ≤ P ≤ N ≤ 1018 and they have the same parity, as otherwise the pattern
could never be perfectly centered.
Output
Print a single integer, the amount of empty space left on the back of the sweater.
Figure K.2: Applying the pattern to the full width for sample 1
In the second sample the sweater is 16 loops in circumference. The first pattern is placed
at loops 7, 8, 9 and 10. Two more are placed at 3, 4, 5, 6 and 11, 12, 13, 14. This leaves 1, 2,
15 and 16, which exactly fits one more pattern that will be perfectly centered at the back of the
sweater, creating no asymmetry. Thus that pattern is placed, leaving no empty space.
Figure K.4: Applying the pattern to the full width for sample 2