Problem A: Python File: Time Limit: 1 Second
Problem A: Python File: Time Limit: 1 Second
A file name consists of 2 parts: base name and extension name, separated by a dot character. For
example, Python source code files have extension name "py", documents have extension name
"doc" or "docx". In Windows OS, file names are case insensitive.
Write a program to input a file name as string and check whether it is considered Python source
code file in Windows or not.
Input
Only one line contain file name S ( 1 | S | 128 ). File name will only contain ‘a’-‘z’, ‘A’-‘Z’, ‘.’
and ‘_’.
Output
If file is a python file, print "yes", otherwise "no".
1
Problem B: Shoes Game
Time limit: 1 second
Memory limit: 256 Megabytes
N + 1 (N is odd number) shoes from (N+1)/2 pair of shoes the same type with different sizes are
lined up in arandom order. The game master secretly take one of the shoes out and hide it. The
player need to guess whether the hidden shoe is for left foot or right foot and what is its size.
Write a program to solve this game with cheating computer power.
Input
The first line of input contains the one integer N ( 1 N 105 ), the number of shoes.
The following line contains N integers S1 , S2 ,..., S N 1 | Si | 109 . Shoe i is for left foot if Si 0 ,
otherwise it is for right foot. The size of the shoe is | Si | .
Output
Output one integer R where | R | is equal to the hidden shoe’s size and it is negative if it is a shoe
for left foot, positive otherwise.
2
Problem C: Coin Toss
Time limit: 1 second
Memory limit: 256 Megabytes
A sequence of coin toss can be encoded as a binary string, ‘0’ for head, ‘1’ for tail. Given an
encoded sequence S, toss the coin until S appears in the result sequence then stop.
Let T be the number of coin toss taken for S to appear, find the expected value of T.
Input
The first line of input contains the one integer Q ( 1 Q 104 ), the number of test cases.
The following Q lines, each contain a string S ( 1 | S | 20 ).
Output
Output Q lines, each contains the respective value of T. The answer is considered correct if the
precision error is less than 10 9 .
3
Problem D: Binary String Set
Time limit: 1 second
Memory limit: 256 Megabytes
Let S be a set of strings. S * is a set of the empty string and any concatenation of strings in S (each
string in S can appear multiple times).
Given n strings T1 ,..., Tn . For each Ti , find the least number of characters to remove to satisfy
Ti {0,01,10}* .
Input
The first line of input contains one integer n ( 1 n 106 ). The following n lines, each contains a
string Ti . The total length of all Ti will not exceed 106 .
Output
Output n lines, one integer on each line show the number of characters to remove for the respective
input.
4
Problem E: Special Number
Time limit: 1 second
Memory limit: 256 Megabytes
A number is special if the sum of its digits is a prime number. Given an integer N, count the number
of positive integer pairs ( x, y ) where both x and y are special and x 2 y N .
Input
The first line of input contains the one integer N ( 1 N 1015 ).
Output
Output a single number, the number of satisfied pairs.
5
Problem F: Greetting Card
Time limit: 1 second
Memory limit: 256 Megabytes
In a certain village, N households form circle. Initially, household i’s happiness level is Ai
( 0 i N ). In the morning, each household will write their greetting cards to others but order
them to only arrive at the recipients after work hour. After work, everyone is burnt out and their
happiness level depends solely on the greetting card they receive. Therefore, their happiness level
at the end of the day will be the sum of happiness level of the senders whom they receive from at
the time the cards were written.
On day 0, each household write for their adjacent households only. On each following days, they
double the distance which they send the card to. More specifically, on day t , household i will
write to household (i 2t ) % N and (i 2t % N N ) % N . One household may write for the
same household twice on the same day.
Given an integer K, calculate everyone’s happiness level after 2K days (the end of day 2K 1 ).
Input
The first line of input contains 2 integers N and K ( 2 N 105 , 0 K 109 ), the number of test
cases.
The following line contains N integers Ai ( 1 Ai 109 ).
Output
Output one line contain N integers, represent the happiness level of the households (modulo
109 7 ) after K days respectively.
6
Problem G: Rounded Convex Hull
Time limit: 8 seconds
Memory limit: 512 Megabytes
On a 2D plane, there are N circles and M polygons. Find the perimeter of the convex hull of all
these figures.
Input
The first line of input contains 2 integers N and M ( 0 N , M 105 , 1 N M ), the number of
circles and polygons.
The next N lines, each has 3 numbers xi , yi and ri which is the center of the circle and its radius
The next M lines, each line start with an integer pi ( pi 1 ), the number of vertices in the polygon
followed by pi pair of numbers ( xi ,1 , yi ,1 ),...,( xi , pi , yi , pi ) | xi , j |,| yi , j | 5 104 .Total number of
vertices on all polygons will not exceeed 105 .
Output
Output the perimeter of the convex hull. The answer is considered correct if precision error is less
than 10 5 .
7
Problem H: Grouping
Time limit: 1 second
Memory limit: 256 Megabytes
There are 2n students in a school. Calculate the number of ways pick a group of at least 2 students
so that for pair of students a and b in this group, | a b | 1 and | a b | n x .
Input
The first line of input contains 2 integers n and x 1 n 1018 , 0 x n .
Output
Output a single integer, the answer to the problem modulo 109 7 .
Explaination
In the sample test case, we can pick {1,3} or {2, 4} .
8
Problem I: Gift Box Lottery
Time limit: 3 seconds
Memory limit: 512 Megabytes
Bob takes part in a traditional gift box lottery game held every year. In this game, there are an
infinite number of identical gift boxes with the same content inside. Each box contains N gifts,
labeled from 1 to N, have value A1 , A2 ,..., AN respectively, the players know these values ahead of
t ime. A player must start out with a random gift box, pulling gifts from it randomly, one gift at a
time. All gifts in the box have an equal chance to be pulled. The gift with label 1 is special, after
you pull this gift, you gain the right to skip remaining gifts in this box and move on to the next gift
box. This right can be exercised whenever, as long as you are still pulling from the same box.
Unless using the above special right, you must pull until the current box is empty before moving
to the next box. Pulling special gift from the next box will grant you special right for the new box.
If Bob plays this game optimally, what would be the best value per pull he can get?
Input
The first line of input contains one integer N (1 ≤ N < 20), the number of gifts per gift box.
The following line contains N real numbers Ai ( 0 Ai 106 ), the value of each gifts.
Output
Output a single number, the best value per pull Bob can achieve if he plays optimally. The answer
is considered correct if the precision error is less than 10 5 .
Explaination
In the first sample test case, no matter which gift we pull first from the box, we still need to pick
the other one before moving to the next box, so the result is 1/ 2 0.5 .
In the second sample test case, in case we:
• Pull gift 1 first, move on to next box immediately, result in 1 value from 1 pull.
• Pull gift 2 first, we pull again then move on to next box, result in 1 value from 2 pulls.
Combined, the result is 2 / 3 .
9
Sample Input 1 Sample Output 1
2 0.5
0 1
10
Problem J: Workers Roadmap
Time limit: 1 second
Memory limit: 256 Megabytes
A business received a big order for M products, they must deliver as soon as possible but there is
nothing left in the warehouse, they have to produce from scratch. They have N workers, worker i
produce Ai products every day but take 1 day of leave after every Bi days of work. Calculate
what’s the earliest day they can finish producing M products to deliver.
Input
The first line of input contains 2 integers N and M 1 N 100, 1 M 1015 , the number of
workers and number of products ordered.
Output
Output one integer, the number of days it takes.
11
Problem K: Lexigraphical Matrix
Time limit: 1 second
Memory limit: 256 Megabytes
A Lex Matrix is a matrix of size m×n, m rows, n columns. Rows are numbered from 1 to m,
top to bottom. Columns are numbered from 1 to n, left to right. Ax , y is the y-th value on row x.
Each row is a permutation of 1, 2,..., n .
Lex Matrix A is considered greater than Lex Matrix B if compare each cell starting from the
first row, left to right then to the next row and so on, the first pair of cells (i, j ) where
Ai , j Bi , j , Ai , j Bi , j hold.
Given a Lex Matrix A. You are allowed to pick 2 rows/columns, swap them and repeat by
picking again as many times as you want, modify to achieve the greatest possible Lex Matrix from
A. Let’s call this maximal matrix A . Given q pairs of number xi and yi , 1 xi m, 1 yi n ,
find the value of Axi , yi .
Input
The first line of input contains 2 integers m and n ( 1 m, n 500 ).
The next m lines, representing Lex Matrix A, each contains n numbers, a permutation of 1, 2,..., n .
Output
Output q lines, each contains one integer, the value of Axi , yi .
12
Problem L: Subset Counting
Time limit: 1 second
Memory limit: 256 Megabytes
Given a sequence of integers a1 , a2 ,..., an ; find the number of sets S satisfying the following
conditions:
• S {1, 2,..., n}
• x S : ax S
• y S : (x S : ax y )
Since the result can be rather large, you should output it modulo 998244353.
Input
The input contains multiple test cases. Each test case is presented in two lines as below:
The input is terminated by a line with a single integer 0 which is not a test case. The sum of n over
all test cases does not exceed 106 .
Output
For each test case, write the result on the single line.
Explaination
In the second test cases, 6 valid sets are {1}; {2}; {3}; {1, 2}; {2,3}; {3,1} .
13
Problem M: Graph
Time limit: 1 second
Memory limit: 256 Megabytes
You are given a graph containing n vertices and m directed arcs. Vertices are numbered from
1 to n , inclusive; and arcs are numbered from 1 to m , inclusive. The i-th arc starts from the si -th
vertex, ends at the ti -th vertex and has a cost of ci .
In this problem, a valid path is a sequence of arcs in which the first one starts at the 1-st vertex,
the last one ends at the n-th vertex and every arc ends at the starting vertex of the next one. More
formally, a valid path can be represented by a sequence of indices ( e1 , e2 ,..., ek ) satisfying all below
conditions:
• 1 e1 , e2 ,..., ek m
• se1 1
• sek n
• te j se j 1 for all j such that 0 j k
Please note that these indices do not need to be distinct, meaning that a valid path may walk
through some edge multiple times.
Your task is to mark some (possibly none or all) arcs of the given graph special so as to fullfill
this requirements: Every valid path of this graph passes through special arcs exactly once. In other
words, if the sequence of indices ( f1 , f 2 ,..., f k ) represents a valid path, there should be exactly one
index l such that 1 l k and the f l -th arc is marked special. Moreover, the total cost of special
arcs should be as small as possible.
Input
The input contains multiple test cases. Each test case is presented as below:
The first line contains two integers n and m 2 n 100, 1 m 2500 denoting the
number of vertices and arcs of the graph, respectively.
In the next m lines, each contains three integers si , ti and ci 1 si , ti n, 1 ci 109
denoting the starting and ending vertices of the i-th arc and its cost, respectively.
14
The last line is a blank line.
The input is terminated by two zeros which do not represent a test case. It is guaranteed that:
For every arc, its starting vertex differs from its ending one.
For every graph, each ordered pair ( si , ti ) appears at most once.
A valid path always exists.
The sum of n over all test cases does not exceed 1000. The sum of m over all test cases does not
exceed 25000.
Output
Write the result of each test case in a single line: If it is impossible to mark arcs in order to satisfy
the above require ments, print “IMPOSSIBLE” (without quotes). Otherwise, print the minimum
possible total cost of special arcs.
Explaination
In the first test case, there are three valid paths, whose arc indices are (4,3) , (1,5) and (1, 2,3) .
The optimal solution is to mark the first and the fourth arcs special. Note that marking the first and
the third arcs is not a valid way, since the valid path (1, 2,3) will pass through special arcs twice.
In the second test case, please be aware that a valid path can contain duplicated arcs. Hence,
(2,1, 2) or even (2,1, 2,1, 2) are valid paths of this graph. Consequently, marking the second arc is
not possible, as it appears more than once in those valid paths. As a result, a solution does not exist.
2 2
2 1 1
1 2 1
0 0
15